Passed
Pull Request — master (#32)
by Jitendra
02:52
created

MiddlewareTrait::call()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 2
1
<?php
2
3
namespace PhalconExt\Cli;
4
5
use Phalcon\Cli\Console;
0 ignored issues
show
Bug introduced by
The type Phalcon\Cli\Console was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
This use statement conflicts with another class in this namespace, PhalconExt\Cli\Console. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
7
trait MiddlewareTrait
8
{
9
    protected $middlewares = [
10
        Middleware\Factory::class,
11
    ];
12
13
    protected function bindEvents(Console $console)
14
    {
15
        $evm = $this->di('eventsManager');
0 ignored issues
show
Bug introduced by
It seems like di() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

15
        /** @scrutinizer ignore-call */ 
16
        $evm = $this->di('eventsManager');
Loading history...
16
17
        $evm->attach('dispatch', $console);
18
        $console->setEventsManager($evm);
19
20
        $this->di('dispatcher')->setEventsManager($evm);
21
    }
22
23
    public function middleware(string $class): self
24
    {
25
        $this->middlewares[] = $class;
26
27
        return $this;
28
    }
29
30
    public function middlewares(): array
31
    {
32
        return $this->middlewares;
33
    }
34
35
    public function beforeExecuteRoute(): bool
36
    {
37
        return $this->relay('before');
38
    }
39
40
    public function afterExecuteRoute(): bool
41
    {
42
        return $this->relay('after');
43
    }
44
45
    protected function relay(string $event): bool
46
    {
47
        foreach ($this->middlewares as $middleware) {
48
            if (!$this->call($event, $middleware)) {
49
                return false;
50
            }
51
        }
52
53
        return true;
54
    }
55
56
    protected function call(string $event, $middleware): bool
57
    {
58
        if (\is_string($middleware)) {
59
            $middleware = $this->di($middleware);
60
        }
61
62
        if (!\method_exists($middleware, $event)) {
63
            return true;
64
        }
65
66
        return $middleware->$event($this->di('console'));
67
    }
68
}
69