Skip to content
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
Binary file modified data/database.sqlite
Binary file not shown.
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,6 @@ parameters:
count: 1
path: src/Entity/Post.php

-
message: "#^Method App\\\\Entity\\\\Tag\\:\\:__toString\\(\\) should return string but returns string\\|null\\.$#"
count: 1
path: src/Entity/Tag.php

-
message: "#^Method App\\\\Entity\\\\Tag\\:\\:jsonSerialize\\(\\) should return string but returns string\\|null\\.$#"
count: 1
path: src/Entity/Tag.php

-
message: "#^Method App\\\\Entity\\\\User\\:\\:__serialize\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
Expand Down
3 changes: 1 addition & 2 deletions src/DataFixtures/AppFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ private function loadUsers(ObjectManager $manager): void
private function loadTags(ObjectManager $manager): void
{
foreach ($this->getTagData() as $name) {
$tag = new Tag();
$tag->setName($name);
$tag = new Tag($name);

$manager->persist($tag);
$this->addReference('tag-'.$name, $tag);
Expand Down
14 changes: 6 additions & 8 deletions src/Entity/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,18 @@ class Tag implements \JsonSerializable
#[ORM\Column(type: 'integer')]
private ?int $id = null;

#[ORM\Column(type: 'string', unique: true)]
private ?string $name = null;
public function __construct(
#[ORM\Column(type: 'string', unique: true)]
private readonly string $name,
) {
}

public function getId(): ?int
{
return $this->id;
}

public function setName(string $name): void
{
$this->name = $name;
}

public function getName(): ?string
public function getName(): string
{
return $this->name;
}
Expand Down
4 changes: 1 addition & 3 deletions src/Form/DataTransformer/TagArrayToStringTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ public function reverseTransform($string): array
]);
$newNames = array_diff($names, $tags);
foreach ($newNames as $name) {
$tag = new Tag();
$tag->setName($name);
$tags[] = $tag;
$tags[] = new Tag($name);

// There's no need to persist these new tags because Doctrine does that automatically
// thanks to the cascade={"persist"} option in the App\Entity\Post::$tags property.
Expand Down
19 changes: 4 additions & 15 deletions tests/Form/DataTransformer/TagArrayToStringTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public function testDuplicateNames(): void
public function testUsesAlreadyDefinedTags(): void
{
$persistedTags = [
$this->createTag('Hello'),
$this->createTag('World'),
new Tag('Hello'),
new Tag('World'),
];
$tags = $this->getMockedTransformer($persistedTags)->reverseTransform('Hello, World, How, Are, You');

Expand All @@ -89,8 +89,8 @@ public function testUsesAlreadyDefinedTags(): void
public function testTransform(): void
{
$persistedTags = [
$this->createTag('Hello'),
$this->createTag('World'),
new Tag('Hello'),
new Tag('World'),
];
$transformed = $this->getMockedTransformer()->transform($persistedTags);

Expand All @@ -114,15 +114,4 @@ private function getMockedTransformer(array $findByReturnValues = []): TagArrayT

return new TagArrayToStringTransformer($tagRepository);
}

/**
* This helper method creates a Tag instance for the given tag name.
*/
private function createTag(string $name): Tag
{
$tag = new Tag();
$tag->setName($name);

return $tag;
}
}