Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feature #16102 Simplify AbstractVoter (Koc)
This PR was merged into the 2.8 branch.

Discussion
----------

Simplify AbstractVoter

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no, just simplification
| BC breaks?    | no, because 2.8 is not yet released
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Commits
-------

93de659 Simplify AbstractVoter
  • Loading branch information
fabpot committed Oct 5, 2015
2 parents 9bbab98 + 93de659 commit 3567548
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 30 deletions.
5 changes: 2 additions & 3 deletions UPGRADE-3.0.md
Expand Up @@ -707,10 +707,9 @@ UPGRADE FROM 2.x to 3.0
```php
class MyVoter extends AbstractVoter
{
protected function supports($attribute, $class)
protected function supports($attribute, $object)
{
return $this->isClassInstanceOf($class, 'AppBundle\Entity\Post')
&& in_array($attribute, array('CREATE', 'EDIT'));
return $object instanceof Post && in_array($attribute, array('CREATE', 'EDIT'));
}

// ...
Expand Down
Expand Up @@ -68,10 +68,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 @@ -88,25 +87,22 @@ 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 this voter.
*
* This method will become abstract in 3.0.
*
* @param string $attribute An attribute
* @param string $class The fully qualified class name of the passed object
* @param string $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
*/
protected function supports($attribute, $class)
protected function supports($attribute, $object)
{
@trigger_error('The getSupportedClasses and getSupportedAttributes methods are deprecated since version 2.8 and will be removed in version 3.0. Overwrite supports instead.', E_USER_DEPRECATED);

$classIsSupported = false;
foreach ($this->getSupportedClasses() as $supportedClass) {
if ($this->isClassInstanceOf($class, $supportedClass)) {
if ($object instanceof $supportedClass) {
$classIsSupported = true;
break;
}
Expand All @@ -123,20 +119,6 @@ protected function supports($attribute, $class)
return true;
}

/**
* 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);
}

/**
* Return an array of supported classes. This will be called by supportsClass.
*
Expand Down
Expand Up @@ -84,10 +84,9 @@ 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'));
}
}

Expand Down

0 comments on commit 3567548

Please sign in to comment.