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

createFromEntityAndConditions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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