Conditions | 1 |
Paths | 1 |
Total Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php declare(strict_types=1); |
||
49 | public function run(): void |
||
50 | { |
||
51 | $validator = v::validator([ |
||
52 | 'sku' => r::required(r::sku()), |
||
53 | 'amount' => r::required(r::amount(5)), |
||
54 | 'delivery_date' => r::nullable(r::deliveryDate()), |
||
55 | 'email' => r::email(), |
||
56 | 'address1' => r::required(r::address1()), |
||
57 | 'address2' => r::address2(), |
||
58 | 'accepted' => r::required(r::areTermsAccepted()), |
||
59 | ]); |
||
60 | |||
61 | // Check with invalid data |
||
62 | $invalidInput = [ |
||
63 | 'sku' => '123', |
||
64 | 'amount' => '10', |
||
65 | 'delivery_date' => '2001-01-01', |
||
66 | 'email' => 'john.dow', |
||
67 | // 'address1' => 'Dow 1', // missed required parameter |
||
68 | 'accepted' => 'false', |
||
69 | ]; |
||
70 | $this->console('Invalid data (errors)' . PHP_EOL); |
||
71 | $validator->validate($invalidInput); |
||
72 | $this->printErrors($validator->getErrors()); |
||
|
|||
73 | $this->console('Invalid data (captures)' . PHP_EOL); |
||
74 | $this->printCaptures($validator->getCaptures()); |
||
75 | |||
76 | // Check with valid data |
||
77 | $validInput = [ |
||
78 | 'sku' => '1', |
||
79 | 'amount' => '3', |
||
80 | 'delivery_date' => (new DateTime('+2 days'))->format(DateTime::ISO8601), |
||
81 | 'email' => '[email protected]', |
||
82 | 'address1' => 'Dow 1', |
||
83 | 'address2' => null, |
||
84 | 'accepted' => 'true', |
||
85 | ]; |
||
86 | $this->console(PHP_EOL . 'Valid data (errors)' . PHP_EOL); |
||
87 | $validator->validate($validInput); |
||
88 | $this->printErrors($validator->getErrors()); |
||
89 | $this->console('Valid data (captures)' . PHP_EOL); |
||
90 | $this->printCaptures($validator->getCaptures()); |
||
91 | |||
92 | // The output would be |
||
93 | // ------------------------------------------------------------------------------------------------------- |
||
94 | // Invalid data (errors) |
||
95 | // Param `sku` failed for `123` with: The value should be a valid SKU. |
||
96 | // Param `amount` failed for `10` with: The value should be between 1 and 5. |
||
97 | // Param `delivery_date` failed for `2001-01-01` with: The value should be a valid date time. |
||
98 | // Param `email` failed for `john.dow` with: The value should be a valid email address. |
||
99 | // Param `accepted` failed for `` with: The value should be equal to 1. |
||
100 | // Param `address1` failed for `` with: The value is required. |
||
101 | // Invalid data (captures) |
||
102 | // No captures |
||
103 | |||
104 | // Valid data (errors) |
||
105 | // No errors |
||
106 | // Valid data (captures) |
||
107 | // `sku` = `1` (integer) |
||
108 | // `amount` = `3` (integer) |
||
109 | // `delivery_date` = `2018-01-04T15:07:33+0100` (object) |
||
110 | // `email` = `[email protected]` (string) |
||
111 | // `address1` = `Dow 1` (string) |
||
112 | // `address2` = `` (NULL) |
||
113 | // `accepted` = `1` (boolean) |
||
114 | // ------------------------------------------------------------------------------------------------------- |
||
115 | } |
||
116 | |||
176 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: