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: more deprecations #547

Merged
merged 2 commits into from
Jan 9, 2024
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
/.env.local
/docker-compose.override.yaml
/tests/Fixtures/Migrations/

/utils/rector/vendor
/utils/rector/.phpunit.result.cache
/utils/rector/composer.lock
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ parameters:

-
message: "#^Should not use function \"debug_backtrace\", please change the code\\.$#"
count: 1
count: 2
path: src/Factory.php

-
Expand Down
9 changes: 8 additions & 1 deletion src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public function create(
final public function many(int $min, ?int $max = null): FactoryCollection
{
if (!$max) {
return FactoryCollection::set($this, $min);
return FactoryCollection::many($this, $min);
}

return FactoryCollection::range($this, $min, $max);
Expand Down Expand Up @@ -354,6 +354,13 @@ final public static function isBooted(): bool

final public static function faker(): Faker\Generator
{
if (
null === ($calledClass = \debug_backtrace(options: \DEBUG_BACKTRACE_IGNORE_ARGS, limit: 2)[1]['class'] ?? null)
|| !is_a($calledClass, self::class, allow_string: true)
) {
trigger_deprecation('zenstruck\foundry', '1.37.0', 'Method "%s()" will be protected in Foundry 2.0 and should not be called from outside of a factory. Use function "Zenstruck\Foundry\faker()" instead.', __METHOD__);
}

try {
return self::configuration()->faker();
} catch (FoundryBootException) {
Expand Down
10 changes: 10 additions & 0 deletions src/FactoryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,17 @@ public function __construct(private Factory $factory, ?int $min = null, ?int $ma
$this->max = $max ?? $min;
}

/**
* @deprecated Use FactoryCollection::many() instead
*/
public static function set(Factory $factory, int $count): self
{
trigger_deprecation('zenstruck/foundry', '1.37.0', 'Method %s() is deprecated and will be removed in 2.0. Use "%s::many()" instead.', __METHOD__, __CLASS__);

return self::many($factory, $count);
}

public static function many(Factory $factory, int $count): self
{
return new self($factory, $count, null, null, true);
}
Expand Down
32 changes: 28 additions & 4 deletions src/Object/Instantiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function __invoke(array $attributes, string $class): object

if ($this->alwaysForceProperties || \in_array($attribute, $this->forceProperties, true)) {
try {
self::forceSet($object, $attribute, $value);
self::forceSet($object, $attribute, $value, calledInternally: true);
} catch (\InvalidArgumentException $e) {
if (!$this->allowExtraAttributes) {
throw $e;
Expand All @@ -80,7 +80,7 @@ public function __invoke(array $attributes, string $class): object
if (0 === \mb_strpos($attribute, 'force:')) {
trigger_deprecation('zenstruck\foundry', '1.5.0', 'Using "force:" property prefixes is deprecated, use Instantiator::alwaysForce() instead (https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#instantiation).');

self::forceSet($object, \mb_substr($attribute, 6), $value);
self::forceSet($object, \mb_substr($attribute, 6), $value, calledInternally: true);

continue;
}
Expand Down Expand Up @@ -195,18 +195,42 @@ public function alwaysForce(string ...$properties): self
}

/**
* @deprecated
* @throws \InvalidArgumentException if property does not exist for $object
*/
public static function forceSet(object $object, string $property, mixed $value): void
public static function forceSet(
object $object,
string $property,
mixed $value,
/**
* @internal
*/
bool $calledInternally = false): void
{
if (!$calledInternally) {
trigger_deprecation('zenstruck\foundry', '1.37.0', 'Method "%s()" is deprecated with no replacement.', __METHOD__);
}

self::accessibleProperty($object, $property)->setValue($object, $value);
}

/**
* @deprecated
* @return mixed
*/
public static function forceGet(object $object, string $property)
public static function forceGet(
object $object,
string $property,
/**
* @internal
*/
bool $calledInternally = false
)
{
if (!$calledInternally) {
trigger_deprecation('zenstruck\foundry', '1.37.0', 'Method "%s()" is deprecated with no replacement.', __METHOD__);
}

return self::accessibleProperty($object, $property)->getValue($object);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public function _set(string $property, mixed $value): static
{
$object = $this->_real();

Instantiator::forceSet($object, $property, $value);
Instantiator::forceSet($object, $property, $value, calledInternally: true);

return $this;
}
Expand All @@ -271,7 +271,7 @@ public function forceSetAll(array $properties): static
$object = $this->_real();

foreach ($properties as $property => $value) {
Instantiator::forceSet($object, $property, $value);
Instantiator::forceSet($object, $property, $value, calledInternally: true);
}

return $this;
Expand All @@ -289,7 +289,7 @@ public function get(string $property): mixed

public function _get(string $property): mixed
{
return Instantiator::forceGet($this->_real(), $property);
return Instantiator::forceGet($this->_real(), $property, calledInternally: true);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ function repository(object|string $objectOrClass): RepositoryDecorator
*/
function faker(): Faker\Generator
{
return Factory::faker();
return Factory::configuration()->faker();
}

/**
Expand Down
5 changes: 3 additions & 2 deletions tests/Functional/FakerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
namespace Zenstruck\Foundry\Tests\Functional;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Zenstruck\Foundry\Factory;
use Zenstruck\Foundry\Test\Factories;

use function Zenstruck\Foundry\faker;

/**
* @author Kevin Bond <[email protected]>
*/
Expand All @@ -34,6 +35,6 @@ protected function setUp(): void
*/
public function can_use_custom_provider(): void
{
$this->assertSame('custom-value', Factory::faker()->customValue());
$this->assertSame('custom-value', faker()->customValue());
}
}
2 changes: 1 addition & 1 deletion tests/Unit/FactoryCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class FactoryCollectionTest extends TestCase
*/
public function can_create_with_static_size(): void
{
$collection = FactoryCollection::set(persistent_factory(Category::class), 2);
$collection = FactoryCollection::many(persistent_factory(Category::class), 2);

$this->assertCount(2, $collection->create());
$this->assertCount(2, $collection->create());
Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Zenstruck\Foundry\Tests\Fixtures\Entity\Post;
use Zenstruck\Foundry\Tests\Fixtures\Factories\LegacyPostFactory;

use function Zenstruck\Foundry\faker;
use function Zenstruck\Foundry\lazy;
use function Zenstruck\Foundry\Persistence\persistent_factory;
use function Zenstruck\Foundry\Persistence\proxy_factory;
Expand Down Expand Up @@ -260,10 +261,10 @@ public function can_add_after_instantiate_events(): void
*/
public function can_register_custom_faker(): void
{
$defaultFaker = Factory::faker();
$defaultFaker = faker();
Factory::configuration()->setFaker(Faker\Factory::create());

$this->assertNotSame(\spl_object_id(Factory::faker()), \spl_object_id($defaultFaker));
$this->assertNotSame(\spl_object_id(faker()), \spl_object_id($defaultFaker));
}

/**
Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/InstantiatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ public function prefixing_invalid_attribute_key_with_force_throws_exception(): v

/**
* @test
* @group legacy
*/
public function can_use_force_set_and_get(): void
{
Expand Down Expand Up @@ -457,6 +458,7 @@ public function force_set_throws_exception_for_invalid_property(): void

/**
* @test
* @group legacy
*/
public function force_get_throws_exception_for_invalid_property(): void
{
Expand Down Expand Up @@ -612,7 +614,6 @@ public function always_force_mode_can_set_parent_class_properties(): void
$this->assertSame('constructor B', $object->getPropB());
$this->assertSame('constructor C', $object->getPropC());
$this->assertSame('D', $object->getPropD());
$this->assertSame('E', Instantiator::forceGet($object, 'propE'));
}

/**
Expand Down Expand Up @@ -645,7 +646,7 @@ public function can_set_variadic_constructor_attributes(): void
/**
* @test
*/
public function missing_variadic_argument_thtrows(): void
public function missing_variadic_argument_throws(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Missing constructor argument "propB" for "Zenstruck\Foundry\Tests\Unit\VariadicInstantiatorDummy".');
Expand Down
1 change: 0 additions & 1 deletion utils/rector/.phpunit.result.cache

This file was deleted.

15 changes: 15 additions & 0 deletions utils/rector/config/foundry-set.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Renaming\Contract\MethodCallRenameInterface;
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Renaming\ValueObject\MethodCallRename;
use Rector\Transform\Rector\MethodCall\MethodCallToPropertyFetchRector;
use Rector\Transform\ValueObject\MethodCallToPropertyFetch;
use Zenstruck\Foundry\FactoryCollection;
use Zenstruck\Foundry\Utils\Rector\AddProxyToFactoryCollectionTypeInPhpDoc;
use Zenstruck\Foundry\Utils\Rector\ChangeDisableEnablePersist;
use Zenstruck\Foundry\Utils\Rector\ChangeFactoryBaseClass;
Expand All @@ -11,6 +17,7 @@
use Zenstruck\Foundry\Utils\Rector\ChangeInstantiatorMethodCalls;
use Zenstruck\Foundry\Utils\Rector\ChangeLegacyClassImports;
use Zenstruck\Foundry\Utils\Rector\ChangeProxyMethodCalls;
use Zenstruck\Foundry\Utils\Rector\ChangeStaticFactoryFakerCalls;
use Zenstruck\Foundry\Utils\Rector\PersistenceResolver;
use Zenstruck\Foundry\Utils\Rector\RemoveProxyRealObjectMethodCallsForNotProxifiedObjects;
use Zenstruck\Foundry\Utils\Rector\RuleRequirementsChecker;
Expand All @@ -34,5 +41,13 @@
ChangeFactoryMethodCalls::class,
ChangeFunctionsCalls::class,
ChangeProxyMethodCalls::class,
ChangeStaticFactoryFakerCalls::class,
]);

$rectorConfig->ruleWithConfiguration(
RenameMethodRector::class,
[
new MethodCallRename(FactoryCollection::class, 'set', 'many')
]
);
};
74 changes: 74 additions & 0 deletions utils/rector/src/ChangeStaticFactoryFakerCalls.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace Zenstruck\Foundry\Utils\Rector;

use PhpParser\Node;
use PHPStan\Analyser\MutatingScope;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Zenstruck\Foundry\Factory;

final class ChangeStaticFactoryFakerCalls extends AbstractRector
{
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Node\Expr\StaticCall::class];
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change Factory::faker() calls, outside from a factory (method is not protected.',
[
new CodeSample(
<<<'CODE_SAMPLE'
// not in a factory class
Factory::faker();
CODE_SAMPLE,
<<<'CODE_SAMPLE'
// not in a factory class
\Zenstruck\Foundry\faker();
CODE_SAMPLE
),
]
);
}

/**
* @param Node\Expr\StaticCall $node
*/
public function refactor(Node $node): Node|int|null
{
// if method name is not faker, then do nothing
if ((string)$node->name !== 'faker') {
return null;
}

// if method is not called on a Factory class, then do nothing
if (
!$node->class instanceof Node\Name
|| !is_a((string)$node->class, Factory::class, allow_string: true)
) {
return null;
}

/** @var MutatingScope $mutatingScope */
$mutatingScope = $node->getAttribute('scope');

// if the Factory::faker() was called from a Factory, then do nothing
if (
null !== ($classReflection = $mutatingScope->getClassReflection())
&& is_a($classReflection->getName(), Factory::class, allow_string: true)
) {
return null;
}

return new Node\Expr\FuncCall(new Node\Name('\Zenstruck\Foundry\faker'));
}
}
4 changes: 2 additions & 2 deletions utils/rector/tests/AllRules/Fixtures/object-factory.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class DummyObjectModelFactory extends ModelFactory
{
protected function getDefaults(): array
{
return [];
return ['field' => self::faker()];
}

public function published(): static
Expand Down Expand Up @@ -62,7 +62,7 @@ final class DummyObjectModelFactory extends \Zenstruck\Foundry\ObjectFactory
{
protected function defaults(): array
{
return [];
return ['field' => self::faker()];
}

public function published(): static
Expand Down
Loading