GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 0268aa...3ec9bd )
by cao
05:24
created

ContainerBuilder::postCreateContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpBoot\Annotation;
4
5
use Doctrine\Common\Cache\Cache;
6
use PhpBoot\Cache\CheckableCache;
7
use PhpBoot\Cache\ClassModifiedChecker;
8
use PhpBoot\Utils\Logger;
9
10
abstract class ContainerBuilder
11
{
12
    /**
13
     * ContainerBuilder constructor.
14
     * @param array $annotations 需加载的注释和顺序
15
     * @param Cache $cache
16
     * 语法 http://jmespath.org/tutorial.html
17
     *
18
     *  [
19
     *      [PropertyAnnotationHandler::class,   'property'],
20
     *      ...
21
     *  ];
22
     *
23
     */
24 87
    public function __construct(array $annotations, Cache $cache)
25
    {
26 87
        $this->annotations = $annotations;
27 87
        $this->cache = $cache;
28 87
    }
29
30 1
    public function setCache(Cache $cache)
31 1
    {
32
        $this->cache = $cache;
33
    }
34
    /**
35
     * load from class with local cache
36
     * @param string $className
37
     * @return object
38
     */
39 87
    public function build($className)
40 1
    {
41
        //TODO【重要】 使用全局的缓存版本号, 而不是针对每个文件判断缓存过期与否
42 87
        $rfl = new \ReflectionClass($className) or \PhpBoot\abort("load class $className failed");
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
43 87
        $fileName = $rfl->getFileName();
44 87
        $key = str_replace('\\','.',get_class($this)).md5(serialize($this->annotations).$fileName.$className);
45 87
        $cache = new CheckableCache($this->cache);
46 87
        $res = $cache->get($key, $this);
47 87
        if($res === $this){
48
            try{
49 20
                $meta = $this->buildWithoutCache($className);
50 20
                $cache->set($key, $meta, 0, $fileName?new ClassModifiedChecker($className):null);
51 20
                return $meta;
52
            }catch (\Exception $e){
53
                Logger::warning(__METHOD__.' failed with '.$e->getMessage());
54
                $cache->set($key, $e->getMessage(), 0, $fileName?new ClassModifiedChecker($className):null);
55
                throw $e;
56
            }
57 73
        }elseif(is_string($res)){
58
            \PhpBoot\abort($res);
59
        }else{
60 73
            return $res;
61
        }
62
    }
63
64
    /**
65
     * @param string $className
66
     * @return object
67
     */
68
    abstract protected function createContainer($className);
69
70 20
    protected function postCreateContainer($object)
71
    {
72
73 20
    }
74 15
    protected function handleAnnotation($handlerName, $container, $ann){
75 15
        $handler = new $handlerName();
76 15
        return $handler($container, $ann);
77
    }
78
    /**
79
     * @param $className
80
     * @return object
81
     */
82 20
    public function buildWithoutCache($className)
83
    {
84 20
        $container = $this->createContainer($className);
85 20
        $anns = AnnotationReader::read($className, $this->cache);
86 20
        foreach ($this->annotations as $i){
87 20
            list($class, $target) = $i;
88
89 20
            $found = \JmesPath\search($target, $anns);
90 20
            if(is_array($found)){
91 20
                foreach ($found as $f){
92 20
                    $this->handleAnnotation($class, $container,$f); //TODO 支持
93 20
                }
94 20
            }else{
95 12
                $this->handleAnnotation($class, $container, $found);
96
            }
97 20
        }
98 20
        $this->postCreateContainer($container);
99 20
        return $container;
100
    }
101
102
    /**
103
     * @var array
104
     */
105
    private $annotations=[];
106
    /**
107
     * @var Cache
108
     */
109
    private $cache;
110
}