Completed
Push — 1.x ( 784bb7...3079dc )
by Alexander
02:41
created

GoAspectContainer::__construct()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 76
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 2.0059

Importance

Changes 7
Bugs 5 Features 0
Metric Value
c 7
b 5
f 0
dl 0
loc 76
ccs 31
cts 35
cp 0.8857
rs 8.9667
cc 2
eloc 45
nc 1
nop 0
crap 2.0059

How to fix   Long Method   

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 2012, 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\Core;
12
13
use Doctrine\Common\Annotations\FileCacheReader;
14
use ReflectionClass;
15
use Go\Aop;
16
use Go\Aop\Pointcut\PointcutLexer;
17
use Go\Aop\Pointcut\PointcutGrammar;
18
use Go\Aop\Pointcut\PointcutParser;
19
use Go\Instrument\RawAnnotationReader;
20
use Go\Instrument\ClassLoading\CachePathManager;
21
use Doctrine\Common\Annotations\AnnotationReader;
22
23
/**
24
 * Aspect container contains list of all pointcuts and advisors
25
 */
26
class GoAspectContainer extends Container implements AspectContainer
27
{
28
    /**
29
     * List of resources for application
30
     *
31
     * @var array
32
     */
33
    protected $resources = [];
34
35
    /**
36
     * Cached timestamp for resources
37
     *
38
     * @var integer
39
     */
40
    protected $maxTimestamp = 0;
41
42
    /**
43
     * Constructor for container
44
     */
45 11
    public function __construct()
46
    {
47
        // Register all services in the container
48
        $this->share('aspect.loader', function(Container $container) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
49 2
            $aspectLoader = new AspectLoader(
50
                $container,
0 ignored issues
show
Documentation introduced by
$container is of type object<Go\Core\Container>, but the function expects a object<Go\Core\AspectContainer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
51 2
                $container->get('aspect.annotation.reader')
52
            );
53 2
            $lexer  = $container->get('aspect.pointcut.lexer');
54 2
            $parser = $container->get('aspect.pointcut.parser');
55
56
            // Register general aspect loader extension
57 2
            $aspectLoader->registerLoaderExtension(new GeneralAspectLoaderExtension($lexer, $parser));
58 2
            $aspectLoader->registerLoaderExtension(new IntroductionAspectExtension($lexer, $parser));
59
60 2
            return $aspectLoader;
61 11
        });
62
63
        $this->share('aspect.cached.loader', function(Container $container) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
64
            $cachedAspectLoader = new CachedAspectLoader(
65
                $container,
0 ignored issues
show
Documentation introduced by
$container is of type object<Go\Core\Container>, but the function expects a object<Go\Core\AspectContainer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
                'aspect.loader',
67
                $container->get('kernel.options')
68
            );
69
70
            return $cachedAspectLoader;
71 11
        });
72
73
        $this->share('aspect.advisor.accessor', function(Container $container) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
74
            return new LazyAdvisorAccessor(
75
                $container,
0 ignored issues
show
Documentation introduced by
$container is of type object<Go\Core\Container>, but the function expects a object<Go\Core\AspectContainer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
                $container->get('aspect.cached.loader')
77
            );
78 11
        });
79
80
        $this->share('aspect.advice_matcher', function(Container $container) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
81 1
            return new AdviceMatcher(
82 1
                $container->get('aspect.loader'),
83 1
                $container->get('kernel.interceptFunctions')
84
            );
85 11
        });
86
87
        $this->share('aspect.annotation.reader', function(Container $container) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
88 3
            $options = $container->get('kernel.options');
89 3
            $reader  = new AnnotationReader();
90 3
            if (!empty($options['cacheDir'])) {
91
                $reader = new FileCacheReader(
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Annotations\FileCacheReader has been deprecated with message: the FileCacheReader is deprecated and will be removed in version 2.0.0 of doctrine/annotations. Please use the {@see \Doctrine\Common\Annotations\CachedReader} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
92
                    $reader,
93
                    $options['cacheDir'] . DIRECTORY_SEPARATOR . '_annotations' . DIRECTORY_SEPARATOR,
94
                    $options['debug'],
95
                    0777 & (~$options['cacheFileMode'])
96
                );
97
            }
98
99 3
            return $reader;
100 11
        });
