Test Failed
Pull Request — main (#30)
by Peter
20:35 queued 17:53
created

FakeCar::vehicleEnginePowerValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
ccs 5
cts 5
cp 1
crap 1
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
    protected const MODEL_YEAR = "ABCDEFGHJKLMNPRSTVWXY123456789";
12
13
    protected FakeCarDataProviderInterface $dataProvider;
14
15
    /**
16
     * @param Generator $generator
17
     */
18 19
    public function __construct(Generator $generator)
19
    {
20 19
        parent::__construct($generator);
21 19
        $this->setDataProvider(new FakeCarDataProvider());
22
    }
23
24 19
    public function setDataProvider(FakeCarDataProviderInterface $dataProvider): void
25
    {
26 19
        $this->dataProvider = $dataProvider;
27
    }
28
29
    /**
30
     * Get vehicle string with brand and model
31
     *
32
     * @return string
33
     */
34 2
    public function vehicle(): string
35
    {
36 2
        $vehicleBrand = $this->vehicleBrand();
37
38 2
        return $vehicleBrand.' '.$this->vehicleModel($vehicleBrand);
39
    }
40
41
    /**
42
     * Get vehicle with brand and model as an array
43
     *
44
     * @return array
45
     */
46 3
    public function vehicleArray(): array
47
    {
48 3
        $vehicleBrand = $this->vehicleBrand();
49
50 3
        return [
51 3
            'brand' => $vehicleBrand,
52 3
            'model' => $this->vehicleModel($vehicleBrand)
53 3
        ];
54
    }
55
56
    /**
57
     * Get random vehicle brand
58
     *
59
     * @return string
60
     */
61 6
    public function vehicleBrand(): string
62
    {
63 6
        return (string) $this->dataProvider->getVehicleBrand();
64
    }
65
66
    /**
67
     * Get random vehicle model
68
     *
69
     * @param string $brand Get random model from specific brand (optional)
70
     *
71
     * @return mixed
72
     */
73 5
    public function vehicleModel(string $brand = null): string
74
    {
75 5
        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

75
        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...
76
    }
77
78
    /**
79
     * Generate VIN
80
     * @link https://en.wikipedia.org/wiki/Vehicle_identification_number
81
     *
82
     * @param int $year
83
     *
84
     * @return mixed
85
     */
86 1
    public function vin(int $year = 1980): string
87
    {
88 1
        $modelYear = static::modelYear($year);
89 1
        $regex = "([a-hj-npr-z0-9]{8})_{$modelYear}([a-hj-npr-z0-9]{7})";
90 1
        $vin = static::regexify($regex);
91 1
        return str_replace('_', self::checkDigit($vin), $vin);
92
    }
93
94
    /**
95
     * Get vehicle registration number
96
     *
97
     * @param string $regex
98
     *
99
     * @return string
100
     */
101 1
    public function vehicleRegistration(string $regex = '[A-Z]{3}-[0-9]{3}'): string
102
    {
103
        //TODO: Set format based on locale
104 1
        return static::regexify($regex);
105
    }
106
107
    /**
108
     * Get vehicle type
109
     *
110
     * @return string
111
     * @throws Exception
112
     */
113 3
    public function vehicleType(): string
114
    {
115 3
        return (string) $this->dataProvider->getVehicleType();
116
    }
117
118
    /**
119
     * Get vehicle fuel type
120
     *
121
     * @param int $count
122
     *
123
     * @return string
124
     */
125 2
    public function vehicleFuelType(int $count = 1): string
126
    {
127 2
        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

127
        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...
128
    }
129
130
    /**
131
     * Get vehicle door count
132
     *
133
     * @return int
134
     * @throws Exception
135
     */
136 2
    public function vehicleDoorCount(): int
137
    {
138 2
        return (int) $this->dataProvider->getVehicleDoorCount();
139
    }
140
141
    /**
142
     * Get vehicle door count
143
     *
144
     * @return int
145
     * @throws Exception
146
     */
147 2
    public function vehicleSeatCount(): int
148
    {
149 2
        return (int) $this->dataProvider->getVehicleSeatCount();
150
    }
151
152
    /**
153
     * Get an array of random vehicle properties
154
     *
155
     * @param int $count
156
     *
157
     * @return array
158
     * @throws Exception
159
     */
160 2
    public function vehicleProperties(int $count = 0): array
161
    {
162 2
        return $this->dataProvider->getVehicleProperties($count);
163
    }
164
165
    /**
166
     * Get random vehicle gearbox type
167
     *
168
     * @return mixed
169
     * @throws Exception
170
     */
171 1
    public function vehicleGearBoxType(): string
172
    {
173 1
        return $this->dataProvider->getVehicleGearBoxType();
174
    }
175
176
    /**
177
     * Get random vehicle gearbox type without unit
178
     *
179
     * @return mixed
180
     * @throws Exception
181
     */
182
    public function vehicleGearBoxTypeValue(): string
183 1
    {
184
        return $this->dataProvider->getVehicleGearBoxType();
185 1
    }
186
187
188
    /**
189
     * Get engine torque
190
     *
191
     * @return mixed
192
     * @throws Exception
193
     */
194 1
    public function vehicleEngineTorque(): string
195
    {
196 1
        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

196
        return $this->dataProvider->/** @scrutinizer ignore-call */ getVehicleEngineTorque();
Loading history...
197
    }
198
199
    /**
200
     * Get engine torque without unit
201
     *
202
     * @return mixed
203
     * @throws Exception
204
     */
205 2
    public function vehicleEngineTorqueValue(): string
206
    {
207 2
        return $this->dataProvider->getVehicleEngineTorque();
208
    }
209
210
    /**
211
     * Get engine power (horsepower or kilowatts)
212
     *
213
     * @return mixed
214
     * @throws Exception
215 3
     */
216
    public function vehicleEnginePower(): string
217 3
    {
218
        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

218
        return $this->dataProvider->/** @scrutinizer ignore-call */ getVehicleEnginePower();
Loading history...
219
    }
220
221
    /**
222
     * Get engine power without unit
223
     *
224
     * @return mixed
225 2
     * @throws Exception
226
     */
227 2
    public function vehicleEnginePowerValue(): string
228 2
    {
229 2
        $this->isSupported(__FUNCTION__);
230 2
231 2
        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

231
        return $this->dataProvider->/** @scrutinizer ignore-call */ getVehicleEnginePowerValue();
Loading history...
232 2
    }
233
234 2
    public function isSupported($method): bool
235
    {
236
        if (method_exists($this->dataProvider, $method)) {
237
            return true;
238
        }
239
240
        throw new \RuntimeException('Method not supported be data provider. Please implement '.$method.'() in your data provider.');
241
    }
242 2
243
244 2
    /**
245 1
     * @param int $year
246
     *
247
     * @return string
248 2
     */
249
    public static function modelYear(int $year = 1980): string
250
    {
251
        return substr(self::MODEL_YEAR, ($year - 1980) % 30, 1);
252
    }
253
254
    /**
255
     * @param string $character
256
     *
257
     * @return string
258
     */
259
    private static function transliterate(string $character): string
260
    {
261
        return stripos(self::EBCDIC, $character) % 10;
262
    }
263
264
    /**php
265
     * @param string $vin
266
     *
267
     * @return mixed
268
     */
269
    private static function checkDigit(string $vin): string
270
    {
271
        $map = "0123456789X";
272
        $weights = "8765432X098765432";
273
        $sum = 0;
274
        for ($i=0; $i < 17; $i++) {
275
            $sum += self::transliterate(substr($vin, $i, 1))
276
                    * stripos($map, $weights[$i]);
277
        }
278
        return $map[$sum % 11];
279
    }
280
281
    /**
282
     * @param $vin
283
     *
284
     * @return bool
285
     */
286
    public static function validateVin(string $vin): bool
287
    {
288
        if (strlen($vin) !== 17) {
289
            return false;
290
        }
291
292
        return self::checkDigit($vin) == $vin[8];
293
    }
294
}
295