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

Enhancement: Extract named constructor #1380

Merged
merged 1 commit into from
May 5, 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
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ parameters:
path: test/Unit/EntityDefinitionTest.php

-
message: "#^Parameter \\#2 \\$fieldDefinitions of class Ergebnis\\\\FactoryBot\\\\EntityDefinition constructor expects array\\<string, Ergebnis\\\\FactoryBot\\\\FieldDefinition\\\\Resolvable\\>, array\\<string, mixed\\> given\\.$#"
message: "#^Parameter \\#2 \\$fieldDefinitions of static method Ergebnis\\\\FactoryBot\\\\EntityDefinition\\:\\:create\\(\\) expects array\\<string, Ergebnis\\\\FactoryBot\\\\FieldDefinition\\\\Resolvable\\>, array\\<string, mixed\\> given\\.$#"
count: 1
path: test/Unit/EntityDefinitionTest.php

Expand Down
6 changes: 6 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@
<code><![CDATA[Entity\User]]></code>
</UnnecessaryVarAnnotation>
</file>
<file src="src/EntityDefinition.php">
<MixedReturnTypeCoercion>
<code><![CDATA[$this->fieldDefinitions]]></code>
<code><![CDATA[array<string, FieldDefinition\Resolvable>]]></code>
</MixedReturnTypeCoercion>
</file>
<file src="src/FixtureFactory.php">
<MissingClosureReturnType>
<code><![CDATA[function (FieldDefinition\Resolvable $fieldDefinition) {]]></code>
Expand Down
23 changes: 18 additions & 5 deletions src/EntityDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,36 @@
*/
final class EntityDefinition
{
private function __construct(
private ORM\Mapping\ClassMetadata $classMetadata,
private array $fieldDefinitions,
private \Closure $afterCreate,
) {
}

/**
* @param array<string, FieldDefinition\Resolvable> $fieldDefinitions
*
* @throws Exception\InvalidFieldDefinitions
*/
public function __construct(
private ORM\Mapping\ClassMetadata $classMetadata,
private array $fieldDefinitions,
private \Closure $afterCreate,
) {
public static function create(
ORM\Mapping\ClassMetadata $classMetadata,
array $fieldDefinitions,
\Closure $afterCreate,
): self {
$invalidFieldDefinitions = \array_filter($fieldDefinitions, static function ($fieldDefinition): bool {
return !$fieldDefinition instanceof FieldDefinition\Resolvable;
});

if ([] !== $invalidFieldDefinitions) {
throw Exception\InvalidFieldDefinitions::values();
}

return new self(
$classMetadata,
$fieldDefinitions,
$afterCreate,
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/FixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
};
}

$this->entityDefinitions[$className] = new EntityDefinition(
$this->entityDefinitions[$className] = EntityDefinition::create(
$classMetadata,
$fieldDefinitions,
$afterCreate,
Expand Down Expand Up @@ -454,7 +454,7 @@
$inversedBy,
);

if (!$collection instanceof Common\Collections\Collection) {

Check warning on line 457 in src/FixtureFactory.php

View workflow job for this annotation

GitHub Actions / Mutation Tests (8.1, locked)

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ } $classMetadataOfFieldValue = $this->entityManager->getClassMetadata($fieldValue::class); $collection = $classMetadataOfFieldValue->getFieldValue($fieldValue, $inversedBy); - if (!$collection instanceof Common\Collections\Collection) { + if (!true) { return; } $collection->add($entity);
return;
}

Expand Down
8 changes: 4 additions & 4 deletions test/Unit/EntityDefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class EntityDefinitionTest extends Framework\TestCase
use Test\Util\Helper;

#[Framework\Attributes\DataProviderExternal(Test\DataProvider\ValueProvider::class, 'arbitrary')]
public function testConstructorRejectsFieldDefinitionsWhenValuesAreNotFieldDefinitions(mixed $fieldDefinition): void
public function testCreateRejectsFieldDefinitionsWhenValuesAreNotFieldDefinitions(mixed $fieldDefinition): void
{
$fieldDefinitions = [
'foo' => FieldDefinition::value('bar'),
Expand All @@ -38,7 +38,7 @@ public function testConstructorRejectsFieldDefinitionsWhenValuesAreNotFieldDefin

$this->expectException(Exception\InvalidFieldDefinitions::class);

new EntityDefinition(
EntityDefinition::create(
$this->createMock(ORM\Mapping\ClassMetadata::class),
$fieldDefinitions,
static function ($entity, array $fieldValues): void {
Expand All @@ -47,7 +47,7 @@ static function ($entity, array $fieldValues): void {
);
}

public function testConstructorSetsValues(): void
public function testCreateReturnsEntityDefinition(): void
{
$classMetadata = $this->createMock(ORM\Mapping\ClassMetadata::class);

Expand All @@ -60,7 +60,7 @@ public function testConstructorSetsValues(): void
// intentionally left blank
};

$entityDefinition = new EntityDefinition(
$entityDefinition = EntityDefinition::create(
$classMetadata,
$fieldDefinitions,
$afterCreate,
Expand Down
Loading