8000 Simplify AbstractVoter by Koc · Pull Request #16079 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Simplify AbstractVoter #16079

8000
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

Closed
wants to merge 1 commit into from
Closed
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
32 changes: 32 additions & 0 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,38 @@ UPGRADE FROM 2.x to 3.0
* The `supportsAttribute()` and `supportsClass()` methods of classes `AuthenticatedVoter`, `ExpressionVoter`
and `RoleVoter` have been removed.

* The `AbstractVoter::supports()` method signature have changed: second argument now is object instead of class name.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor grammar thing:
The AbstractVoter::supports() method signature has changed: second argument is now an object instead of class name.


Before:

```php
class MyVoter extends AbstractVoter
{
protected function supports($attribute, $class)
{
return 'EDIT' === $attribute && $this->isClassInstanceOf($class, 'Topic');
}

// ...
}
```

After:

```php
class MyVoter extends AbstractVoter
{
protected function supports($attribute, $object)
{
return 'EDIT' === $attribute && $object instanceof Topic;
}

// ...
}
```

* The `isClassInstanceOf()` method of classe `AbstractVoter` have been removed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The isClassInstanceOf() method of class AbstractVoter has been removed.


### Translator

* The `Translator::setFallbackLocale()` method has been removed in favor of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Abstract Voter implementation that reduces boilerplate code required to create a custom Voter.
*
* @author Roman Marintšenko <inoryy@gmail.com>
* @author Konstantin Myakshin <koc-dp@yandex.ru>
*/
abstract class AbstractVoter implements VoterInterface
{
Expand All @@ -41,10 +42,9 @@ public function vote(TokenInterface $token, $object, array $attributes)

// abstain vote by default in case none of the attributes are supported
$vote = self::ACCESS_ABSTAIN;
$class = get_class($object);

foreach ($attributes as $attribute) {
if (!$this->supports($attribute, $class)) {
if (!$this->supports($attribute, $object)) {
continue;
}

Expand All @@ -61,31 +61,14 @@ public function vote(TokenInterface $token, $object, array $attributes)
}

/**
* Determines if the attribute and class are supported by this voter.
*
* To determine if the passed class is instance of the supported class, the
* isClassInstanceOf() method can be used.
* Determines if the attribute and object are supported by t 8000 his voter.
*
* @param string $attribute An attribute
* @param string $class The fully qualified class name of the passed object
* @param object $object The object to secure
*
* @return bool True if the attribute and class is supported, false otherwise
* @return bool True if the attribute and object is supported, false otherwise
*/
abstract protected function supports($attribute, $class);

/**
* A helper method to test if the actual class is instanceof or equal
* to the expected class.
*
* @param string $actualClass The actual class name
* @param string $expectedClass The expected class name
*
* @return bool
*/
protected function isClassInstanceOf($actualClass, $expectedClass)
{
return $expectedClass === $actualClass || is_subclass_of($actualClass, $expectedClass);
}
abstract protected function supports($attribute, $object);

/**
* Perform a single access check operation on a given attribute, object and token.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ protected function voteOnAttribute($attribute, $object, TokenInterface $token)
return 'EDIT' === $attribute;
}

protected function supports($attribute, $class)
protected function supports($attribute, $object)
{
return $this->isClassInstanceOf($class, 'stdClass')
&& in_array($attribute, array('EDIT', 'CREATE'));
return $object instanceof \stdClass && in_array($attribute, array('EDIT', 'CREATE'));
}
}
0