Build   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 12
eloc 20
c 4
b 0
f 0
dl 0
loc 81
ccs 22
cts 22
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A definition() 0 17 6
A host() 0 11 2
A build() 0 3 1
A create() 0 4 2
A createDefault() 0 3 1
1
<?php
2
/**
3
 *
4
 */
5
6
namespace Mvc5\Route\Definition;
7
8
use Mvc5\Exception;
9
use Mvc5\Route\Config;
10
use Mvc5\Route\Route;
11
12
use function is_array;
13
14
use const Mvc5\{ CLASS_NAME, CONSTRAINTS, HOST, NAME, PATH, REGEX, TOKENS };
0 ignored issues
show
Bug introduced by
The type Mvc5\TOKENS 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, Mvc5\Route\Definition\REGEX. 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...
Bug introduced by
The type Mvc5\PATH 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
The type Mvc5\CLASS_NAME 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
The type Mvc5\HOST 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
The type Mvc5\REGEX 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
The type Mvc5\NAME 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, Mvc5\Route\Definition\TOKENS. 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...
Bug introduced by
The type Mvc5\CONSTRAINTS 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...
15
16
trait Build
17
{
18
    /**
19
     *
20
     */
21
    use Regex;
22
    use Tokens;
23
24
    /**
25
     * @param array|Route $route
26
     * @param bool $compile
27
     * @return Route
28
     * @throws \Throwable
29
     */
30 32
    protected function build($route, bool $compile = true) : Route
31
    {
32 32
        return $this->definition($this->create($route), $compile);
33
    }
34
35
    /**
36
     * @param array|Route $route
37
     * @return Route
38
     */
39 32
    protected function create($route) : Route
40
    {
41 32
        return $route instanceof Route ? $route :
42 32
            $this->createDefault($route, $route[CLASS_NAME] ?? Config::class);
43
    }
44
45
    /**
46
     * @param array|\ArrayAccess $route
47
     * @param string $class
48
     * @return Route
49
     */
50 12
    protected function createDefault($route = [], string $class = Config::class) : Route
51
    {
52 12
        return new $class($route);
53
    }
54
55
    /**
56
     * @param Route $route
57
     * @param bool $compile
58
     * @return Route
59
     * @throws \Throwable
60
     */
61 32
    protected function definition(Route $route, bool $compile = true) : Route
62
    {
63
        /** @var Route $route */
64 32
        $route = $this->host($route, $route[HOST]);
65
66 32
        if (!isset($route[PATH])) {
67 3
            return isset($route[REGEX]) ? $route : Exception::invalidArgument('Route path not specified');
68
        }
69
70 29
        !isset($route[TOKENS]) && $route = $route->with(TOKENS, $this->tokens(
71 29
            $route[PATH], $route[CONSTRAINTS] ?? []
72
        ));
73
74 29
        $compile && !isset($route[REGEX]) &&
75 22
            $route = $route->with(REGEX, $this->regex($route[TOKENS]));
76
77 29
        return $route;
78
    }
79
80
    /**
81
     * @param Route $route
82
     * @param array|mixed $host
83
     * @return Route
84
     * @throws \Throwable
85
     */
86 32
    protected function host(Route $route, $host) : Route
87
    {
88 32
        if (!is_array($host)) {
89 31
            return $route;
90
        }
91
92 2
        $host[TOKENS] ??= $this->tokens($host[NAME], $host[CONSTRAINTS] ?? []);
93
94 2
        $host[REGEX] ??= $this->regex($host[TOKENS]);
95
96 2
        return $route->with(HOST, $host);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $route->with(Mvc5\HOST, $host) returns the type Mvc5\Config\Model which includes types incompatible with the type-hinted return Mvc5\Route\Route.
Loading history...
97
    }
98
}
99