Completed
Pull Request — master (#33)
by Timothée
03:16
created

UnknownMiddleware   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 90.91%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 21
ccs 10
cts 11
cp 0.9091
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A withId() 0 6 1
A completeHelpMessage() 0 11 2
1
<?php
2
3
namespace League\Tactician\Bundle\DependencyInjection\Compiler;
4
5
use League\Tactician\Bundle\Middleware;
6
7
class UnknownMiddleware extends \RuntimeException
8
{
9 3
    public static function withId($serviceId)
10
    {
11 3
        return new static(
12 3
            trim(sprintf('Unknown middleware with service id "%s". %s', $serviceId, static::completeHelpMessage($serviceId)))
0 ignored issues
show
Bug introduced by
Since completeHelpMessage() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of completeHelpMessage() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
13 2
        );
14
    }
15
16 3
    private static function completeHelpMessage($serviceId)
17
    {
18
        $helpMessages = [
19 3
            Middleware\ValidatorMiddleware::SERVICE_ID => 'You should have the symfony validator service enabled to use this middleware.',
20 3
            Middleware\SecurityMiddleware::SERVICE_ID => 'You should have the symfony security service enabled to use this middleware.',
21 2
        ];
22
23 3
        if (array_key_exists($serviceId, $helpMessages)) {
24 3
            return $helpMessages[$serviceId];
25
        }
26
    }
27
}
28