Completed
Push — 1.x ( c183a4...05b51a )
by Alexander
8s
created

AbstractJoinpoint::sortAdvices()   C

Complexity

Conditions 10
Paths 1

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 10
Metric Value
dl 0
loc 24
ccs 14
cts 14
cp 1
rs 5.2164
cc 10
eloc 15
nc 1
nop 1
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2011, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Aop\Framework;
12
13
use Go\Aop\Advice;
14
use Go\Aop\AdviceAfter;
15
use Go\Aop\AdviceBefore;
16
use Go\Aop\AdviceAround;
17
use Go\Aop\Intercept\Joinpoint;
18
19
/**
20
 *  Abstract joinpoint for framework
21
 *
22
 * Join points are points in the execution of the system, such as method calls,
23
 * where behavior supplied by aspects is combined. A join point is a point in
24
 * the execution of the program, which is used to define the dynamic structure
25
 * of a crosscutting concern.
26
 *
27
 * @link http://en.wikipedia.org/wiki/Aspect-oriented_software_development#Join_point_model
28
 */
29
abstract class AbstractJoinpoint implements Joinpoint
30
{
31
    /**
32
     * List of advices
33
     *
34
     * @var array|Advice[]
35
     */
36
    protected $advices = [];
37
38
    /**
39
     * Current advice index
40
     *
41
     * @var int
42
     */
43
    protected $current = 0;
44
45
    /**
46
     * Stack frames to work with recursive calls or with cross-calls inside object
47
     *
48
     * @var array
49
     */
50
    protected $stackFrames = [];
51
52
    /**
53
     * Recursion level for invocation
54
     *
55
     * @var int
56
     */
57
    protected $level = 0;
58
59
    /**
60
     * Initializes list of advices for current joinpoint
61
     *
62
     * @param array $advices List of advices
63
     */
64 26
    public function __construct(array $advices)
65
    {
66 26
        $this->advices = $advices;
67 26
    }
68
69
    /**
70
     * Sorts advices by priority
71
     *
72
     * @param array|Advice[] $advices
73
     * @return array|Advice[] Sorted list of advices
74
     */
75 13
    public static function sortAdvices(array $advices)
76
    {
77 13
        $sortedAdvices = $advices;
78 13
        uasort($sortedAdvices, function(Advice $first, Advice $second) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
79
            switch (true) {
80 8
                case $first instanceof AdviceBefore && !($second instanceof AdviceBefore):
81 3
                    return -1;
82
83 6
                case $first instanceof AdviceAround && !($second instanceof AdviceAround):
84 3
                    return 1;
85
86 4
                case $first instanceof AdviceAfter && !($second instanceof AdviceAfter):
87 2
                    return $second instanceof AdviceBefore ? 1 : -1;
88
89 2
                case ($first instanceof OrderedAdvice && $second instanceof OrderedAdvice):
90 1
                    return $first->getAdviceOrder() - $second->getAdviceOrder();
91
92
                default:
93 1
                    return 0;
94
            }
95 13
        });
96
97 13
        return $sortedAdvices;
98
    }
99
}
100