Completed
Pull Request — master (#158)
by Grégoire
02:23
created

TwigTemplate   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 38
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A attachRecorder() 0 4 1
A getAttribute() 0 10 3
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\CacheBundle\Twig;
13
14
use Sonata\Cache\Invalidation\Recorder;
15
use Twig\Template;
16
17
abstract class TwigTemplate extends Template
18
{
19
    /**
20
     * @var \Sonata\CacheBundle\Invalidation\Recorder
21
     */
22
    protected static $recorder;
23
24
    /**
25
     * @static
26
     *
27
     * @param \Sonata\Cache\Invalidation\Recorder $recorder
28
     */
29
    public static function attachRecorder(Recorder $recorder)
30
    {
31
        self::$recorder = $recorder;
0 ignored issues
show
Documentation Bug introduced by
It seems like $recorder of type object<Sonata\Cache\Invalidation\Recorder> is incompatible with the declared type object<Sonata\CacheBundle\Invalidation\Recorder> of property $recorder.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32
    }
33
34
    /**
35
     * @param mixed  $object
36
     * @param string $item
37
     * @param array  $arguments
38
     * @param string $type
39
     * @param bool   $isDefinedTest
40
     * @param bool   $ignoreStrictCheck
41
     *
42
     * @return mixed
43
     */
44
    protected function getAttribute($object, $item, array $arguments = [], $type = Template::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false)
0 ignored issues
show
Unused Code introduced by
The parameter $ignoreStrictCheck is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45
    {
46
        $attribute = parent::getAttribute($object, $item, $arguments, $type, $isDefinedTest);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Twig\Template as the method getAttribute() does only exist in the following sub-classes of Twig\Template: Sonata\CacheBundle\Twig\TwigTemplate. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
47
48
        if (self::$recorder && is_object($object)) {
49
            self::$recorder->add($object);
50
        }
51
52
        return $attribute;
53
    }
54
}
55