Conditions | 1 |
Paths | 1 |
Total Lines | 63 |
Code Lines | 24 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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; |
||
44 | public function run() |
||
45 | { |
||
46 | // Validation rules for input are |
||
47 | // - `email` must be a string and a valid email value (as FILTER_VALIDATE_EMAIL describes) |
||
48 | // - `first_name` required in input, must be a string with length from 1 to 255 characters |
||
49 | // - `last_name` could be either `null` or if given it must be a string with length from 1 to 255 characters |
||
50 | // - `payment_plan` must be a valid index for data in database (we will emulate request to database) |
||
51 | // - `interests` must be an array of non-empty strings (any number of items, no limit for max length) |
||
52 | |||
53 | $invalidInput = [ |
||
54 | 'email' => 'john.dow', |
||
55 | //'first_name' => 'John', |
||
1 ignored issue
–
show
|
|||
56 | 'last_name' => '', |
||
57 | 'payment_plan' => 123, |
||
58 | 'interests' => ['leisure', false, 'php', 321], |
||
59 | ]; |
||
60 | |||
61 | $validInput = [ |
||
62 | 'email' => '[email protected]', |
||
63 | 'first_name' => 'John', |
||
64 | 'last_name' => null, |
||
65 | 'payment_plan' => 2, |
||
66 | 'interests' => ['leisure', 'php', 'programming'], |
||
67 | ]; |
||
68 | |||
69 | // Having app specific rules separated makes the code easier to read and reuse. |
||
70 | // Though you can have the rules in-lined. |
||
71 | |||
72 | $rules = [ |
||
73 | 'email' => v::isEmail(), |
||
74 | 'first_name' => v::isRequiredString(255), |
||
75 | 'last_name' => v::isNullOrNonEmptyString(255), |
||
76 | 'payment_plan' => v::isExistingPaymentPlan(), |
||
77 | 'interests' => v::isListOfStrings(), |
||
78 | ]; |
||
79 | |||
80 | $this->console('Invalid data' . PHP_EOL); |
||
81 | $this->printErrors( |
||
82 | v::validator(v::arrayX($rules))->validate($invalidInput) |
||
83 | ); |
||
84 | |||
85 | $this->console(PHP_EOL . 'Valid data' . PHP_EOL); |
||
86 | $this->printErrors( |
||
87 | v::validator(v::arrayX($rules))->validate($validInput) |
||
88 | ); |
||
89 | |||
90 | // Note that error message placeholders like `first_name` are replaced with |
||
91 | // more readable such as `First Name` and others. |
||
92 | // |
||
93 | // The output would be |
||
94 | // ------------------------------------------------------------------------------------------------------- |
||
95 | // Invalid data |
||
96 | // Param `email` failed for `john.dow` with: The `Email address` value should be a valid email address. |
||
97 | // Param `last_name` failed for `` with: The `Last Name` value should be between 1 and 255 characters. |
||
98 | // Param `payment_plan` failed for `123` with: The `Payment plan` value should be an existing payment plan. |
||
99 | // Param `interests` failed for `` with: The `Interests` value should be a string. |
||
100 | // Param `interests` failed for `321` with: The `Interests` value should be a string. |
||
101 | // Param `first_name` failed for `` with: The `First Name` value is required. |
||
102 | // |
||
103 | // Valid data |
||
104 | // No errors |
||
105 | // ------------------------------------------------------------------------------------------------------- |
||
106 | } |
||
107 | |||
141 |
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.