Skip to content

Commit

Permalink
Merge pull request #548 from nikophil/fix/deprecation-layerr
Browse files Browse the repository at this point in the history
fix(bc-layer): few improvments
  • Loading branch information
kbond authored Jan 11, 2024
2 parents 188c27a + 814507b commit f5d1c25
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 16 deletions.
2 changes: 0 additions & 2 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,6 @@ final public function many(int $min, ?int $max = null): FactoryCollection
final public function sequence(iterable|callable $sequence): FactoryCollection
{
if (\is_callable($sequence)) {
trigger_deprecation('zenstruck\foundry', '1.37.0', 'Passing a callable to method "%s()" is deprecated and will be removed in 2.0.', __METHOD__);

$sequence = $sequence();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function forceSetAll(array $properties): static;
/**
* @deprecated Use method "_get()" instead
*/
public function get(string $property): mixed;
public function forceGet(string $property): mixed;

/**
* @deprecated Use method "_repository()" instead
Expand Down
2 changes: 1 addition & 1 deletion src/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public function forceSetAll(array $properties): static
/**
* @deprecated Use method "_get()" instead
*/
public function get(string $property): mixed
public function forceGet(string $property): mixed
{
trigger_deprecation('zenstruck\foundry', '1.37.0', 'Method "%s()" is deprecated and will be removed in 2.0. Use "%s::_get()" instead.', __METHOD__, self::class);

Expand Down
1 change: 0 additions & 1 deletion tests/Functional/ModelFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ public function can_create_sequence(): void

/**
* @dataProvider sequenceProvider
* @group legacy
*/
public function can_create_sequence_with_callable(callable $sequence): void
{
Expand Down
12 changes: 11 additions & 1 deletion utils/rector/src/ChangeFactoryBaseClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Analyser\MutatingScope;
use PHPStan\PhpDocParser\Ast\PhpDoc\ExtendsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
Expand Down Expand Up @@ -191,7 +192,16 @@ public function refactor(Node $node): ?Node

private function changeBaseClass(Node\Stmt\Class_ $node): Node\Stmt\Class_|null
{
if ($this->persistenceResolver->shouldTransformFactoryIntoObjectFactory($this->getName($node))) {
/** @var MutatingScope $mutatingScope */
$mutatingScope = $node->getAttribute('scope');
$classReflection = $mutatingScope->getClassReflection();

if (
!str_starts_with($classReflection->getParentClass()->getName(), 'Zenstruck\Foundry')
|| str_starts_with($classReflection->getParentClass()->getName(), 'Zenstruck\Foundry\Utils\Rector')
) {
$newFactoryClass = $classReflection->getParentClass()->getName();
} elseif ($this->persistenceResolver->shouldTransformFactoryIntoObjectFactory($this->getName($node))) {
$newFactoryClass = '\\' . ObjectFactory::class;
$node->extends = new Node\Name($newFactoryClass);
} else {
Expand Down
31 changes: 27 additions & 4 deletions utils/rector/src/ChangeFactoryMethodCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Zenstruck\Foundry\Utils\Rector;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -24,7 +26,7 @@ public function __construct(
*/
public function getNodeTypes(): array
{
return [Node\Expr\MethodCall::class];
return [Node\Expr\MethodCall::class, Node\Expr\StaticCall::class];
}

public function getRuleDefinition(): RuleDefinition
Expand Down Expand Up @@ -74,10 +76,16 @@ public function published(): static
);
}

/**
* @param Node\Expr\MethodCall $node
*/
public function refactor(Node $node): Node|int|null
{
return match($node::class) {
Node\Expr\MethodCall::class => $this->changeMethodCall($node),
Node\Expr\StaticCall::class => $this->changeStaticCall($node),
default => null,
};
}

public function changeMethodCall(MethodCall $node): Node|int|null
{
if (!$this->isObjectType($node->var, new ObjectType(Factory::class))) {
return null;
Expand All @@ -101,4 +109,19 @@ public function refactor(Node $node): Node|int|null

return null;
}

private function changeStaticCall(StaticCall $node): Node|null
{
if (!$this->isObjectType($node->class, new ObjectType(Factory::class))) {
return null;
}

if ((string)$node->name === 'getDefaults') {
$node->name = new Node\Identifier('defaults');

return $node;
}

return null;
}
}
4 changes: 2 additions & 2 deletions utils/rector/src/ChangeProxyMethodCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function getRuleDefinition(): RuleDefinition
$proxy->remove();
$proxy->refresh();
$proxy->forceSet();
$proxy->get();
$proxy->forceGet();
$proxy->repository();
$proxy->enableAutoRefresh();
$proxy->disableAutoRefresh();
Expand Down Expand Up @@ -83,7 +83,7 @@ public function refactor(Node $node): ?Node
case 'forceSet':
$node->name = new Node\Identifier('_set');
return $node;
case 'get':
case 'forceGet':
$node->name = new Node\Identifier('_get');
return $node;
case 'repository':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private function isProxyOfFutureObjectFactory(Node\Expr $var): bool
return false;
}

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

$type = $mutatingScope->getType($var);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Zenstruck\Foundry\Utils\Rector\Tests\Fixtures;

use Zenstruck\Foundry\Utils\Rector\Tests\Fixtures\DummyObject;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;

final class DummyModelFactoryExtendingUserFactory extends DummyObjectModelFactory
{
protected function getDefaults(): array
{
return [];
}

protected function initialize(): self
{
return $this;
}

protected static function getClass(): string
{
return DummyObject::class;
}
}
?>
-----
<?php

namespace Zenstruck\Foundry\Utils\Rector\Tests\Fixtures;

use Zenstruck\Foundry\Utils\Rector\Tests\Fixtures\DummyObject;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;

final class DummyModelFactoryExtendingUserFactory extends DummyObjectModelFactory
{
protected function defaults(): array
{
return [];
}

protected function initialize(): static
{
return $this;
}

public static function class(): string
{
return DummyObject::class;
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Zenstruck\Foundry\Utils\Rector\Tests\Fixtures;

use Zenstruck\Foundry\Utils\Rector\Tests\Fixtures\DummyObject;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;

final class DummyModelFactoryExtendingUserFactory extends DummyObjectModelFactory
{
protected function getDefaults(): array
{
return self::getDefaults();
}

protected function initialize(): self
{
return $this;
}

protected static function getClass(): string
{
return DummyObject::class;
}
}
?>
-----
<?php

namespace Zenstruck\Foundry\Utils\Rector\Tests\Fixtures;

use Zenstruck\Foundry\Utils\Rector\Tests\Fixtures\DummyObject;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;

final class DummyModelFactoryExtendingUserFactory extends DummyObjectModelFactory
{
protected function getDefaults(): array
{
return self::defaults();
}

protected function initialize(): self
{
return $this;
}

protected static function getClass(): string
{
return DummyObject::class;
}
}
?>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function legacyProxy(\Zenstruck\Foundry\Proxy $proxy): void
$proxy->remove();
$proxy->refresh();
$proxy->forceSet();
$proxy->get();
$proxy->forceGet();
$proxy->repository();
$proxy->enableAutoRefresh();
$proxy->disableAutoRefresh();
Expand All @@ -23,7 +23,7 @@ function newProxy(\Zenstruck\Foundry\Persistence\Proxy $proxy): void
$proxy->remove();
$proxy->refresh();
$proxy->forceSet();
$proxy->get();
$proxy->forceGet();
$proxy->repository();
$proxy->enableAutoRefresh();
$proxy->disableAutoRefresh();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Zenstruck\Foundry\Utils\Rector\Tests\Fixtures;

final class DummyModelFactoryExtendingUserFactory extends DummyObjectModelFactory
{
protected function getDefaults(): array
{
return [];
}

protected static function getClass(): string
{
return DummyObject::class;
}
}
2 changes: 1 addition & 1 deletion utils/rector/tests/Fixtures/DummyObjectModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Zenstruck\Foundry\ModelFactory;

final class DummyObjectModelFactory extends ModelFactory
class DummyObjectModelFactory extends ModelFactory
{
protected function getDefaults(): array
{
Expand Down

0 comments on commit f5d1c25

Please sign in to comment.