Skip to content

Commit 70edb37

Browse files
committed
Merge branch 'cleanup'
2 parents f901360 + ce2e0b3 commit 70edb37

26 files changed

+322
-104
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
/tests/fixtures/Toolkit/Sync/Entity/Provider/PhotoProvider.php linguist-generated
4848
/tests/fixtures/Toolkit/Sync/Entity/Provider/PostProvider.php linguist-generated
4949
/tests/fixtures/Toolkit/Sync/Entity/Provider/TaskProvider.php linguist-generated
50+
/tests/fixtures/Toolkit/Sync/Entity/Provider/UnimplementedProvider.php linguist-generated
5051
/tests/fixtures/Toolkit/Sync/Entity/Provider/UserProvider.php linguist-generated
5152
/tests/fixtures/Toolkit/Sync/Entity/Task.php linguist-generated
5253
/tests/fixtures/Toolkit/Sync/Entity/User.php linguist-generated

phpstan-baseline-7.4.neon

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpstan-baseline-8.3.neon

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/generate.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
use Salient\Tests\Sync\Entity\Photo;
5858
use Salient\Tests\Sync\Entity\Post;
5959
use Salient\Tests\Sync\Entity\Task;
60+
use Salient\Tests\Sync\Entity\Unimplemented;
6061
use Salient\Tests\Sync\Entity\User;
6162
use Salient\Tests\Sync\Provider\JsonPlaceholderApi;
6263

@@ -103,6 +104,7 @@
103104
Post::class => [],
104105
Task::class => [],
105106
User::class => [],
107+
Unimplemented::class => [],
106108
];
107109

108110
$app = new CliApplication(dirname(__DIR__));

src/Sli/Command/Generate/GenerateSyncEntity.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,11 @@ protected static function getRemovablePrefixes(): ?array
400400
continue;
401401
}
402402

403+
if (is_array($value) && Arr::ofString(array_keys($value))) {
404+
$properties[$key] = 'array<string,mixed>|null';
405+
continue;
406+
}
407+
403408
$type = gettype($value);
404409
$type = $typeMap[$type] ?? $type;
405410
$type .= '|null';

src/Toolkit/Cache/CacheStore.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ public function getAllKeys(?int $maxAge = null): array
443443
*
444444
* @param int|null $now If given, items expire relative to this Unix
445445
* timestamp instead of the time {@see CacheStore::asOfNow()} is called.
446+
* @return static
446447
*/
447448
public function asOfNow(?int $now = null): self
448449
{
@@ -468,6 +469,9 @@ private function now(): int
468469
: $this->Now;
469470
}
470471

472+
/**
473+
* @return static
474+
*/
471475
private function maybeAsOfNow(): self
472476
{
473477
return $this->Now === null

src/Toolkit/Container/Container.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ private function addRule(string $id, array $rule): void
263263
}
264264

