Skip to content

Commit

Permalink
bot: fix cs [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil authored and kbond committed Nov 15, 2023
1 parent 4e13b68 commit 22f1374
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 75 deletions.
55 changes: 27 additions & 28 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/FactoryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
10 changes: 5 additions & 5 deletions src/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ public function __construct()
'zenstruck\foundry', '1.37.0',
\sprintf(
<<<MESSAGE
Class "%s" is deprecated and will be removed in version 2.0. Use "%s" instead.
Be aware that both classes exist "%s" and "%s".
You should use the first one if you don't need a proxy or if the target class of the factory is final:
in Foundry 2.0 proxy system will leverage Symfony's lazy object system and it will be impossible to create a proxy for a final class.
MESSAGE,
Class "%s" is deprecated and will be removed in version 2.0. Use "%s" instead.
Be aware that both classes exist "%s" and "%s".
You should use the first one if you don't need a proxy or if the target class of the factory is final:
in Foundry 2.0 proxy system will leverage Symfony's lazy object system and it will be impossible to create a proxy for a final class.
MESSAGE,
self::class,
$newFactoryClass,
PersistentObjectFactory::class,
Expand Down
13 changes: 6 additions & 7 deletions src/Persistence/PersistentObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function __callStatic(string $name, array $arguments): array
throw new \BadMethodCallException(\sprintf('Call to undefined static method "%s::%s".', static::class, $name));
}

return static::new()->many($arguments[0])->create(($arguments[1] ?? []), noProxy: true);
return static::new()->many($arguments[0])->create($arguments[1] ?? [], noProxy: true);
}

/**
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -349,12 +348,12 @@ final protected function addState(array|callable $attributes = []): static
abstract protected function defaults(): array|callable;

/**
* @param list<Proxy<TModel>> $proxies
* @param list<Proxy<TModel>> $proxies
* @return list<TModel>
*/
private static function unproxy(array $proxies): array
{
return array_map(
return \array_map(
static fn(Proxy $proxy) => $proxy->_real(),
$proxies
);
Expand Down
35 changes: 16 additions & 19 deletions src/Persistence/PersistentProxyObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,31 @@
*/
abstract class PersistentProxyObjectFactory extends PersistentObjectFactory
{
/**
* @phpstan-return list<Proxy<TModel>>
*/
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())
);
}

return parent::new($defaultAttributes, ...$states);
}

/**
* @final
*
* @return Proxy<TModel>
*/
final public function create(
Expand All @@ -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<Proxy<TModel>>
*/
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.
*
Expand Down
1 change: 0 additions & 1 deletion src/Persistence/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 3 additions & 7 deletions src/Persistence/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
* @see Configuration::repositoryFor()
*
* @template TObject of object
*
*/
function repository(string $class): RepositoryDecorator
{
Expand Down Expand Up @@ -58,7 +57,7 @@ function persist_proxy(string $class, array|callable $attributes = []): Proxy
*
* @template T of object
*
* @param class-string<T> $class
* @param class-string<T> $class
* @param array<string,mixed>|callable(int):array<string,mixed> $attributes
*
* @return PersistentObjectFactory<T>
Expand All @@ -73,23 +72,20 @@ function persistent_factory(string $class, array|callable $attributes = []): Per
*
* @template T of object
*
* @param class-string<T> $class
* @param class-string<T> $class
* @param array<string,mixed>|callable(int):array<string,mixed> $attributes
*
* @return PersistentProxyObjectFactory<T>
*/
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
*/
Expand Down
3 changes: 2 additions & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/ModelFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <kevinbond@gmail.com>
Expand Down
2 changes: 0 additions & 2 deletions tests/Functional/ORMProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/Functional/PersistentObjectFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

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 Functional;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down

0 comments on commit 22f1374

Please sign in to comment.