AbstractGermanyPlate::getType()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace PBurggraf\LicensePlate\Detector\Germany;
4
5
use PBurggraf\LicensePlate\Detector\GermanyDetector;
6
7
/**
8
 * @author Philip Burggraf <[email protected]>
9
 */
10
abstract class AbstractGermanyPlate
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $plate;
16
17
    /**
18
     * @var string
19
     */
20
    protected $prefix;
21
22
    /**
23
     * @var string[]
24
     */
25
    protected static $name = [
26
        '',
27
    ];
28
29
    /**
30
     * @var int[]
31
     */
32
    protected static $type = [
33
        GermanyDetector::PLATE_TYPE_DEFAULT,
34
    ];
35
36
    /**
37
     * @var string[]
38
     */
39
    protected static $regexes = [
40
        '/^(?:[A-ZÄÖÜ]{1,2} [A-Z]{1,2} [1-9]\d{0,3}[EH]?|[A-ZÄÖÜ]{3} (?:[A-Z] [1-9]\d{0,3}[EH]?|[A-Z]{2} [1-9]\d{0,2}[EH]?))$/',
41
    ];
42
43
    /**
44
     * @var string[]
45
     */
46
    protected static $globalName = [
47
        'Konsularische Korps',
48
    ];
49
50
    /**
51
     * @var int[]
52
     */
53
    protected static $globalType = [
54
        GermanyDetector::PLATE_TYPE_DIPLOMATIC_CORPS,
55
    ];
56
57
    /**
58
     * @var string[]
59
     */
60
    protected static $globalRegex = [
61
        '/^[A-ZÄÖÜ]{1,2} 9\d{2,4}$/',
62
    ];
63
64
    /**
65
     * AbstractGermanyPlate constructor.
66
     *
67
     * @param string $plate
68
     */
69
    public function __construct($plate)
70
    {
71
        $this->plate = $plate;
72
73
        static::$name = array_merge(static::$name, static::$globalName);
74
        static::$type = array_merge(static::$type, static::$globalType);
75
        static::$regexes = array_merge(static::$regexes, static::$globalRegex);
76
    }
77
78
    /**
79
     * @return string
80
     */
81 View Code Duplication
    public function getDescription()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
    {
83
        $result = $this->getIndex();
84
85
        if ($result !== false) {
86
            return static::$name[(int) $result];
87
        }
88
89
        return '';
90
    }
91
92
    /**
93
     * @return int
94
     */
95 View Code Duplication
    public function getType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97
        $result = $this->getIndex();
98
99
        if ($result !== false) {
100
            return static::$type[(int) $result];
101
        }
102
103
        return GermanyDetector::PLATE_TYPE_UNKNOWN;
104
    }
105
106
    /**
107
     * @return bool
108
     */
109
    public function isValid()
110
    {
111
        return $this->getIndex() !== false;
112
    }
113
114
    /**
115
     * @return int|bool
116
     */
117
    protected function getIndex()
118
    {
119
        foreach (static::$regexes as $index => $regex) {
120
            if (preg_match($regex . 'u', $this->plate) === 1) {
121
                return $index;
122
            }
123
        }
124
125
        return false;
126
    }
127
}
128