265265
/**
266-
* @inheritDoc
266+
* @return static
267267
*/
268268
final public function inContextOf(string $id): self
269269
{
@@ -358,7 +358,7 @@ private function _bind(
358358
}
359359

360360
/**
361-
* @inheritDoc
361+
* @return $this
362362
*/
363363
final public function bind(
364364
string $id,
@@ -369,7 +369,7 @@ final public function bind(
369369
}
370370

371371
/**
372-
* @inheritDoc
372+
* @return $this
373373
*/
374374
final public function bindIf(
375375
string $id,
@@ -384,7 +384,7 @@ final public function bindIf(
384384
}
385385

386386
/**
387-
* @inheritDoc
387+
* @return $this
388388
*/
389389
final public function singleton(
390390
string $id,
@@ -395,7 +395,7 @@ final public function singleton(
395395
}
396396

397397
/**
398-
* @inheritDoc
398+
* @return $this
399399
*/
400400
final public function singletonIf(
401401
string $id,
@@ -418,7 +418,7 @@ final public function hasProvider(string $id): bool
418418
}
419419

420420
/**
421-
* @inheritDoc
421+
* @return $this
422422
*/
423423
final public function provider(
424424
string $id,
@@ -520,7 +520,7 @@ private function applyService(
520520
}
521521

522522
/**
523-
* @inheritDoc
523+
* @return $this
524524
*/
525525
final public function addContextualBinding($context, string $dependency, $value): self
526526
{
@@ -563,7 +563,7 @@ final public function addContextualBinding($context, string $dependency, $value)
563563
}
564564

565565
/**
566-
* @inheritDoc
566+
* @return $this
567567
*/
568568
final public function instance(string $id, $instance): self
569569
{
@@ -573,7 +573,7 @@ final public function instance(string $id, $instance): self
573573
}
574574

575575
/**
576-
* @inheritDoc
576+
* @return $this
577577
*/
578578
final public function instanceIf(string $id, $instance): self
579579
{
@@ -585,7 +585,7 @@ final public function instanceIf(string $id, $instance): self
585585
}
586586

587587
/**
588-
* @inheritDoc
588+
* @return $this
589589
*/
590590
final public function providers(
591591
array $serviceMap,
@@ -630,7 +630,7 @@ final public function getProviders(): array
630630
}
631631

632632
/**
633-
* @inheritDoc
633+
* @return $this
634634
*/
635635
final public function unbind(string $id): self
636636
{
@@ -640,7 +640,7 @@ final public function unbind(string $id): self
640640
}
641641

642642
/**
643-
* @inheritDoc
643+
* @return $this
644644
*/
645645
final public function unbindInstance(string $id): self
646646
{

src/Toolkit/Sync/AbstractSyncDefinition.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,9 @@ abstract protected function getClosure($operation): ?Closure;
215215
private array $Closures = [];
216216

217217
/**
218-
* @var static
218+
* @var static|null
219219
*/
220-
private self $WithoutOverrides;
220+
private $WithoutOverrides;
221221

222222
/**
223223
* @param class-string<TEntity> $entity
@@ -296,7 +296,7 @@ public function __construct(
296296
public function __clone()
297297
{
298298
$this->Closures = [];
299-
unset($this->WithoutOverrides);
299+
$this->WithoutOverrides = null;
300300
}
301301

302302
/**
@@ -408,7 +408,7 @@ function (SyncContextInterface $ctx, $id, ...$args) use ($closure) {
408408
*/
409409
final public function getFallbackClosure($operation): ?Closure
410410
{
411-
if (!isset($this->WithoutOverrides)) {
411+
if ($this->WithoutOverrides === null) {
412412
$clone = clone $this;
413413
$clone->Overrides = [];
414414
$this->WithoutOverrides = $clone;

src/Toolkit/Sync/AbstractSyncProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ final public function with(string $entity, $context = null): SyncEntityProvider
264264

265265
return $container->get(
266266
SyncEntityProvider::class,
267-
[$entity, $this, $this->getDefinition($entity), $context]
267+
[$entity, $this, $context]
268268
);
269269
}
270270

src/Toolkit/Sync/Support/SyncEntityProvider.php

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Salient\Contract\Sync\SyncProviderInterface;
1717
use Salient\Iterator\IterableIterator;
1818
use Salient\Sync\Exception\SyncOperationNotImplementedException;
19+
use Salient\Sync\AbstractSyncEntity;
1920
use Salient\Sync\SyncStore;
2021
use Generator;
2122
use LogicException;
@@ -76,13 +77,11 @@ final class SyncEntityProvider implements SyncEntityProviderInterface
7677
/**
7778
* @param class-string<TEntity> $entity
7879
* @param TProvider $provider
79-
* @param SyncDefinitionInterface<TEntity,TProvider> $definition
8080
*/
8181
public function __construct(
8282
ContainerInterface $container,
8383
string $entity,
8484
SyncProviderInterface $provider,
85-
SyncDefinitionInterface $definition,
8685
?SyncContextInterface $context = null
8786
) {
8887
if (!is_a($entity, SyncEntityInterface::class, true)) {
@@ -93,14 +92,62 @@ public function __construct(
9392
));
9493
}
9594

96-
$entityProvider = SyncIntrospector::entityToProvider($entity);
97-
if (!($provider instanceof $entityProvider)) {
98-
throw new LogicException(get_class($provider) . ' does not implement ' . $entityProvider);
95+
if ($context && $context->getProvider() !== $provider) {
96+
throw new LogicException(sprintf(
97+
'%s has different provider (%s, expected %s)',
98+
get_class($context),
99+
$context->getProvider()->name(),
100+
$provider->name(),
101+
));
102+
}
103+
104+
$_entity = $entity;
105+
$checked = [];
106+
do {
107+
$entityProvider = SyncIntrospector::entityToProvider($entity, $container);
108+
if (interface_exists($entityProvider)) {
109+
break;
110+
}
111+
$checked[] = $entityProvider;
112+
$entityProvider = null;
113+
$entity = get_parent_class($entity);
114+
if ($entity === false ||
115+
$entity === AbstractSyncEntity::class ||
116+
!is_a($entity, SyncEntityInterface::class, true)) {
117+
break;
118+
}
119+
} while (true);
120+
121+
if ($entityProvider === null) {
122+
throw new LogicException(sprintf(
123+
'%s does not have a provider interface (tried: %s)',
124+
$_entity,
125+
implode(', ', $checked),
126+
));
127+
}
128+
129+
if (!is_a($provider, $entityProvider)) {
130+
throw new LogicException(sprintf(
131+
'%s does not implement %s',
132+
get_class($provider),
133+
$entityProvider
134+
));
135+
}
136+
137+
/** @var class-string $entity */
138+
if ($entity !== $_entity && $container->getName($entity) !== $_entity) {
139+
throw new LogicException(sprintf(
140+
'%s cannot be serviced by provider interface %s unless it is bound to the container as %s',
141+
$_entity,
142+
$entityProvider,
143+
$entity,
144+
));
99145
}
100146

147+
/** @var class-string<TEntity> $entity */
101148
$this->Entity = $entity;
102149
$this->Provider = $provider;
103-
$this->Definition = $definition;
150+
$this->Definition = $provider->getDefinition($entity);
104151
$this->Context = $context ?? $provider->getContext($container);
105152
$this->Store = $provider->store();
106153
}

0 commit comments

Comments
 (0)