Passed
Pull Request — master (#32)
by Jitendra
01:59
created

MiddlewareTrait::bindEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace PhalconExt\Cli;
4
5
use Ahc\Cli\Helper\OutputHelper;
0 ignored issues
show
Bug introduced by
The type Ahc\Cli\Helper\OutputHelper 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...
6
use Ahc\Cli\Input\Command;
0 ignored issues
show
Bug introduced by
The type Ahc\Cli\Input\Command 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...
7
use Ahc\Cli\Output\Writer;
0 ignored issues
show
Bug introduced by
The type Ahc\Cli\Output\Writer 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...
8
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...
9
10
trait MiddlewareTrait
11
{
12
    protected $middlewares = [
13
        Middleware\Factory::class,
14
    ];
15
16
    protected function bindEvents(Console $console)
17
    {
18
        $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

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