101
        $this->share('aspect.annotation.raw.reader', function() {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
102 4
            return new RawAnnotationReader();
103 11
        });
104
        $this->share('aspect.cache.path.manager', function(Container $container) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
105
            return new CachePathManager($container->get('kernel'));
106 11
        });
107
108
        // Pointcut services
109
        $this->share('aspect.pointcut.lexer', function() {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
110 3
            return new PointcutLexer();
111 11
        });
112 11
        $this->share('aspect.pointcut.parser', function(Container $container) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
113 3
            return new PointcutParser(
114 3
                new PointcutGrammar(
115
                    $container,
0 ignored issues
show
Documentation introduced by
$container is of type object<Go\Core\Container>, but the function expects a object<Go\Core\AspectContainer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
116 3
                    $container->get('aspect.annotation.raw.reader')
117
                )
118
            );
119 11
        });
120 11
    }
121
122
    /**
123
     * Returns a pointcut by identifier
124
     *
125
     * @param string $id Pointcut identifier
126
     *
127
     * @return Aop\Pointcut
128
     */
129 1
    public function getPointcut($id)
130
    {
131 1
        return $this->get("pointcut.{$id}");
132
    }
133
134
    /**
135
     * Store the pointcut in the container
136
     *
137
     * @param Aop\Pointcut $pointcut Instance
138
     * @param string $id Key for pointcut
139
     */
140 1
    public function registerPointcut(Aop\Pointcut $pointcut, $id)
141
    {
142 1
        $this->set("pointcut.{$id}", $pointcut, array('pointcut'));
143 1
    }
144
145
    /**
146
     * Returns an advisor by identifier
147
     *
148
     * @param string $id Advisor identifier
149
     *
150
     * @return Aop\Advisor
151
     */
152
    public function getAdvisor($id)
153
    {
154
        return $this->get("advisor.{$id}");
155
    }
156
157
    /**
158
     * Store the advisor in the container
159
     *
160
     * @param Aop\Advisor $advisor Instance
161
     * @param string $id Key for advisor
162
     */
163 1
    public function registerAdvisor(Aop\Advisor $advisor, $id)
164
    {
165 1
        $this->set("advisor.{$id}", $advisor, array('advisor'));
166 1
    }
167
168
    /**
169
     * Returns an aspect by id or class name
170
     *
171
     * @param string $aspectName Aspect name
172
     *
173
     * @return Aop\Aspect
174
     */
175 1
    public function getAspect($aspectName)
176
    {
177 1
        return $this->get("aspect.{$aspectName}");
178
    }
179
180
    /**
181
     * Register an aspect in the container
182
     *
183
     * @param Aop\Aspect $aspect Instance of concrete aspect
184
     */
185 1
    public function registerAspect(Aop\Aspect $aspect)
186
    {
187 1
        $refAspect = new ReflectionClass($aspect);
188 1
        $this->set("aspect.{$refAspect->name}", $aspect, array('aspect'));
189 1
        $this->addResource($refAspect->getFileName());
190 1
    }
191
192
    /**
193
     * Add an AOP resource to the container
194
     *
195
     * Resources is used to check the freshness of AOP cache
196
     */
197 2
    public function addResource($resource)
198
    {
199 2
        $this->resources[]  = $resource;
200 2
        $this->maxTimestamp = 0;
201 2
    }
202
203
    /**
204
     * Returns list of AOP resources
205
     *
206
     * @return array
207
     */
208
    public function getResources()
209
    {
210
        return $this->resources;
211
    }
212
213
    /**
214
     * Checks the freshness of AOP cache
215
     *
216
     * @param integer $timestamp
217
     *
218
     * @return bool Whether or not concrete file is fresh
219
     */
220 1
    public function isFresh($timestamp)
221
    {
222 1
        if (!$this->maxTimestamp && !empty($this->resources)) {
223 1
            $this->maxTimestamp = max(array_map('filemtime', $this->resources));
224
        }
225
226 1
        return $this->maxTimestamp <= $timestamp;
227
    }
228
}
229