Skip to content

Commit c512f19

Browse files
committedOct 28, 2022
minor #1357 Remove Tag::setName (chr-hertel)
This PR was merged into the main branch. Discussion ---------- Remove Tag::setName In this case we can move it to constructor Commits ------- ced9eb2 remove Tag::setName
2 parents 539cbc0 + ced9eb2 commit c512f19

File tree

6 files changed

+12
-38
lines changed

6 files changed

+12
-38
lines changed
 

‎data/database.sqlite

0 Bytes
Binary file not shown.

‎phpstan-baseline.neon

-10
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,6 @@ parameters:
185185
count: 1
186186
path: src/Entity/Post.php
187187

188-
-
189-
message: "#^Method App\\\\Entity\\\\Tag\\:\\:__toString\\(\\) should return string but returns string\\|null\\.$#"
190-
count: 1
191-
path: src/Entity/Tag.php
192-
193-
-
194-
message: "#^Method App\\\\Entity\\\\Tag\\:\\:jsonSerialize\\(\\) should return string but returns string\\|null\\.$#"
195-
count: 1
196-
path: src/Entity/Tag.php
197-
198188
-
199189
message: "#^Method App\\\\Entity\\\\User\\:\\:__serialize\\(\\) return type has no value type specified in iterable type array\\.$#"
200190
count: 1

‎src/DataFixtures/AppFixtures.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ private function loadUsers(ObjectManager $manager): void
5656
private function loadTags(ObjectManager $manager): void
5757
{
5858
foreach ($this->getTagData() as $name) {
59-
$tag = new Tag();
60-
$tag->setName($name);
59+
$tag = new Tag($name);
6160

6261
$manager->persist($tag);
6362
$this->addReference('tag-'.$name, $tag);

‎src/Entity/Tag.php

+6-8
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,18 @@ class Tag implements \JsonSerializable
2929
#[ORM\Column(type: 'integer')]
3030
private ?int $id = null;
3131

32-
#[ORM\Column(type: 'string', unique: true)]
33-
private ?string $name = null;
32+
public function __construct(
33+
#[ORM\Column(type: 'string', unique: true)]
34+
private readonly string $name,
35+
) {
36+
}
3437

3538
public function getId(): ?int
3639
{
3740
return $this->id;
3841
}
3942

40-
public function setName(string $name): void
41-
{
42-
$this->name = $name;
43-
}
44-
45-
public function getName(): ?string
43+
public function getName(): string
4644
{
4745
return $this->name;
4846
}

‎src/Form/DataTransformer/TagArrayToStringTransformer.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ public function reverseTransform($string): array
6262
]);
6363
$newNames = array_diff($names, $tags);
6464
foreach ($newNames as $name) {
65-
$tag = new Tag();
66-
$tag->setName($name);
67-
$tags[] = $tag;
65+
$tags[] = new Tag($name);
6866

6967
// There's no need to persist these new tags because Doctrine does that automatically
7068
// thanks to the cascade={"persist"} option in the App\Entity\Post::$tags property.

‎tests/Form/DataTransformer/TagArrayToStringTransformerTest.php

+4-15
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public function testDuplicateNames(): void
7272
public function testUsesAlreadyDefinedTags(): void
7373
{
7474
$persistedTags = [
75-
$this->createTag('Hello'),
76-
$this->createTag('World'),
75+
new Tag('Hello'),
76+
new Tag('World'),
7777
];
7878
$tags = $this->getMockedTransformer($persistedTags)->reverseTransform('Hello, World, How, Are, You');
7979

@@ -89,8 +89,8 @@ public function testUsesAlreadyDefinedTags(): void
8989
public function testTransform(): void
9090
{
9191
$persistedTags = [
92-
$this->createTag('Hello'),
93-
$this->createTag('World'),
92+
new Tag('Hello'),
93+
new Tag('World'),
9494
];
9595
$transformed = $this->getMockedTransformer()->transform($persistedTags);
9696

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

115115
return new TagArrayToStringTransformer($tagRepository);
116116
}
117-
118-
/**
119-
* This helper method creates a Tag instance for the given tag name.
120-
*/
121-
private function createTag(string $name): Tag
122-
{
123-
$tag = new Tag();
124-
$tag->setName($name);
125-
126-
return $tag;
127-
}
128117
}

0 commit comments

Comments
 (0)
Please sign in to comment.