Conditions | 5 |
Paths | 16 |
Total Lines | 66 |
Code Lines | 34 |
Lines | 10 |
Ratio | 15.15 % |
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 namespace Sample; |
||
59 | |||
60 | // Check with invalid data |
||
61 | $invalidInput = [ |
||
62 | 'sku' => '123', |
||
63 | 'amount' => '10', |
||
64 | 'delivery_date' => '2001-01-01', |
||
65 | 'email' => 'john.dow', |
||
66 | // 'address1' => 'Dow 1', // missed required parameter |
||
|
|||
67 | 'accepted' => 'false', |
||
68 | ]; |
||
69 | $this->console('Invalid data (errors)' . PHP_EOL); |
||
70 | $validator->validate($invalidInput); |
||
71 | $this->printErrors($validator->getErrors()); |
||
72 | $this->console('Invalid data (captures)' . PHP_EOL); |
||
73 | $this->printCaptures($validator->getCaptures()); |
||
74 | |||
75 | // Check with valid data |
||
76 | $validInput = [ |
||
77 | 'sku' => '1', |
||
78 | 'amount' => '3', |
||
79 | 'delivery_date' => (new DateTime('+2 days'))->format(DateTime::ISO8601), |
||
80 | 'email' => '[email protected]', |
||
81 | 'address1' => 'Dow 1', |
||
82 | 'address2' => null, |
||
83 | 'accepted' => 'true', |
||
84 | ]; |
||
85 | $this->console(PHP_EOL . 'Valid data (errors)' . PHP_EOL); |
||
86 | $validator->validate($validInput); |
||
87 | $this->printErrors($validator->getErrors()); |
||
88 | $this->console('Valid data (captures)' . PHP_EOL); |
||
89 | $this->printCaptures($validator->getCaptures()); |
||
90 | |||
91 | // The output would be |
||
92 | // ------------------------------------------------------------------------------------------------------- |
||
93 | // Invalid data (errors) |
||
94 | // Param `sku` failed for `123` with: The value should be a valid SKU. |
||
95 | // Param `amount` failed for `10` with: The value should be between 1 and 5. |
||
96 | // Param `delivery_date` failed for `2001-01-01` with: The value should be a valid date time. |
||
97 | // Param `email` failed for `john.dow` with: The value should be a valid email address. |
||
98 | // Param `accepted` failed for `` with: The value should be equal to 1. |
||
99 | // Param `address1` failed for `` with: The value is required. |
||
100 | // Invalid data (captures) |
||
101 | // No captures |
||
102 | |||
103 | // Valid data (errors) |
||
104 | // No errors |
||
105 | // Valid data (captures) |
||
106 | // `sku` = `1` (integer) |
||
107 | // `amount` = `3` (integer) |
||
108 | // `delivery_date` = `2018-01-04T15:07:33+0100` (object) |
||
109 | // `email` = `[email protected]` (string) |
||
110 | // `address1` = `Dow 1` (string) |
||
111 | // `address2` = `` (NULL) |
||
112 | // `accepted` = `1` (boolean) |
||
113 | // ------------------------------------------------------------------------------------------------------- |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param iterable $errors |
||
118 | * |
||
119 | * @return void |
||
120 | */ |
||
121 | private function printErrors(iterable $errors): void |
||
122 | { |
||
123 | $hasErrors = false; |
||
124 | |||
125 | foreach ($errors as $error) { |
||
175 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.