GeneralAspectLoaderExtension   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 84.21%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 0
dl 0
loc 64
ccs 16
cts 19
cp 0.8421
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 24 5
A getInterceptor() 0 21 5
1
<?php
2
3
declare(strict_types = 1);
4
/*
5
 * Go! AOP framework
6
 *
7
 * @copyright Copyright 2012, Lisachenko Alexander <[email protected]>
8
 *
9
 * This source file is subject to the license that is bundled
10
 * with this source code in the file LICENSE.
11
 */
12
13
namespace Go\Core;
14
15
use Closure;
16
use Go\Aop\Advisor;
17
use Go\Aop\Aspect;
18
use Go\Aop\Framework\AfterInterceptor;
19
use Go\Aop\Framework\AfterThrowingInterceptor;
20
use Go\Aop\Framework\AroundInterceptor;
21
use Go\Aop\Framework\BeforeInterceptor;
22
use Go\Aop\Intercept\Interceptor;
23
use Go\Aop\Pointcut;
24
use Go\Aop\Support\DefaultPointcutAdvisor;
25
use Go\Lang\Annotation;
26
use Go\Lang\Annotation\After;
27
use Go\Lang\Annotation\AfterThrowing;
28
use Go\Lang\Annotation\Around;
29
use Go\Lang\Annotation\BaseInterceptor;
30
use Go\Lang\Annotation\Before;
31
use ReflectionClass;
32
use UnexpectedValueException;
33
34
use function get_class;
35
36 1
/**
37
 * General aspect loader add common support for general advices, declared as annotations
38 1
 */
39
class GeneralAspectLoaderExtension extends AbstractAspectLoaderExtension
40
{
41
    /**
42
     * Loads definition from specific point of aspect into the container
43
     *
44 2
     * @param Aspect          $aspect           Instance of aspect
45
     * @param ReflectionClass $reflectionAspect Reflection of point
46 2
     *
47
     * @return array<string,Pointcut>|array<string,Advisor>
48
     *
49
     * @throws UnexpectedValueException
50
     */
51
    public function load(Aspect $aspect, ReflectionClass $reflectionAspect): array
52
    {
53
        $loadedItems = [];
54
        foreach ($reflectionAspect->getMethods() as $aspectMethod) {
55
            $methodId    = $reflectionAspect->getName() . '->'. $aspectMethod->getName();
56
            $annotations = $this->reader->getMethodAnnotations($aspectMethod);
57
58 1
            foreach ($annotations as $annotation) {
59
                if ($annotation instanceof Annotation\Pointcut) {
60 1
                    $loadedItems[$methodId] = $this->parsePointcut($aspect, $reflectionAspect, $annotation->value);
61 1
                } elseif ($annotation instanceof Annotation\BaseInterceptor) {
0 ignored issues
show
Bug introduced by
The class Go\Lang\Annotation\BaseInterceptor does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
62
                    $pointcut       = $this->parsePointcut($aspect, $reflectionAspect, $annotation->value);
63
                    $adviceCallback = $aspectMethod->getClosure($aspect);
64
                    $interceptor    = $this->getInterceptor($annotation, $adviceCallback);
65
66
                    $loadedItems[$methodId] = new DefaultPointcutAdvisor($pointcut, $interceptor);
67
                } else {
68
                    throw new UnexpectedValueException('Unsupported annotation class: ' . get_class($annotation));
69
                }
70
            }
71
        }
72
73
        return $loadedItems;
74
    }
75 1
76
    /**
77 1
     * Returns an interceptor instance by meta-type annotation and closure
78 1
     *
79 1
     * @throws UnexpectedValueException For unsupported annotations
80 1
     */
81
    protected function getInterceptor(BaseInterceptor $metaInformation, Closure $adviceCallback): Interceptor
82
    {
83
        $adviceOrder        = $metaInformation->order;
84 1
        $pointcutExpression = $metaInformation->value;
85
        switch (true) {
86
            case ($metaInformation instanceof Before):
87
                return new BeforeInterceptor($adviceCallback, $adviceOrder, $pointcutExpression);
88 1
89 1
            case ($metaInformation instanceof After):
90
                return new AfterInterceptor($adviceCallback, $adviceOrder, $pointcutExpression);
91 1
92 1
            case ($metaInformation instanceof Around):
93
                return new AroundInterceptor($adviceCallback, $adviceOrder, $pointcutExpression);
94
95
            case ($metaInformation instanceof AfterThrowing):
96
                return new AfterThrowingInterceptor($adviceCallback, $adviceOrder, $pointcutExpression);
97
98 1
            default:
99
                throw new UnexpectedValueException('Unsupported method meta class: ' . get_class($metaInformation));
100
        }
101
    }
102
}
103