Test Failed
Pull Request — main (#30)
by Peter
09:21 queued 06:39
created

FakeCar::isSupported()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 1
cts 1
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Faker\Provider;
4
5
use Exception;
6
use Faker\Generator;
7
8
class FakeCar extends \Faker\Provider\Base
9
{
10
    protected const EBCDIC = '0123456789.ABCDEFGH..JKLMN.P.R..STUVWXYZ';
11
12
    protected const MODEL_YEAR = 'ABCDEFGHJKLMNPRSTVWXY123456789';
13
14
    protected FakeCarDataProviderInterface $dataProvider;
15
16
    public function __construct(Generator $generator)
17
    {
18 19
        parent::__construct($generator);
19
        $this->setDataProvider(new FakeCarDataProvider);
20 19
    }
21 19
22
    public function setDataProvider(FakeCarDataProviderInterface $dataProvider): void
23
    {
24 19
        $this->dataProvider = $dataProvider;
25
    }
26 19
27
    /**
28
     * Get vehicle string with brand and model
29
     */
30
    public function vehicle(): string
31
    {
32
        $vehicleBrand = $this->vehicleBrand();
33
34 2
        return $vehicleBrand.' '.$this->vehicleModel($vehicleBrand);
35
    }
36 2
37
    /**
38 2
     * Get vehicle with brand and model as an array
39
     */
40
    public function vehicleArray(): array
41
    {
42
        $vehicleBrand = $this->vehicleBrand();
43
44
        return [
45
            'brand' => $vehicleBrand,
46 3
            'model' => $this->vehicleModel($vehicleBrand),
47
        ];
48 3
    }
49
50 3
    /**
51 3
     * Get random vehicle brand
52 3
     */
53 3
    public function vehicleBrand(): string
54
    {
55
        return (string) $this->dataProvider->getVehicleBrand();
56
    }
57
58
    /**
59
     * Get random vehicle model
60
     *
61 6
     * @param  string  $brand  Get random model from specific brand (optional)
62
     * @return mixed
63 6
     */
64
    public function vehicleModel(?string $brand = null): string
65
    {
66
        return (string) $this->dataProvider->getVehicleModel($brand);
0 ignored issues
show
Unused Code introduced by
The call to Faker\Provider\FakeCarDa...face::getVehicleModel() has too many arguments starting with $brand. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        return (string) $this->dataProvider->/** @scrutinizer ignore-call */ getVehicleModel($brand);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
67
    }
68
69
    /**
70
     * Generate VIN
71
     *
72
     * @link https://en.wikipedia.org/wiki/Vehicle_identification_number
73 5
     *
74
     * @return mixed
75 5
     */
76
    public function vin(int $year = 1980): string
77
    {
78
        $modelYear = static::modelYear($year);
79
        $regex     = "([a-hj-npr-z0-9]{8})_{$modelYear}([a-hj-npr-z0-9]{7})";
80
        $vin       = static::regexify($regex);
81
82
        return str_replace('_', self::checkDigit($vin), $vin);
83
    }
84
85
    /**
86 1
     * Get vehicle registration number
87
     */
88 1
    public function vehicleRegistration(string $regex = '[A-Z]{3}-[0-9]{3}'): string
89 1
    {
90 1
        //TODO: Set format based on locale
91 1
        return static::regexify($regex);
92
    }
93
94
    /**
95
     * Get vehicle type
96
     *
97
     * @throws Exception
98
     */
99
    public function vehicleType(): string
100
    {
101 1
        return (string) $this->dataProvider->getVehicleType();
102
    }
103
104 1
    /**
105
     * Get vehicle fuel type
106
     */
107
    public function vehicleFuelType(int $count = 1): string
108
    {
109
        return (string) $this->dataProvider->getVehicleFuelType($count);
0 ignored issues
show
Unused Code introduced by
The call to Faker\Provider\FakeCarDa...e::getVehicleFuelType() has too many arguments starting with $count. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
        return (string) $this->dataProvider->/** @scrutinizer ignore-call */ getVehicleFuelType($count);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
110
    }
111
112
    /**
113 3
     * Get vehicle door count
114
     *
115 3
     * @throws Exception
116
     */
117
    public function vehicleDoorCount(): int
118
    {
119
        return (int) $this->dataProvider->getVehicleDoorCount();
120
    }
121
122
    /**
123
     * Get vehicle door count
124
     *
125 2
     * @throws Exception
126
     */
127 2
    public function vehicleSeatCount(): int
128
    {
129
        return (int) $this->dataProvider->getVehicleSeatCount();
130
    }
131
132
    /**
133
     * Get an array of random vehicle properties
134
     *
135
     *
136 2
     * @throws Exception
137
     */
138 2
    public function vehicleProperties(int $count = 0): array
139
    {
140
        return $this->dataProvider->getVehicleProperties($count);
141
    }
142
143
    /**
144
     * Get random vehicle gearbox type
145
     *
146
     * @return mixed
147 2
     *
148
     * @throws Exception
149 2
     */
150
    public function vehicleGearBoxType(): string
151
    {
152
        return $this->dataProvider->getVehicleGearBoxType();
153
    }
154
155
    /**
156
     * Get random vehicle gearbox type without unit
157
     *
158
     * @return mixed
159
     *
160 2
     * @throws Exception
161
     */
162 2
    public function vehicleGearBoxTypeValue(): string
163
    {
164
        return $this->dataProvider->getVehicleGearBoxType();
165
    }
166
167
    /**
168
     * Get engine torque
169
     *
170
     * @return mixed
171 1
     *
172
     * @throws Exception
173 1
     */
174
    public function vehicleEngineTorque(): string
175
    {
176
        return $this->dataProvider->getVehicleEngineTorque();
0 ignored issues
show
Bug introduced by
The method getVehicleEngineTorque() does not exist on Faker\Provider\FakeCarDataProviderInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Faker\Provider\FakeCarDataProviderInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

176
        return $this->dataProvider->/** @scrutinizer ignore-call */ getVehicleEngineTorque();
Loading history...
177
    }
178
179
    /**
180
     * Get engine torque without unit
181
     *
182
     * @return mixed
183 1
     *
184
     * @throws Exception
185 1
     */
186
    public function vehicleEngineTorqueValue(): string
187
    {
188
        return $this->dataProvider->getVehicleEngineTorque();
189
    }
190
191
    /**
192
     * Get engine power (horsepower or kilowatts)
193
     *
194 1
     * @return mixed
195
     *
196 1
     * @throws Exception
197
     */
198
    public function vehicleEnginePower(): string
199
    {
200
        return $this->dataProvider->getVehicleEnginePower();
0 ignored issues
show
Bug introduced by
The method getVehicleEnginePower() does not exist on Faker\Provider\FakeCarDataProviderInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Faker\Provider\FakeCarDataProviderInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

200
        return $this->dataProvider->/** @scrutinizer ignore-call */ getVehicleEnginePower();
Loading history...
201
    }
202
203
    /**
204
     * Get engine power without unit
205 2
     *
206
     * @return mixed
207 2
     *
208
     * @throws Exception
209
     */
210
    public function vehicleEnginePowerValue(): string
211
    {
212
        $this->isSupported(__FUNCTION__);
213
214
        return $this->dataProvider->getVehicleEnginePowerValue();
0 ignored issues
show
Bug introduced by
The method getVehicleEnginePowerValue() does not exist on Faker\Provider\FakeCarDataProviderInterface. It seems like you code against a sub-type of Faker\Provider\FakeCarDataProviderInterface such as Faker\Provider\FakeCarDataProvider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

214
        return $this->dataProvider->/** @scrutinizer ignore-call */ getVehicleEnginePowerValue();
Loading history...
215 3
    }
216
217 3
    public function isSupported($method): bool
218
    {
219
        if (method_exists($this->dataProvider, $method)) {
220
            return true;
221
        }
222
223
        throw new \RuntimeException('Method not supported be data provider. Please implement '.$method.'() in your data provider.');
224
    }
225 2
226
    public static function modelYear(int $year = 1980): string
227 2
    {
228 2
        return substr(self::MODEL_YEAR, ($year - 1980) % 30, 1);
229 2
    }
230 2
231 2
    private static function transliterate(string $character): string
232 2
    {
233
        return stripos(self::EBCDIC, $character) % 10;
234 2
    }
235
236
    /**php
237
     * @param string $vin
238
     *
239
     * @return mixed
240
     */
241
    private static function checkDigit(string $vin): string
242 2
    {
243
        $map     = '0123456789X';
244 2
        $weights = '8765432X098765432';
245 1
        $sum     = 0;
246
        for ($i = 0; $i < 17; $i++) {
247
            $sum += self::transliterate(substr($vin, $i, 1))
248 2
                    * stripos($map, $weights[$i]);
249
        }
250
251
        return $map[$sum % 11];
252
    }
253
254
    public static function validateVin(string $vin): bool
255
    {
256
        if (strlen($vin) !== 17) {
257
            return false;
258
        }
259
260
        return self::checkDigit($vin) == $vin[8];
261
    }
262
}
263