Skip to content

Commit 6536a58

Browse files
committed
bug #805 Enhancement: Fullfill interface and move comments to the method description (OskarStark)
This PR was merged into the 1.0-dev branch. Discussion ---------- Enhancement: Fullfill interface and move comments to the method description Before this fix, static analyzers are complaining: ``` Return type (void) of method App\Entity\User::getSalt() should be compatible with return type (string|null) of method Symfony\Component\Security\Core\User\UserInterface::getSalt() ``` cc @chalasr Commits ------- e7957ba Enhancement: Fullfill interface
2 parents bc7ccf1 + e7957ba commit 6536a58

8 files changed

+81
-39
lines changed

src/Security/UserClassBuilder.php

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,20 @@ private function addGetPassword(ClassSourceManipulator $manipulator, UserClassCo
165165
// add an empty method only
166166
$builder = $manipulator->createMethodBuilder(
167167
'getPassword',
168-
null,
169-
false,
170-
['@see UserInterface']
168+
'string',
169+
true,
170+
[
171+
'This method is not needed for apps that do not check user passwords.',
172+
'',
173+
'@see UserInterface',
174+
]
171175
);
176+
172177
$builder->addStmt(
173-
$manipulator->createMethodLevelCommentNode(
174-
'not needed for apps that do not check user passwords'
178+
new Node\Stmt\Return_(
179+
new Node\Expr\ConstFetch(
180+
new Node\Name('null')
181+
)
175182
)
176183
);
177184

@@ -222,26 +229,38 @@ private function addGetPassword(ClassSourceManipulator $manipulator, UserClassCo
222229

223230
private function addGetSalt(ClassSourceManipulator $manipulator, UserClassConfiguration $userClassConfig)
224231
{
225-
// add getSalt(): always empty
232+
if ($userClassConfig->hasPassword()) {
233+
$methodDescription = [
234+
'Returning a salt is only needed, if you are not using a modern',
235+
'hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.',
236+
];
237+
} else {
238+
$methodDescription = [
239+
'This method is not needed for apps that do not check user passwords.',
240+
];
241+
}
242+
243+
// add getSalt(): ?string - always returning null
226244
$builder = $manipulator->createMethodBuilder(
227245
'getSalt',
228-
null,
229-
false,
230-
['@see UserInterface']
246+
'string',
247+
true,
248+
array_merge(
249+
$methodDescription,
250+
[
251+
'',
252+
'@see UserInterface',
253+
]
254+
)
231255
);
232-
if ($userClassConfig->hasPassword()) {
233-
$builder->addStmt(
234-
$manipulator->createMethodLevelCommentNode(
235-
'not needed when using the "bcrypt" algorithm in security.yaml'
236-
)
237-
);
238-
} else {
239-
$builder->addStmt(
240-
$manipulator->createMethodLevelCommentNode(
241-
'not needed for apps that do not check user passwords'
256+
257+
$builder->addStmt(
258+
new Node\Stmt\Return_(
259+
new Node\Expr\ConstFetch(
260+
new Node\Name('null')
242261
)
243-
);
244-
}
262+
)
263+
);
245264

246265
$manipulator->addMethodBuilder($builder);
247266
}

tests/Security/fixtures/expected/UserEntityEmailWithPassword.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,14 @@ public function setPassword(string $password): self
9595
}
9696

9797
/**
98+
* Returning a salt is only needed, if you are not using a modern
99+
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
100+
*
98101
* @see UserInterface
99102
*/
100-
public function getSalt()
103+
public function getSalt(): ?string
101104
{
102-
// not needed when using the "bcrypt" algorithm in security.yaml
105+
return null;
103106
}
104107

105108
/**

tests/Security/fixtures/expected/UserEntityUser_nameWithPassword.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,14 @@ public function setPassword(string $password): self
9090
}
9191

9292
/**
93+
* Returning a salt is only needed, if you are not using a modern
94+
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
95+
*
9396
* @see UserInterface
9497
*/
95-
public function getSalt()
98+
public function getSalt(): ?string
9699
{
97-
// not needed when using the "bcrypt" algorithm in security.yaml
100+
return null;
98101
}
99102

100103
/**

tests/Security/fixtures/expected/UserEntityUsernameNoPassword.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,23 @@ public function setRoles(array $roles): self
6969
}
7070

7171
/**
72+
* This method is not needed for apps that do not check user passwords.
73+
*
7274
* @see UserInterface
7375
*/
74-
public function getPassword()
76+
public function getPassword(): ?string
7577
{
76-
// not needed for apps that do not check user passwords
78+
return null;
7779
}
7880

7981
/**
82+
* This method is not needed for apps that do not check user passwords.
83+
*
8084
* @see UserInterface
8185
*/
82-
public function getSalt()
86+
public function getSalt(): ?string
8387
{
84-
// not needed for apps that do not check user passwords
88+
return null;
8589
}
8690

8791
/**

tests/Security/fixtures/expected/UserEntityUsernameWithPassword.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,14 @@ public function setPassword(string $password): self
9090
}
9191

9292
/**
93+
* Returning a salt is only needed, if you are not using a modern
94+
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
95+
*
9396
* @see UserInterface
9497
*/
95-
public function getSalt()
98+
public function getSalt(): ?string
9699
{
97-
// not needed when using the "bcrypt" algorithm in security.yaml
100+
return null;
98101
}
99102

100103
/**

tests/Security/fixtures/expected/UserModelEmailWithPassword.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,14 @@ public function setPassword(string $password): self
7272
}
7373

7474
/**
75+
* Returning a salt is only needed, if you are not using a modern
76+
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
77+
*
7578
* @see UserInterface
7679
*/
77-
public function getSalt()
80+
public function getSalt(): ?string
7881
{
79-
// not needed when using the "bcrypt" algorithm in security.yaml
82+
return null;
8083
}
8184

8285
/**

tests/Security/fixtures/expected/UserModelUsernameNoPassword.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,23 @@ public function setRoles(array $roles): self
4747
}
4848

4949
/**
50+
* This method is not needed for apps that do not check user passwords.
51+
*
5052
* @see UserInterface
5153
*/
52-
public function getPassword()
54+
public function getPassword(): ?string
5355
{
54-
// not needed for apps that do not check user passwords
56+
return null;
5557
}
5658

5759
/**
60+
* This method is not needed for apps that do not check user passwords.
61+
*
5862
* @see UserInterface
5963
*/
60-
public function getSalt()
64+
public function getSalt(): ?string
6165
{
62-
// not needed for apps that do not check user passwords
66+
return null;
6367
}
6468

6569
/**

tests/Security/fixtures/expected/UserModelUsernameWithPassword.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,14 @@ public function setPassword(string $password): self
6767
}
6868

6969
/**
70+
* Returning a salt is only needed, if you are not using a modern
71+
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
72+
*
7073
* @see UserInterface
7174
*/
72-
public function getSalt()
75+
public function getSalt(): ?string
7376
{
74-
// not needed when using the "bcrypt" algorithm in security.yaml
77+
return null;
7578
}
7679

7780
/**

0 commit comments

Comments
 (0)