Passed
Push — main ( ebe7df...9489b3 )
by Peter
02:53
created

FakeCar::vehicleEnginePower()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
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
    /**
178
     * Get engine torque
179
     *
180
     * @return mixed
181
     * @throws Exception
182
     */
183 1
    public function vehicleEngineTorque(): string
184
    {
185 1
        return $this->dataProvider->getVehicleEngineTorque();
186
    }
187
188
    /**
189
     * Get engine power (horsepower or kilowatts)
190
     *
191
     * @return mixed
192
     * @throws Exception
193
     */
194 1
    public function vehicleEnginePower(): string
195
    {
196 1
        return $this->dataProvider->getVehicleEnginePower();
197
    }
198
199
200
    /**
201
     * @param int $year
202
     *
203
     * @return string
204
     */
205 2
    public static function modelYear(int $year = 1980): string
206
    {
207 2
        return substr(self::MODEL_YEAR, ($year - 1980) % 30, 1);
208
    }
209
210
    /**
211
     * @param string $character
212
     *
213
     * @return string
214
     */
215 3
    private static function transliterate(string $character): string
216
    {
217 3
        return stripos(self::EBCDIC, $character) % 10;
218
    }
219
220
    /**php
221
     * @param string $vin
222
     *
223
     * @return mixed
224
     */
225 2
    private static function checkDigit(string $vin): string
226
    {
227 2
        $map = "0123456789X";
228 2
        $weights = "8765432X098765432";
229 2
        $sum = 0;
230 2
        for ($i=0; $i < 17; $i++) {
231 2
            $sum += self::transliterate(substr($vin, $i, 1))
232 2
                    * stripos($map, $weights[$i]);
233
        }
234 2
        return $map[$sum % 11];
235
    }
236
237
    /**
238
     * @param $vin
239
     *
240
     * @return bool
241
     */
242 2
    public static function validateVin(string $vin): bool
243
    {
244 2
        if (strlen($vin) !== 17) {
245 1
            return false;
246
        }
247
248 2
        return self::checkDigit($vin) == $vin[8];
249
    }
250
}
251