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.

ControllerContainerBuilder::build()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 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\Controller;
4
5
use DI\FactoryInterface;
6
use \DI\InvokerInterface as DIInvokerInterface;
7
use Doctrine\Common\Cache\Cache;
8
use PhpBoot\Cache\LocalCacheInterface;
9
use PhpBoot\Controller\Annotations\BindAnnotationHandler;
10
use PhpBoot\Controller\Annotations\ClassAnnotationHandler;
11
use PhpBoot\Controller\Annotations\HookAnnotationHandler;
12
use PhpBoot\Controller\Annotations\ParamAnnotationHandler;
13
use PhpBoot\Controller\Annotations\PathAnnotationHandler;
14
use PhpBoot\Controller\Annotations\ReturnAnnotationHandler;
15
use PhpBoot\Controller\Annotations\RouteAnnotationHandler;
16
use PhpBoot\Controller\Annotations\ThrowsAnnotationHandler;
17
use PhpBoot\Controller\Annotations\ValidateAnnotationHandler;
18
use PhpBoot\Annotation\ContainerBuilder;
19
20
class ControllerContainerBuilder extends ContainerBuilder
21
{
22
    static $DEFAULT_ANNOTATIONS=[
23
        [ClassAnnotationHandler::class, 'class'],
24
        [PathAnnotationHandler::class, "class.children[?name=='path']"],
25
        [RouteAnnotationHandler::class, "methods.*.children[?name=='route'][]"],
26
        [ParamAnnotationHandler::class, "methods.*.children[?name=='param'][]"],
27
        [ReturnAnnotationHandler::class, "methods.*.children[?name=='return'][]"],
28
        [BindAnnotationHandler::class, "methods.*.children[].children[?name=='bind'][]"],
29
        [ThrowsAnnotationHandler::class, "methods.*.children[?name=='throws'][]"],
30
        [ValidateAnnotationHandler::class, "methods.*.children[].children[?name=='v'][]"],
31
        [HookAnnotationHandler::class, "methods.*.children[?name=='hook'][]"],
32
    ];
33
34
    /**
35
     * ControllerContainerBuilder constructor.
36
     * @param FactoryInterface $factory
37
     * @param DIInvokerInterface $diInvoker
38
     * @param Cache $cache
39
     * @param array $annotations
40
     */
41 89 View Code Duplication
    public function __construct(FactoryInterface $factory,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
                                DIInvokerInterface $diInvoker,
43
                                Cache $cache,
44
                                array $annotations = null)
45
    {
46 89
        if($annotations){
47
            parent::__construct($annotations, $cache);
48
        }else{
49 89
            parent::__construct(self::$DEFAULT_ANNOTATIONS, $cache);
50
        }
51
52 89
        $this->factory = $factory;
53 89
        $this->diInvoker = $diInvoker;
54 89
    }
55
    /**
56
     * load from class with local cache
57
     * @param string $className
58
     * @return ControllerContainer
59
     */
60 7
    public function build($className)
61
    {
62 7
        return parent::build($className);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return parent::build($className); (object|null|integer|double|array|boolean) is incompatible with the return type documented by PhpBoot\Controller\Contr...ContainerBuilder::build of type PhpBoot\Controller\ControllerContainer|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
63
    }
64
65
    /**
66
     * @param $className
67
     * @return ControllerContainer
68
     */
69 4
    public function buildWithoutCache($className)
70
    {
71 4
        return parent::buildWithoutCache($className);
72
    }
73
74
    /**
75
     * @param string $className
76
     * @return object
77
     */
78 4
    protected function createContainer($className)
79
    {
80 4
        return $this->factory->make(ControllerContainer::class, ['className'=>$className]);
81
    }
82
83 4
    protected function handleAnnotation($handlerName, $container, $ann)
84
    {
85 4
        $handler = $this->factory->make($handlerName);
86 4
        return $this->diInvoker->call($handler, [$container, $ann]);
0 ignored issues
show
Documentation introduced by
$handler is of type *, but the function expects a callable.

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...
87
    }
88
89
90
    /**
91
     * @var FactoryInterface
92
     */
93
    private $factory;
94
    /**
95
     * @var DIInvokerInterface
96
     */
97
    private $diInvoker;
98
}