Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(bc): fix SCA in factory collection #4

Draft
wants to merge 1 commit into
base: 1.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 17 additions & 3 deletions src/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,33 @@

use Zenstruck\Foundry\Persistence\PersistentObjectFactory;
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
use Zenstruck\Foundry\Persistence\Proxy;

/**
* @template TModel of object
* @template-extends PersistentProxyObjectFactory<TModel>
*
* @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
41 changes: 41 additions & 0 deletions utils/phpstan/tests/FoundryRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Foundry\Tests\PhpStan;

use PHPStan\Testing\TypeInferenceTestCase;

final class FoundryRuleTest extends TypeInferenceTestCase
{
/**
* @return iterable<mixed>
*/
public function typesProvider(): iterable
{
yield from $this->gatherAssertTypes(__DIR__.'/test-types.php');
}

/**
* @test
* @dataProvider typesProvider
*/
public function it_can_resolve_tests(string $assertType, string $file, mixed ...$args): void
{
$this->assertFileAsserts($assertType, $file, ...$args);
}

public static function getAdditionalConfigFiles(): array
{
return [__DIR__.'/../../phpstan-foundry.neon'];
}
}
2 changes: 1 addition & 1 deletion utils/rector/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"rector/rector": "^1.0",
"phpunit/phpunit": "^10.5",
"phpstan/phpstan-doctrine": "^1.3",
"zenstruck/foundry": "dev-1.x-bc",
"zenstruck/foundry": "1.x-dev",
"symfony/var-dumper": "^7.0",
"doctrine/orm": "^2.17",
"symfony/framework-bundle": "^7.0",
Expand Down
Loading