8000 correct cs · symfony/symfony@5a4fbe1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5a4fbe1

Browse files
committed
correct cs
voter has vote and getvote tests correction update
1 parent 3dcdb19 commit 5a4fbe1

37 files changed

+973
-241
lines changed

src/Symfony/Component/Security/Core/Authorization/AccessDecision.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public function isDenied(): bool
5151
return VoterInterface::ACCESS_DENIED === $this->access;
5252
}
5353

54-
5554
/**
5655
* @return string[]
5756
*/
@@ -99,4 +98,4 @@ private function getVotesByAccess(int $access): array
9998
{
10099
return array_filter($this->votes, static fn (Vote $vote): bool => $vote->getAccess() === $access);
101100
}
102-
}
101+
}

src/Symfony/Component/Security/Core/Authorization/AccessDecisionManager.php

Lines changed: 5 additions & 6 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function getDecision(TokenInterface $token, array $attributes, mixed $obj
4545
{
4646
// Special case for AccessListener, do not remove the right side of the condition before 6.0
4747
if (\count($attributes) > 1 && !$allowMultipleAttributes) {
48-
throw new InvalidArgumentException(sprintf('Passing more than one Security attribute to "%s()" is not supported.', __METHOD__));
48+
throw new InvalidArgumentException(\sprintf('Passing more than one Security attribute to "%s()" is not supported.', __METHOD__));
4949
}
5050

5151
if (method_exists($this->strategy, 'getDecision')) {
@@ -83,12 +83,11 @@ public function decide(TokenInterface $token, array $attributes, mixed $object =
8383
private function collectVotes(TokenInterface $token, array $attributes, mixed $object): \Traversable
8484
{
8585
foreach ($this->getVoters($attributes, $object) as $voter) {
86-
$vote = $voter->vote($token, $object, $attributes);
87-
if (! $vote instanceOf Vote)
88-
{
89-
$vote = new Vote($vote);
86+
if(method_exists($voter, 'getVote')) {
87+
yield $voter->getVote($token, $object, $attributes);
88+
} else {
89+
yield new Vote($voter->vote($token, $object, $attributes));
9090
}
91-
yield $vote;
9291
}
9392
}
9493

src/Symfony/Component/Security/Core/Authorization/AuthorizationCheckerInterface.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\Security\Core\Authorization;
1313

14-
use Symfony\Component\Security\Core\Authorization\AccessDecision;
1514
/**
1615
* The AuthorizationCheckerInterface.
1716
*

src/Symfony/Component/Security/Core/Authorization/Strategy/AffirmativeStrategy.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
namespace Symfony\Component\Security\Core\Authorization\Strategy;
1313

14+
use Symfony\Component\Security\Core\Authorization\AccessDecision;
1415
use Symfony\Component\Security\Core\Authorization\Voter\Vote;
1516
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
16-
use Symfony\Component\Security\Core\Authorization\AccessDecision;
1717

1818
/**
1919
* Grants access if any voter returns an affirmative response.
@@ -33,7 +33,7 @@ public function __construct(
3333

3434
public function decide(\Traversable $results): bool
3535
{
36-
return $this->getDecision(new \ArrayIterator(array_map(fn($vote) => new Vote($vote), iterator_to_array($results))))->isGranted();
36+
return $this->getDecision(new \ArrayIterator(array_map(fn ($vote) => new Vote($vote), iterator_to_array($results))))->isGranted();
3737
}
3838

3939
public function getDecision(\Traversable $votes): AccessDecision
@@ -46,7 +46,7 @@ public function getDecision(\Traversable $votes): AccessDecision
4646
$currentVotes[] = $vote;
4747

4848
if ($vote->isGranted()) {
49-
return new AccessDecision(VoterInterface::ACCESS_GRANTED,$currentVotes);
49+
return new AccessDecision(VoterInterface::ACCESS_GRANTED, $currentVotes);
5050
}
5151

5252
if ($vote->isDenied()) {
@@ -55,10 +55,10 @@ public function getDecision(\Traversable $votes): AccessDecision
5555
}
5656

5757
if ($deny > 0) {
58-
return new AccessDecision(VoterInterface::ACCESS_DENIED,$currentVotes);
58+
return new AccessDecision(VoterInterface::ACCESS_DENIED, $currentVotes);
5959
}
6060

61-
return new AccessDecision($this->allowIfAllAbstainDecisions ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_DENIED,$currentVotes);
61+
return new AccessDecision($this->allowIfAllAbstainDecisions ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_DENIED, $currentVotes);
6262
}
6363

6464
public function __toString(): string

src/Symfony/Component/Security/Core/Authorization/Strategy/ConsensusStrategy.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __construct(
4242

4343
public function decide(\Traversable $results): bool
4444
{
45-
return $this->getDecision(new \ArrayIterator(array_map(fn($vote) => new Vote($vote), iterator_to_array($results))))->isGranted();
45+
return $this->getDecision(new \ArrayIterator(array_map(fn ($vote) => new Vote($vote), iterator_to_array($results))))->isGranted();
4646
}
4747

4848
public function getDecision(\Traversable $votes): AccessDecision
@@ -62,18 +62,18 @@ public function getDecision(\Traversable $votes): AccessDecision
6262
}
6363

6464
if ($grant > $deny) {
65-
return new AccessDecision(VoterInterface::ACCESS_GRANTED,$currentVotes);
65+
return new AccessDecision(VoterInterface::ACCESS_GRANTED, $currentVotes);
6666
}
6767

6868
if ($deny > $grant) {
69-
return new AccessDecision(VoterInterface::ACCESS_DENIED,$currentVotes);
69+
return new AccessDecision(VoterInterface::ACCESS_DENIED, $currentVotes);
7070
}
7171

7272
if ($grant > 0) {
73-
return new AccessDecision($this->allowIfEqualGrantedDeniedDecisions ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_DENIED,$currentVotes);
73+
return new AccessDecision($this->allowIfEqualGrantedDeniedDecisions ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_DENIED, $currentVotes);
7474
}
7575

76-
return new AccessDecision($this->allowIfAllAbstainDecisions ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_DENIED,$currentVotes);
76+
return new AccessDecision($this->allowIfAllAbstainDecisions ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_DENIED, $currentVotes);
7777
}
7878

7979
public function __toString(): string

src/Symfony/Component/Security/Core/Authorization/Strategy/PriorityStrategy.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct(
3434

3535
public function decide(\Traversable $results): bool
3636
{
37-
return $this->getDecision(new \ArrayIterator(array_map(fn($vote) => new Vote($vote), iterator_to_array($results))))->isGranted();
37+
return $this->getDecision(new \ArrayIterator(array_map(fn ($vote) => new Vote($vote), iterator_to_array($results))))->isGranted();
3838
}
3939

4040
public function getDecision(\Traversable $votes): AccessDecision
@@ -45,15 +45,15 @@ public function getDecision(\Traversable $votes): AccessDecision
4545
foreach ($votes as $vote) {
4646
$currentVotes[] = $vote;
4747
if ($vote->isGranted()) {
48-
return new AccessDecision(VoterInterface::ACCESS_GRANTED,$currentVotes);
48+
return new AccessDecision(VoterInterface::ACCESS_GRANTED, $currentVotes);
4949
}
5050

5151
if ($vote->isDenied()) {
52-
return new AccessDecision(VoterInterface::ACCESS_DENIED,$currentVotes);
52+
return new AccessDecision(VoterInterface::ACCESS_DENIED, $currentVotes);
5353
}
5454
}
5555

56-
return new AccessDecision($this->allowIfAllAbstainDecisions ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_DENIED,$currentVotes);
56+
return new AccessDecision($this->allowIfAllAbstainDecisions ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_DENIED, $currentVotes);
5757
}
5858

5959
public function __toString(): string

src/Symfony/Component/Security/Core/Authorization/Strategy/ScoringStrategy.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
1717

1818
/**
19-
* Grants access if vote results greated than 0
19+
* Grants access if vote results greated than 0.
2020
*
2121
* If all voters abstained from voting, the decision will be based on the
2222
* allowIfAllAbstainDecisions property value (defaults to false).
@@ -32,7 +32,7 @@ public function __construct(
3232

3333
public function decide(\Traversable $results): bool
3434
{
35-
return $this->getDecision(new \ArrayIterator(array_map(fn($vote) => new Vote($vote), iterator_to_array($results))))->isGranted();
35+
return $this->getDecision(new \ArrayIterator(array_map(fn ($vote) => new Vote($vote), iterator_to_array($results))))->isGranted();
3636
}
3737

3838
public function getDecision(\Traversable $votes): AccessDecision
@@ -47,14 +47,14 @@ public function getDecision(\Traversable $votes): AccessDecision
4747
}
4848

4949
if ($score > 0) {
50-
return new AccessDecision(VoterInterface::ACCESS_GRANTED,$currentVotes, 'score = ' . $score);
50+
return new AccessDecision(VoterInterface::ACCESS_GRANTED, $currentVotes, 'score = '.$score);
5151
}
5252

5353
if ($score < 0) {
54-
return new AccessDecision(VoterInterface::ACCESS_DENIED,$currentVotes, 'score = ' . $score);
54+
return new AccessDecision(VoterInterface::ACCESS_DENIED, $currentVotes, 'score = '.$score);
5555
}
5656

57-
return new AccessDecision($this->allowIfAllAbstainDecisions ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_DENIED,$currentVotes);
57+
return new AccessDecision($this->allowIfAllAbstainDecisions ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_DENIED, $currentVotes);
5858
}
5959

6060
public function __toString(): string

src/Symfony/Component/Security/Core/Authorization/Strategy/UnanimousStrategy.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(
3333

3434
public function decide(\Traversable $results): bool
3535
{
36-
return $this->getDecision(new \ArrayIterator(array_map(fn($vote) => new Vote($vote), iterator_to_array($results))))->isGranted();
36+
return $this->getDecision(new \ArrayIterator(array_map(fn ($vote) => new Vote($vote), iterator_to_array($results))))->isGranted();
3737
}
3838

3939
public function getDecision(\Traversable $votes): AccessDecision
@@ -46,7 +46,7 @@ public function getDecision(\Traversable $votes): AccessDecision
4646
$currentVotes[] = $vote;
4747

4848
if ($vote->isDenied()) {
49-
return new AccessDecision(VoterInterface::ACCESS_DENIED,$currentVotes);
49+
return new AccessDecision(VoterInterface::ACCESS_DENIED, $currentVotes);
5050
}
5151

5252
if ($vote->isGranted()) {
@@ -56,10 +56,10 @@ public function getDecision(\Traversable $votes): AccessDecision
5656

5757
// no deny votes
5858
if ($grant > 0) {
59-
return new AccessDecision(VoterInterface::ACCESS_GRANTED,$currentVotes);
59+
return new AccessDecision(VoterInterface::ACCESS_GRANTED, $currentVotes);
6060
}
6161

62-
return new AccessDecision($this->allowIfAllAbstainDecisions ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_DENIED,$currentVotes);
62+
return new AccessDecision($this->allowIfAllAbstainDecisions ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_DENIED, $currentVotes);
6363
}
6464

6565
public function __toString(): string

src/Symfony/Component/Security/Core/Authorization/TraceableAccessDecisionManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ public function decide(TokenInterface $token, array $attributes, mixed $object =
8888
/**
8989
* Adds voter vote and class to the voter details.
9090
*
91-
* @param array $attributes attributes used for the vote
92-
* @param VoteInterface|int $vote vote of the voter
91+
* @param array $attributes attributes used for the vote
92+
* @param VoteInterface|int $vote vote of the voter
9393
*/
9494
public function addVoterVote(VoterInterface $voter, array $attributes, VoteInterface|int $vote): void
9595
{

src/Symfony/Component/Security/Core/Authorization/Voter/AuthenticatedVoter.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,17 @@ public function __construct(
3939
}
4040

4141
public function vote(TokenInterface $token, mixed $subject, array $attributes): int
42+
{
43+
return $this->getVote($token, $subject, $attributes)->getAccess();
44+
}
45+
46+
public function getVote(TokenInterface $token, mixed $subject, array $attributes): VoteInterface
4247
{
4348
if ($attributes === [self::PUBLIC_ACCESS]) {
44-
return VoterInterface::ACCESS_GRANTED;
49+
return new Vote(VoterInterface::ACCESS_GRANTED);
4550
}
4651

47-
$result = VoterInterface::ACCESS_ABSTAIN;
52+
$result = new Vote(VoterInterface::ACCESS_ABSTAIN);
4853
foreach ($attributes as $attribute) {
4954
if (null === $attribute || (self::IS_AUTHENTICATED_FULLY !== $attribute
5055
&& self::IS_AUTHENTICATED_REMEMBERED !== $attribute
@@ -54,29 +59,29 @@ public function vote(TokenInterface $token, mixed $subject, array $attributes):
5459
continue;
5560
}
5661

57-
$result = VoterInterface::ACCESS_DENIED;
62+
$result = new Vote(VoterInterface::ACCESS_DENIED);
5863

5964
if (self::IS_AUTHENTICATED_FULLY === $attribute
6065
&& $this->authenticationTrustResolver->isFullFledged($token)) {
61-
return VoterInterface::ACCESS_GRANTED;
66+
return new Vote(VoterInterface::ACCESS_GRANTED);
6267
}
6368

6469
if (self::IS_AUTHENTICATED_REMEMBERED === $attribute
6570
&& ($this->authenticationTrustResolver->isRememberMe($token)
6671
|| $this->authenticationTrustResolver->isFullFledged($token))) {
67-
return VoterInterface::ACCESS_GRANTED;
72+
return new Vote(VoterInterface::ACCESS_GRANTED);
6873
}
6974

7075
if (self::IS_AUTHENTICATED === $attribute && $this->authenticationTrustResolver->isAuthenticated($token)) {
71-
return VoterInterface::ACCESS_GRANTED;
76+
return new Vote(VoterInterface::ACCESS_GRANTED);
7277
}
7378

7479
if (self::IS_REMEMBERED === $attribute && $this->authenticationTrustResolver->isRememberMe($token)) {
75-
return VoterInterface::ACCESS_GRANTED;
80+
return new Vote(VoterInterface::ACCESS_GRANTED);
7681
}
7782

7883
if (self::IS_IMPERSONATOR === $attribute && $token instanceof SwitchUserToken) {
79-
return VoterInterface::ACCESS_GRANTED;
84+
return new Vote(VoterInterface::ACCESS_GRANTED);
8085
}
8186
}
8287

src/Symfony/Component/Security/Core/Authorization/Voter/ExpressionVoter.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ public function supportsType(string $subjectType): bool
4444
return true;
4545
}
4646

47-
public function vote(TokenInterface $token, mixed $subject, array $attributes): VoteInterface
47+
public function vote(TokenInterface $token, mixed $subject, array $attributes): int
48+
{
49+
return $this->getVote($token, $subject, $attributes)->getAccess();
50+
}
51+
52+
public function getVote(TokenInterface $token, mixed $subject, array $attributes): VoteInterface
4853
{
4954
$result = new Vote(VoterInterface::ACCESS_ABSTAIN);
5055
$variables = null;

src/Symfony/Component/Security/Core/Authorization/Voter/RoleVoter.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ public function __construct(
2525
) {
2626
}
2727

28-
public function vote(TokenInterface $token, mixed $subject, array $attributes): Vote
28+
public function vote(TokenInterface $token, mixed $subject, array $attributes): int
29+
{
30+
return $this->getVote($token, $subject, $attributes)->getAccess();
31+
}
32+
33+
public function getVote(TokenInterface $token, mixed $subject, array $attributes): VoteInterface
2934
{
3035
$result = new Vote(VoterInterface::ACCESS_ABSTAIN);
3136
$roles = $this->extractRoles($token);

src/Symfony/Component/Security/Core/Authorization/Voter/TraceableVoter.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,17 @@ public function __construct(
3030
) {
3131
}
3232

33-
public function vote(TokenInterface $token, mixed $subject, array $attributes): Vote
33+
public function vote(TokenInterface $token, mixed $subject, array $attributes): int
3434
{
35-
$vote = $this->voter->vote($token, $subject, $attributes);
35+
return $this->getVote($token, $subject, $attributes)->getAccess();
36+
}
3637

37-
if(!$vote instanceof Vote) {
38-
$vote = new Vote($vote);
38+
public function getVote(TokenInterface $token, mixed $subject, array $attributes): VoteInterface
39+
{
40+
if(method_exists($this->voter, 'getVote')) {
41+
$vote = $this->voter->getVote($token, $subject, $attributes);
42+
} else {
43+
$vote = new Vote($this->voter->vote($token, $subject, $attributes));
3944
}
4045

4146
$this->eventDispatcher->dispatch(new VoteEvent($this->voter, $subject, $attributes, $vote), 'debug.security.authorization.vote');

src/Symfony/Component/Security/Core/Authorization/Voter/Vote.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ class Vote implements VoteInterface
3333
*/
3434
public function __construct(private int $access, string|array $messages = [], private array $context = [], private $scoring = false)
3535
{
36-
if(!$scoring && !in_array($access, [VoterInterface::ACCESS_GRANTED, VoterInterface::ACCESS_ABSTAIN, VoterInterface::ACCESS_DENIED], true)) {
37-
throw new \LogicException(\sprintf('"$access" must return one of "%s" constants ("ACCESS_GRANTED", "ACCESS_DENIED" or "ACCESS_ABSTAIN") when "$scoring" is false, "%s" returned.', VoterInterface::class, $access));
36+
if (!$scoring && !\in_array($access, [VoterInterface::ACCESS_GRANTED, VoterInterface::ACCESS_ABSTAIN, VoterInterface::ACCESS_DENIED], true)) {
37+
throw new \LogicException(\sprintf('"$access" must return one of "%s" constants ("ACCESS_GRANTED", "ACCESS_DENIED" or "ACCESS_ABSTAIN") when "$scoring" is false, "%s" returned.', VoterInterface::class, $access));
3838
}
3939
$this->setMessages($messages);
4040
}
4141

4242
public function __debugInfo(): array
4343
{
44-
return[
44+
return [
4545
'message' => $this->getMessage(),
4646
'context' => $this->context,
4747
'voteResultMessage' => $this->getVoteResultMessage(),
@@ -55,7 +55,7 @@ public function getAccess(): int
5555

5656
public function isGranted(): bool
5757
{
58-
return $this->access === true || $this->access > 0;
58+
return true === $this->access || $this->access > 0;
5959
}
6060

6161
public function isAbstain(): bool
@@ -65,7 +65,7 @@ public function isAbstain(): bool
6565

6666
public function isDenied(): bool
6767
{
68-
return $this->access === false || $this->access < 0;
68+
return false === $this->access || $this->access < 0;
6969
}
7070

7171
/**
@@ -76,7 +76,7 @@ public function setMessages(string|array $messages): void
7676
$this->messages = (array) $messages;
7777
foreach ($this->messages as $message) {
7878
if (!\is_string($message)) {
79-
throw new InvalidArgumentException(sprintf('Message must be string, "%s" given.', get_debug_type($message)));
79+
throw new InvalidArgumentException(\sprintf('Message must be string, "%s" given.', get_debug_type($message)));
8080
}
8181
}
8282
}
@@ -101,7 +101,7 @@ public function getMessage(): string
101101

102102
public function getVoteResultMessage(): string
103103
{
104-
return $this->scoring ? 'SCORE : ' . $this->access : match ($this->access) {
104+
return $this->scoring ? 'SCORE : '.$this->access : match ($this->access) {
105105
VoterInterface::ACCESS_GRANTED => 'ACCESS GRANTED',
106106
VoterInterface::ACCESS_DENIED => 'ACCESS DENIED',
107107
VoterInterface::ACCESS_ABSTAIN => 'ACCESS ABSTAIN',
@@ -113,5 +113,4 @@ public function getContext(): array
113113
{
114114
return $this->context;
115115
}
116-
117-
}
116+
}

0 commit comments

Comments
 (0)
0