Completed
Push — develop ( 3676ae...babe21 )
by Tom
03:56
created

VerifyOrDie   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
c 1
b 0
f 1
lcom 0
cbo 0
dl 0
loc 53
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C filename() 0 22 8
A argumentType() 0 10 2
A violation() 0 4 1
1
<?php
2
/**
3
 * this file is part of magerun
4
 *
5
 * @author Tom Klingenberg <https://github.com/ktomk>
6
 */
7
8
namespace N98\Util;
9
10
use InvalidArgumentException;
11
use RuntimeException;
12
13
/**
14
 * Class VerifyOrDie
15
 *
16
 * @package N98\Util
17
 */
18
class VerifyOrDie
19
{
20
    /**
21
     * Portable basename
22
     *
23
     * @param string $basename
24
     * @param string $message [optional]
25
     * @return string
26
     */
27
    public static function filename($basename, $message = null)
28
    {
29
        static::argumentType('basename', 'string', $basename);
0 ignored issues
show
Bug introduced by
Since argumentType() 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 argumentType() 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...
30
        null === $message || static::argumentType('message', 'string', $message);
0 ignored issues
show
Bug introduced by
Since argumentType() 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 argumentType() 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...
31
32
        # a filename must at least contain a single character
33
        if (!strlen($basename)) {
34
            self::violation($message ?: 'Filename is zero-length string');
35
        }
36
37
        # no control characters, no posix forbidden ones, no windows forbidden ones and no spaces - and not empty
38
        $pattern = '~^[^\x00-\x1F\x7F/<>:"\\|?* ]+$~';
39
        if (!preg_match($pattern, $basename)) {
40
            self::violation($message ?: sprintf("Filename %s is not portable", var_export($basename, true)));
41
        }
42
43
        if ('-' === $basename[0]) {
44
            self::violation($message ?: sprintf("Filename %s starts with a dash", var_export($basename, true)));
45
        }
46
47
        return $basename;
48
    }
49
50
    /**
51
     * @param string $name
52
     * @param string $internalType
53
     * @param mixed $subject
54
     */
55
    private static function argumentType($name, $internalType, $subject)
56
    {
57
        $actual = gettype($subject);
58
        if ($actual !== $internalType) {
59
            throw new InvalidArgumentException(
60
                sprintf('Parameter %s must be of type %s, %s given', $name, $internalType, $actual)
61
            );
62
        }
63
64
    }
65
66
    private static function violation($message)
67
    {
68
        throw new RuntimeException($message);
69
    }
70
}
71