-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: skip readonly properties on entities when generating factories
- Loading branch information
1 parent
d8c8c46
commit 26233a4
Showing
6 changed files
with
113 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,8 @@ | |
use Symfony\Bundle\MakerBundle\Str; | ||
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails; | ||
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor; | ||
use Symfony\Component\PropertyInfo\PropertyInfoExtractor; | ||
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface; | ||
use Symfony\Component\PropertyInfo\PropertyInitializableExtractorInterface; | ||
use Zenstruck\Foundry\ObjectFactory; | ||
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory; | ||
use Zenstruck\Foundry\Persistence\Proxy; | ||
|
@@ -31,13 +32,19 @@ final class MakeFactoryData | |
public const STATIC_ANALYSIS_TOOL_PHPSTAN = 'phpstan'; | ||
public const STATIC_ANALYSIS_TOOL_PSALM = 'psalm'; | ||
|
||
/** | ||
* @var null|PropertyAccessExtractorInterface&PropertyInitializableExtractorInterface | ||
Check failure on line 36 in src/Maker/Factory/MakeFactoryData.php
|
||
*/ | ||
private static mixed $propertyInfo = null; | ||
|
||
/** @var list<string> */ | ||
private array $uses; | ||
/** @var array<string, string> */ | ||
private array $defaultProperties = []; | ||
/** @var list<MakeFactoryPHPDocMethod> */ | ||
private array $methodsInPHPDoc; | ||
private PropertyInfoExtractor $propertyInfo; | ||
/** @var string[]|true */ | ||
private array|bool $forceProperties; | ||
|
||
public function __construct( | ||
private \ReflectionClass $object, | ||
|
@@ -46,6 +53,7 @@ public function __construct( | |
private string $staticAnalysisTool, | ||
private bool $persisted, | ||
bool $withPhpDoc, | ||
array|bool $forceProperties | ||
) { | ||
$this->uses = [ | ||
$this->getFactoryClass(), | ||
|
@@ -65,14 +73,7 @@ public function __construct( | |
} | ||
|
||
$this->methodsInPHPDoc = $withPhpDoc ? MakeFactoryPHPDocMethod::createAll($this) : []; | ||
$reflectionExtractor = new ReflectionExtractor(); | ||
$this->propertyInfo = new PropertyInfoExtractor( | ||
[], | ||
[], | ||
[], | ||
[$reflectionExtractor], | ||
[$reflectionExtractor] | ||
); | ||
$this->forceProperties = $forceProperties; | ||
} | ||
|
||
// @phpstan-ignore-next-line | ||
|
@@ -167,12 +168,17 @@ public function getDefaultProperties(): array | |
$defaultProperties = $this->defaultProperties; | ||
$clazz = $this->object->getName(); | ||
|
||
foreach ($defaultProperties as $propertyName => $propertyDefault) { | ||
if ($this->propertyInfo->isWritable($clazz, $propertyName) || $this->propertyInfo->isInitializable($clazz, $propertyName)) { | ||
continue; | ||
$defaultProperties = array_filter($defaultProperties, function (string $propertyName) use ($clazz): bool { | ||
if (true === $this->forceProperties) { | ||
return true; | ||
} | ||
unset($defaultProperties[$propertyName]); | ||
} | ||
|
||
if (is_array($this->forceProperties) && \in_array($propertyName, $this->forceProperties, true)) { | ||
return true; | ||
} | ||
|
||
return self::propertyInfo()->isWritable($clazz, $propertyName) || self::propertyInfo()->isInitializable($clazz, $propertyName); | ||
}, ARRAY_FILTER_USE_KEY); | ||
|
||
\ksort($defaultProperties); | ||
|
||
|
@@ -209,4 +215,9 @@ public function addEnumDefaultProperty(string $propertyName, string $enumClass): | |
"self::faker()->randomElement({$enumShortClassName}::cases()),", | ||
); | ||
} | ||
|
||
private static function propertyInfo(): PropertyAccessExtractorInterface&PropertyInitializableExtractorInterface | ||
{ | ||
return self::$propertyInfo ??= new ReflectionExtractor(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
tests/Fixture/Maker/expected/does_force_initialization_of_non_settable_with_always_force.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Factory; | ||
|
||
use Zenstruck\Foundry\ObjectFactory; | ||
use Zenstruck\Foundry\Tests\Fixture\ObjectWithNonWriteable; | ||
|
||
/** | ||
* @extends ObjectFactory<ObjectWithNonWriteable> | ||
*/ | ||
final class ObjectWithNonWriteableFactory extends ObjectFactory | ||
{ | ||
/** | ||
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services | ||
* | ||
* @todo inject services if required | ||
*/ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
public static function class(): string | ||
{ | ||
return ObjectWithNonWriteable::class; | ||
} | ||
|
||
/** | ||
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories | ||
* | ||
* @todo add your default values here | ||
*/ | ||
protected function defaults(): array|callable | ||
{ | ||
return [ | ||
'baz' => self::faker()->sentence(), | ||
'bar' => self::faker()->sentence(), | ||
'foo' => self::faker()->sentence(), | ||
]; | ||
} | ||
|
||
/** | ||
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization | ||
*/ | ||
protected function initialize(): static | ||
{ | ||
return $this | ||
// ->afterInstantiate(function(ObjectWithNonWriteable $objectWithNonWriteable): void {}) | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters