Skip to content

Commit

Permalink
fix(sca): fix type system with legacy FactoryCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Apr 1, 2024
1 parent bbcef6a commit 15fd4ab
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 15 deletions.
33 changes: 29 additions & 4 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ parameters:
path: src/Configuration.php

-
message: "#^Method Zenstruck\\\\Foundry\\\\Factory\\:\\:createAndUproxify\\(\\) should return TObject of object but returns object\\.$#"
message: "#^Method Zenstruck\\\\Foundry\\\\Factory\\:\\:__call\\(\\) should return array\\<int, \\(TObject of object\\)\\|Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TObject of object\\>\\> but returns array\\<int, \\(TObject of object\\)\\|Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TObject of object\\>\\|Zenstruck\\\\Foundry\\\\Proxy\\<Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TObject of object\\>\\>\\>\\.$#"
count: 1
path: src/Factory.php

-
message: "#^Method Zenstruck\\\\Foundry\\\\Factory\\:\\:createAndUnproxify\\(\\) should return TObject of object but returns object\\.$#"
count: 1
path: src/Factory.php

Expand All @@ -26,7 +31,7 @@ parameters:
path: src/Factory.php

-
message: "#^Method Zenstruck\\\\Foundry\\\\FactoryCollection\\:\\:create\\(\\) should return array\\<int, TObject of object\\> but returns array\\<int\\<0, max\\>, \\(TObject of object\\)\\|Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TObject of object\\>\\>\\.$#"
message: "#^Method Zenstruck\\\\Foundry\\\\FactoryCollection\\:\\:create\\(\\) should return array\\<int, \\(TObject of object\\)\\|Zenstruck\\\\Foundry\\\\Proxy\\<TObject of object\\>\\> but returns array\\<int\\<0, max\\>, \\(TObject of object\\)\\|Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TObject of object\\>\\>\\.$#"
count: 1
path: src/FactoryCollection.php

Expand Down Expand Up @@ -56,12 +61,12 @@ parameters:
path: src/Instantiator.php

-
message: "#^Method Zenstruck\\\\Foundry\\\\ObjectFactory\\:\\:__callStatic\\(\\) should return array\\<int, TModel of object\\> but returns array\\<int, \\(TModel of object\\)\\|Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TModel of object\\>\\>\\.$#"
message: "#^Method Zenstruck\\\\Foundry\\\\ObjectFactory\\:\\:__callStatic\\(\\) should return array\\<int, TModel of object\\> but returns array\\<int, \\(TModel of object\\)\\|Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TModel of object\\>\\|Zenstruck\\\\Foundry\\\\Proxy\\<Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TModel of object\\>\\>\\>\\.$#"
count: 1
path: src/ObjectFactory.php

-
message: "#^Method Zenstruck\\\\Foundry\\\\ObjectFactory\\:\\:createSequence\\(\\) should return array\\<int, TModel of object\\> but returns array\\<int, \\(TModel of object\\)\\|Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TModel of object\\>\\>\\.$#"
message: "#^Method Zenstruck\\\\Foundry\\\\ObjectFactory\\:\\:createSequence\\(\\) should return array\\<int, TModel of object\\> but returns array\\<int, \\(TModel of object\\)\\|Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TModel of object\\>\\|Zenstruck\\\\Foundry\\\\Proxy\\<Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TModel of object\\>\\>\\>\\.$#"
count: 1
path: src/ObjectFactory.php

Expand All @@ -75,6 +80,16 @@ parameters:
count: 1
path: src/ObjectFactory.php

-
message: "#^Method Zenstruck\\\\Foundry\\\\Persistence\\\\PersistentProxyObjectFactory\\:\\:__callStatic\\(\\) should return array\\<int, Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TModel of object\\>\\> but returns array\\<int, Zenstruck\\\\Foundry\\\\Proxy\\<Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TModel of object\\>\\>\\>\\.$#"
count: 1
path: src/Persistence/PersistentProxyObjectFactory.php

-
message: "#^Method Zenstruck\\\\Foundry\\\\Persistence\\\\PersistentProxyObjectFactory\\:\\:createSequence\\(\\) should return array\\<int, Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TModel of object\\>\\> but returns array\\<int, Zenstruck\\\\Foundry\\\\Proxy\\<Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TModel of object\\>\\>\\>\\.$#"
count: 1
path: src/Persistence/PersistentProxyObjectFactory.php

-
message: "#^Should not use function \"debug_backtrace\", please change the code\\.$#"
count: 1
Expand Down Expand Up @@ -149,3 +164,13 @@ parameters:
message: "#^Parameter \\#1 \\$configuration of static method Zenstruck\\\\Foundry\\\\Factory\\<object\\>\\:\\:boot\\(\\) expects Zenstruck\\\\Foundry\\\\Configuration, object\\|null given\\.$#"
count: 1
path: src/ZenstruckFoundryBundle.php

-
message: "#^Function Zenstruck\\\\Foundry\\\\create_many\\(\\) should return array\\<int, Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TObject of object\\>\\> but returns array\\<int, Zenstruck\\\\Foundry\\\\Proxy\\<Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TObject of object\\>\\>\\>\\.$#"
count: 1
path: src/functions.php

