Completed
Pull Request — master (#98)
by Alejandro
06:29
created

EntityDoesNotExistException   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromEntityAndConditions() 0 8 1
A serializeParams() 0 9 2
1
<?php
2
namespace Shlinkio\Shlink\Core\Exception;
3
4
use Shlinkio\Shlink\Common\Exception\ExceptionInterface;
5
6
class EntityDoesNotExistException extends \RuntimeException implements ExceptionInterface
7
{
8 1
    public static function createFromEntityAndConditions($entityName, array $conditions)
9
    {
10 1
        return new self(sprintf(
11 1
            'Entity of type %s with params [%s] does not exist',
12 1
            $entityName,
13 1
            static::serializeParams($conditions)
0 ignored issues
show
Bug introduced by
Since serializeParams() 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 serializeParams() 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...
14
        ));
15
    }
16
17 1
    private static function serializeParams(array $params)
18
    {
19 1
        $result = [];
20 1
        foreach ($params as $key => $value) {
21 1
            $result[] = sprintf('"%s" => "%s"', $key, $value);
22
        }
23
24 1
        return implode(', ', $result);
25
    }
26
}
27