Completed
Push — master ( b6d688...cba8ad )
by Alexander
11s
created

IntroductionAspectExtension   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 7.41%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 7
dl 0
loc 81
ccs 2
cts 27
cp 0.0741
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getKind() 0 4 1
A getTargets() 0 4 1
A supports() 0 6 2
B load() 0 30 3
1
<?php
2
declare(strict_types = 1);
3
/*
4
 * Go! AOP framework
5
 *
6
 * @copyright Copyright 2012, Lisachenko Alexander <[email protected]>
7
 *
8
 * This source file is subject to the license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Go\Core;
13
14
use Go\Aop\Advisor;
15
use Go\Aop\Aspect;
16
use Go\Aop\Framework;
17
use Go\Aop\Pointcut;
18
use Go\Aop\Support;
19
use Go\Lang\Annotation;
20
use ReflectionProperty;
21
use Reflector;
22
23
/**
24
 * Introduction aspect extension
25
 */
26
class IntroductionAspectExtension extends AbstractAspectLoaderExtension
27
{
28
29
    /**
30
     * Introduction aspect loader works with annotations from aspect
31
     *
32
     * For extension that works with annotations additional metaInformation will be passed
33
     *
34
     * @return string
35
     */
36
    public function getKind() : string
37
    {
38
        return self::KIND_ANNOTATION;
39
    }
40
41
    /**
42
     * Introduction aspect loader works only with properties of aspect
43
     */
44 2
    public function getTargets() : array
45
    {
46 2
        return [self::TARGET_PROPERTY];
47
    }
48
49
    /**
50
     * Checks if loader is able to handle specific point of aspect
51
     *
52
     * @param Aspect $aspect Instance of aspect
53
     * @param mixed|\ReflectionClass|\ReflectionMethod|\ReflectionProperty $reflection Reflection of point
54
     * @param mixed|null $metaInformation Additional meta-information, e.g. annotation for method
55
     *
56
     * @return boolean true if extension is able to create an advisor from reflection and metaInformation
57
     */
58
    public function supports(Aspect $aspect, $reflection, $metaInformation = null) : bool
59
    {
60
        return
61
            ($metaInformation instanceof Annotation\DeclareParents) ||
62
            ($metaInformation instanceof Annotation\DeclareError);
63
    }
64
65
    /**
66
     * Loads definition from specific point of aspect into the container
67
     *
68
     * @param Aspect $aspect Instance of aspect
69
     * @param Reflector|ReflectionProperty $reflection Reflection of point
70
     * @param mixed|null $metaInformation Additional meta-information
71
     *
72
     * @throws \UnexpectedValueException
73
     *
74
     * @return array|Pointcut[]|Advisor[]
75
     */
76
    public function load(Aspect $aspect, Reflector $reflection, $metaInformation = null) : array
77
    {
78
        $loadedItems = [];
79
        $pointcut    = $this->parsePointcut($aspect, $reflection, $metaInformation);
80
        $propertyId  = $reflection->class . '->' . $reflection->name;
0 ignored issues
show
Bug introduced by
Accessing class on the interface Reflector suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing name on the interface Reflector suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
81
82
        switch (true) {
83
            case ($metaInformation instanceof Annotation\DeclareParents):
84
                $interface = $metaInformation->interface;
85
                $implement = $metaInformation->defaultImpl;
86
                $advice    = new Framework\TraitIntroductionInfo($interface, $implement);
87
                $advisor   = new Support\DeclareParentsAdvisor($pointcut->getClassFilter(), $advice);
88
                $loadedItems[$propertyId] = $advisor;
89
                break;
90
91
            case ($metaInformation instanceof Annotation\DeclareError):
92
                $reflection->setAccessible(true);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Reflector as the method setAccessible() does only exist in the following implementations of said interface: Doctrine\Common\Reflecti...ublicReflectionProperty, Doctrine\Common\Reflection\StaticReflectionMethod, Doctrine\Common\Reflecti...taticReflectionProperty, Doctrine\ORM\Mapping\ReflectionEmbeddedProperty, Go\Aop\Support\AnnotatedReflectionMethod, Go\ParserReflection\ReflectionMethod, Go\ParserReflection\ReflectionProperty, ReflectionMethod, ReflectionProperty.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
93
                $message = $reflection->getValue($aspect);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Reflector as the method getValue() does only exist in the following implementations of said interface: Doctrine\Common\Reflecti...ublicReflectionProperty, Doctrine\Common\Reflecti...taticReflectionProperty, Doctrine\ORM\Mapping\ReflectionEmbeddedProperty, Go\ParserReflection\ReflectionProperty, ReflectionProperty.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
94
                $level   = $metaInformation->level;
95
                $advice  = new Framework\DeclareErrorInterceptor($message, $level, $metaInformation->value);
96
                $loadedItems[$propertyId] = new Support\DefaultPointcutAdvisor($pointcut, $advice);
0 ignored issues
show
Compatibility introduced by
$pointcut of type object<Go\Aop\PointFilter> is not a sub-type of object<Go\Aop\Pointcut>. It seems like you assume a child interface of the interface Go\Aop\PointFilter to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
97
                break;
98
99
            default:
100
                throw new \UnexpectedValueException("Unsupported pointcut class: " . get_class($pointcut));
101
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
102
        }
103
104
        return $loadedItems;
105
    }
106
}
107