diff --git a/src/Factory.php b/src/Factory.php index 7821d4f9..555996ed 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -100,10 +100,9 @@ public function create( * @internal */ bool $noProxy = false - ): object - { - if (\count(func_get_args()) === 2 && !str_starts_with(debug_backtrace(options: \DEBUG_BACKTRACE_IGNORE_ARGS, limit: 1)[0]['class'] ?? '', 'Zenstruck\Foundry')) { - trigger_deprecation('zenstruck\foundry', '1.37.0', sprintf('Parameter "$noProxy" of method "%s()" is deprecated and will be removed in Foundry 2.0.', __METHOD__)); + ): object { + if (2 === \count(\func_get_args()) && !\str_starts_with(\debug_backtrace(options: \DEBUG_BACKTRACE_IGNORE_ARGS, limit: 1)[0]['class'] ?? '', 'Zenstruck\Foundry')) { + trigger_deprecation('zenstruck\foundry', '1.37.0', \sprintf('Parameter "$noProxy" of method "%s()" is deprecated and will be removed in Foundry 2.0.', __METHOD__)); } // merge the factory attribute set with the passed attributes @@ -361,6 +360,30 @@ final public static function delayFlush(callable $callback): mixed return self::configuration()->delayFlush($callback); } + /** + * @internal + * + * @return TObject + */ + public function createAndUproxify(): object + { + $object = $this->create( + noProxy: !$this->shouldUseProxy() + ); + + return $object instanceof Proxy ? $object->_real() : $object; + } + + /** + * @internal + * + * @return ($this is PersistentProxyObjectFactory ? true : false) + */ + public function shouldUseProxy(): bool + { + return $this instanceof PersistentProxyObjectFactory; + } + protected function isPersisting(): bool { if (!$this->persist || !self::configuration()->isPersistEnabled() || !self::configuration()->hasManagerRegistry()) { @@ -502,28 +525,4 @@ private function withCascadePersist(): static return $cloned; } - - /** - * @internal - * - * @return TObject - */ - public function createAndUproxify(): object - { - $object = $this->create( - noProxy: !$this->shouldUseProxy() - ); - - return $object instanceof Proxy ? $object->_real() : $object; - } - - /** - * @internal - * - * @return ($this is PersistentProxyObjectFactory ? true : false) - */ - public function shouldUseProxy(): bool - { - return $this instanceof PersistentProxyObjectFactory; - } } diff --git a/src/FactoryCollection.php b/src/FactoryCollection.php index c8a4700e..b7d099a1 100644 --- a/src/FactoryCollection.php +++ b/src/FactoryCollection.php @@ -79,8 +79,8 @@ public function create( */ bool $noProxy = false ): array { - if (\count(func_get_args()) === 2 && !str_starts_with(debug_backtrace(options: \DEBUG_BACKTRACE_IGNORE_ARGS, limit: 1)[0]['class'] ?? '', 'Zenstruck\Foundry')) { - trigger_deprecation('zenstruck\foundry', '1.37.0', sprintf('Parameter "$noProxy" of method "%s()" is deprecated and will be removed in Foundry 2.0.', __METHOD__)); + if (2 === \count(\func_get_args()) && !\str_starts_with(\debug_backtrace(options: \DEBUG_BACKTRACE_IGNORE_ARGS, limit: 1)[0]['class'] ?? '', 'Zenstruck\Foundry')) { + trigger_deprecation('zenstruck\foundry', '1.37.0', \sprintf('Parameter "$noProxy" of method "%s()" is deprecated and will be removed in Foundry 2.0.', __METHOD__)); } $objects = []; diff --git a/src/ModelFactory.php b/src/ModelFactory.php index 1f6ff928..8b7ec423 100644 --- a/src/ModelFactory.php +++ b/src/ModelFactory.php @@ -36,11 +36,11 @@ public function __construct() 'zenstruck\foundry', '1.37.0', \sprintf( <<many($arguments[0])->create(($arguments[1] ?? []), noProxy: true); + return static::new()->many($arguments[0])->create($arguments[1] ?? [], noProxy: true); } /** @@ -99,10 +99,9 @@ public function create( * @internal */ bool $noProxy = false - ): object - { - if (\count(func_get_args()) === 2 && !str_starts_with(debug_backtrace(options: \DEBUG_BACKTRACE_IGNORE_ARGS, limit: 1)[0]['class'] ?? '', 'Zenstruck\Foundry')) { - trigger_deprecation('zenstruck\foundry', '1.37.0', sprintf('Parameter "$noProxy" of method "%s()" is deprecated and will be removed in Foundry 2.0.', __METHOD__)); + ): object { + if (2 === \count(\func_get_args()) && !\str_starts_with(\debug_backtrace(options: \DEBUG_BACKTRACE_IGNORE_ARGS, limit: 1)[0]['class'] ?? '', 'Zenstruck\Foundry')) { + trigger_deprecation('zenstruck\foundry', '1.37.0', \sprintf('Parameter "$noProxy" of method "%s()" is deprecated and will be removed in Foundry 2.0.', __METHOD__)); } return parent::create( @@ -349,12 +348,12 @@ final protected function addState(array|callable $attributes = []): static abstract protected function defaults(): array|callable; /** - * @param list> $proxies + * @param list> $proxies * @return list */ private static function unproxy(array $proxies): array { - return array_map( + return \array_map( static fn(Proxy $proxy) => $proxy->_real(), $proxies ); diff --git a/src/Persistence/PersistentProxyObjectFactory.php b/src/Persistence/PersistentProxyObjectFactory.php index db4c728f..18a818bd 100644 --- a/src/Persistence/PersistentProxyObjectFactory.php +++ b/src/Persistence/PersistentProxyObjectFactory.php @@ -27,12 +27,24 @@ */ abstract class PersistentProxyObjectFactory extends PersistentObjectFactory { + /** + * @phpstan-return list> + */ + public static function __callStatic(string $name, array $arguments): array + { + if ('createMany' !== $name) { + throw new \BadMethodCallException(\sprintf('Call to undefined static method "%s::%s".', static::class, $name)); + } + + return static::new()->many($arguments[0])->create($arguments[1] ?? [], noProxy: false); + } + final public static function new(array|callable|string $defaultAttributes = [], string ...$states): static { if ((new \ReflectionClass(static::class()))->isFinal()) { trigger_deprecation( 'zenstruck\foundry', '1.37.0', - \sprintf('Using a proxy factory with a final class is deprecated and will throw an error in Foundry 2.0. Use "%s" instead, or unfinalize "%s" class.', PersistentProxyObjectFactory::class, static::class()) + \sprintf('Using a proxy factory with a final class is deprecated and will throw an error in Foundry 2.0. Use "%s" instead, or unfinalize "%s" class.', self::class, static::class()) ); } @@ -40,8 +52,6 @@ final public static function new(array|callable|string $defaultAttributes = [], } /** - * @final - * * @return Proxy */ final public function create( @@ -51,27 +61,14 @@ final public function create( * @internal */ bool $noProxy = false - ): object - { - if (\count(func_get_args()) === 2 && !str_starts_with(debug_backtrace(options: \DEBUG_BACKTRACE_IGNORE_ARGS, limit: 1)[0]['class'] ?? '', 'Zenstruck\Foundry')) { - trigger_deprecation('zenstruck\foundry', '1.37.0', sprintf('Parameter "$noProxy" of method "%s()" is deprecated and will be removed in Foundry 2.0.', __METHOD__)); + ): object { + if (2 === \count(\func_get_args()) && !\str_starts_with(\debug_backtrace(options: \DEBUG_BACKTRACE_IGNORE_ARGS, limit: 1)[0]['class'] ?? '', 'Zenstruck\Foundry')) { + trigger_deprecation('zenstruck\foundry', '1.37.0', \sprintf('Parameter "$noProxy" of method "%s()" is deprecated and will be removed in Foundry 2.0.', __METHOD__)); } return Factory::create($attributes, noProxy: false); } - /** - * @phpstan-return list> - */ - public static function __callStatic(string $name, array $arguments): array - { - if ('createMany' !== $name) { - throw new \BadMethodCallException(\sprintf('Call to undefined static method "%s::%s".', static::class, $name)); - } - - return static::new()->many($arguments[0])->create($arguments[1] ?? [], noProxy: false); - } - /** * A shortcut to create a single model without states. * diff --git a/src/Persistence/Proxy.php b/src/Persistence/Proxy.php index 7755e2c8..9200d9bb 100644 --- a/src/Persistence/Proxy.php +++ b/src/Persistence/Proxy.php @@ -19,7 +19,6 @@ use Zenstruck\Callback\Parameter; use Zenstruck\Foundry\Factory; use Zenstruck\Foundry\Instantiator; -use Zenstruck\Foundry\Persistence\Proxy as ProxyBase; /** * @template TProxiedObject of object diff --git a/src/Persistence/functions.php b/src/Persistence/functions.php index 68425157..3509a80c 100644 --- a/src/Persistence/functions.php +++ b/src/Persistence/functions.php @@ -22,7 +22,6 @@ * @see Configuration::repositoryFor() * * @template TObject of object - * */ function repository(string $class): RepositoryDecorator { @@ -58,7 +57,7 @@ function persist_proxy(string $class, array|callable $attributes = []): Proxy * * @template T of object * - * @param class-string $class + * @param class-string $class * @param array|callable(int):array $attributes * * @return PersistentObjectFactory @@ -73,7 +72,7 @@ function persistent_factory(string $class, array|callable $attributes = []): Per * * @template T of object * - * @param class-string $class + * @param class-string $class * @param array|callable(int):array $attributes * * @return PersistentProxyObjectFactory @@ -81,15 +80,12 @@ function persistent_factory(string $class, array|callable $attributes = []): Per function proxy_factory(string $class, array|callable $attributes = []): PersistentProxyObjectFactory { if ((new \ReflectionClass($class))->isFinal()) { - throw new \RuntimeException( - \sprintf('Cannot create PersistentProxyObjectFactory for final class "%s". Pass parameter "$withProxy" to false instead, or unfinalize "%1$s" class.', $class) - ); + throw new \RuntimeException(\sprintf('Cannot create PersistentProxyObjectFactory for final class "%s". Pass parameter "$withProxy" to false instead, or unfinalize "%1$s" class.', $class)); } return AnonymousFactoryGenerator::create($class, PersistentProxyObjectFactory::class)::new($attributes); } - /** * @param callable():void $callback */ diff --git a/src/functions.php b/src/functions.php index 1f269654..48355b16 100644 --- a/src/functions.php +++ b/src/functions.php @@ -14,6 +14,7 @@ use Faker; use Zenstruck\Foundry\Persistence\Proxy; use Zenstruck\Foundry\Persistence\RepositoryDecorator; + use function Zenstruck\Foundry\Persistence\persist_proxy; use function Zenstruck\Foundry\Persistence\persistent_factory; use function Zenstruck\Foundry\Persistence\proxy_factory; @@ -47,7 +48,7 @@ function factory(string $class, array|callable $defaultAttributes = []): Anonymo */ function anonymous(string $class, array|callable $defaultAttributes = []): Factory { - trigger_deprecation('zenstruck\foundry', '1.37', sprintf('Usage of "%s()" function is deprecated and will be removed in 2.0. Use the "Zenstruck\Foundry\Persistence\proxy_factory()" function instead.', __FUNCTION__)); + trigger_deprecation('zenstruck\foundry', '1.37', \sprintf('Usage of "%s()" function is deprecated and will be removed in 2.0. Use the "Zenstruck\Foundry\Persistence\proxy_factory()" function instead.', __FUNCTION__)); return proxy_factory($class, $defaultAttributes); } diff --git a/tests/Functional/FactoryTest.php b/tests/Functional/FactoryTest.php index de8d59ee..5a1e7444 100644 --- a/tests/Functional/FactoryTest.php +++ b/tests/Functional/FactoryTest.php @@ -23,10 +23,10 @@ use Zenstruck\Foundry\Tests\Fixtures\Object\SomeObjectFactory; use Zenstruck\Foundry\Tests\Fixtures\Object\SomeOtherObject; -use function Zenstruck\Foundry\Persistence\persistent_factory; use function Zenstruck\Foundry\object; use function Zenstruck\Foundry\Persistence\flush_after; use function Zenstruck\Foundry\Persistence\persist; +use function Zenstruck\Foundry\Persistence\persistent_factory; use function Zenstruck\Foundry\Persistence\repository; /** diff --git a/tests/Functional/ModelFactoryTest.php b/tests/Functional/ModelFactoryTest.php index bcf2805f..7b3375d2 100644 --- a/tests/Functional/ModelFactoryTest.php +++ b/tests/Functional/ModelFactoryTest.php @@ -18,10 +18,10 @@ use Zenstruck\Foundry\Test\Factories; use Zenstruck\Foundry\Test\ResetDatabase; -use function Zenstruck\Foundry\Persistence\persistent_factory; use function Zenstruck\Foundry\Persistence\disable_persisting; use function Zenstruck\Foundry\Persistence\enable_persisting; use function Zenstruck\Foundry\Persistence\persist; +use function Zenstruck\Foundry\Persistence\persistent_factory; /** * @author Kevin Bond diff --git a/tests/Functional/ORMProxyTest.php b/tests/Functional/ORMProxyTest.php index d7fe695c..c915eebe 100644 --- a/tests/Functional/ORMProxyTest.php +++ b/tests/Functional/ORMProxyTest.php @@ -11,14 +11,12 @@ namespace Zenstruck\Foundry\Tests\Functional; -use Zenstruck\Foundry\Tests\Fixtures\Entity\Address; use Zenstruck\Foundry\Tests\Fixtures\Entity\Category; use Zenstruck\Foundry\Tests\Fixtures\Entity\Contact; use Zenstruck\Foundry\Tests\Fixtures\Entity\Post; use Zenstruck\Foundry\Tests\Fixtures\Factories\CategoryFactory; use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactory; -use function Zenstruck\Foundry\Persistence\persistent_factory; use function Zenstruck\Foundry\Persistence\proxy_factory; /** diff --git a/tests/Functional/PersistentObjectFactoryTest.php b/tests/Functional/PersistentObjectFactoryTest.php index 2e6472a5..883a1786 100644 --- a/tests/Functional/PersistentObjectFactoryTest.php +++ b/tests/Functional/PersistentObjectFactoryTest.php @@ -2,6 +2,15 @@ declare(strict_types=1); +/* + * This file is part of the zenstruck/foundry package. + * + * (c) Kevin Bond + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Functional; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; diff --git a/tests/Unit/FactoryTest.php b/tests/Unit/FactoryTest.php index cd774743..9c020d2e 100644 --- a/tests/Unit/FactoryTest.php +++ b/tests/Unit/FactoryTest.php @@ -24,8 +24,8 @@ use Zenstruck\Foundry\Tests\Fixtures\Entity\Post; use Zenstruck\Foundry\Tests\Fixtures\Factories\LegacyPostFactory; -use function Zenstruck\Foundry\Persistence\persistent_factory; use function Zenstruck\Foundry\lazy; +use function Zenstruck\Foundry\Persistence\persistent_factory; use function Zenstruck\Foundry\Persistence\proxy_factory; /**