-
message: "#^Function Zenstruck\\\\Foundry\\\\instantiate_many\\(\\) should return array\\<int, Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TObject of object\\>\\> but returns array\\<int, Zenstruck\\\\Foundry\\\\Proxy\\<Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TObject of object\\>\\>\\>\\.$#"
count: 1
path: src/functions.php
12 changes: 6 additions & 6 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ final public static function delayFlush(callable $callback): mixed
*
* @return TObject
*/
public function createAndUproxify(): object
public function createAndUnproxify(): object
{
$object = $this->create(
noProxy: !$this->shouldUseProxy(),
Expand Down Expand Up @@ -469,25 +469,25 @@ private function normalizeAttribute(mixed $value, string $name): mixed
}

if (!self::configuration()->hasManagerRegistry()) {
return $value->createAndUproxify();
return $value->createAndUnproxify();
}

try {
$objectManager = self::configuration()->objectManagerFor($this->class);

if (!$objectManager instanceof EntityManagerInterface || $objectManager->getClassMetadata($value->class)->isEmbeddedClass) {
// we may deal with ODM document or ORM\Embedded
return $value->createAndUproxify();
return $value->createAndUnproxify();
}
} catch (\Throwable) {
// not persisted object
return $value->createAndUproxify();
return $value->createAndUnproxify();
}

$relationshipMetadata = self::getRelationshipMetadata($objectManager, $this->class, $name);

if (!$relationshipMetadata) {
return $value->createAndUproxify();
return $value->createAndUnproxify();
}

if ($relationshipMetadata['isOwningSide']) {
Expand All @@ -506,7 +506,7 @@ private function normalizeAttribute(mixed $value, string $name): mixed
$value = $value->withCascadePersist();
}

return $value->createAndUproxify();
return $value->createAndUnproxify();
}

private static function normalizeObject(object $object): object
Expand Down
2 changes: 1 addition & 1 deletion src/FactoryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static function sequence(Factory $factory, iterable $sequence): self
}

/**
* @return list<TObject>
* @return ($noProxy is true ? list<TObject> : list<Proxy<TObject>>)
*/
public function create(
array|callable $attributes = [],
Expand Down
19 changes: 17 additions & 2 deletions src/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,26 @@
*
* @method static Proxy[]|TModel[] createMany(int $number, array|callable $attributes = [])
*
* @phpstan-method FactoryCollection<Proxy<TModel>> sequence(iterable<array<string, mixed>>|callable(): iterable<array<string, mixed>> $sequence)
* @phpstan-method FactoryCollection<Proxy<TModel>> many(int $min, int|null $max = null)
* @phpstan-method Proxy<TModel> create(array|callable $attributes = [])
* @phpstan-method static Proxy<TModel> createOne(array $attributes = [])
* @phpstan-method static Proxy<TModel> find(object|array|mixed $criteria)
* @phpstan-method static Proxy<TModel> findOrCreate(array $attributes)
* @phpstan-method static Proxy<TModel> first(string $sortedField = 'id')
* @phpstan-method static Proxy<TModel> last(string $sortedField = 'id')
* @phpstan-method static Proxy<TModel> random(array $attributes = [])
* @phpstan-method static Proxy<TModel> randomOrCreate(array $attributes = [])
*
* @phpstan-method FactoryCollection<TModel> sequence(iterable<array<string, mixed>>|callable(): iterable<array<string, mixed>> $sequence)
* @phpstan-method FactoryCollection<TModel> many(int $min, int|null $max = null)
*
* @phpstan-method static list<Proxy<TModel>> all()
* @phpstan-method static list<Proxy<TModel>> createSequence(iterable<array<string, mixed>>|callable(): iterable<array<string, mixed>> $sequence)
* @phpstan-method static list<Proxy<TModel>> createMany(int $number, array|callable $attributes = [])
* @phpstan-method static list<Proxy<TModel>> createMany(int $number, array|callable $attributes = [])
* @phpstan-method static list<Proxy<TModel>> createSequence(array|callable $sequence)
* @phpstan-method static list<Proxy<TModel>> findBy(array $attributes)
* @phpstan-method static list<Proxy<TModel>> randomRange(int $min, int $max, array $attributes = [])
* @phpstan-method static list<Proxy<TModel>> randomSet(int $number, array $attributes = [])
*
* @author Kevin Bond <kevinbond@gmail.com>
*
Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/ProxyRepositoryDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public function find($criteria)
}

if ($attributeValue instanceof Factory) {
$attributeValue = $attributeValue->withoutPersisting()->createAndUproxify();
$attributeValue = $attributeValue->withoutPersisting()->createAndUnproxify();
} elseif ($attributeValue instanceof Proxy) {
$attributeValue = $attributeValue->_real();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/RepositoryDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public function find($criteria)
}

if ($attributeValue instanceof Factory) {
$attributeValue = $attributeValue->withoutPersisting()->createAndUproxify();
$attributeValue = $attributeValue->withoutPersisting()->createAndUnproxify();
} elseif ($attributeValue instanceof Proxy) {
$attributeValue = $attributeValue->_real();
}
Expand Down

0 comments on commit 15fd4ab

Please sign in to comment.