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; |
||
61 | private function showSingleValueValidation(): void |
||
62 | { |
||
63 | $this->console('Basic usage sample.' . PHP_EOL); |
||
64 | $this->console('===================' . PHP_EOL); |
||
65 | |||
66 | // Let's build a rule that validates an input to be either `null` or a string from 5 to 10 characters. |
||
67 | $validator = v::validator( |
||
68 | r::nullable(r::isString(r::stringLengthBetween(5, 10))) |
||
69 | ); |
||
70 | |||
71 | // let's try validation with valid input |
||
72 | $input = null; |
||
73 | View Code Duplication | if ($validator->validate($input) === true) { |
|
|
|||
74 | $this->console("Validation OK for `null`." . PHP_EOL); |
||
75 | } else { |
||
76 | assert(false, 'We should not be here.'); |
||
77 | } |
||
78 | // another one |
||
79 | $input = 'Hello'; |
||
80 | View Code Duplication | if ($validator->validate($input) === true) { |
|
81 | $this->console("Validation OK for `$input`." . PHP_EOL); |
||
82 | } else { |
||
83 | assert(false, 'We should not be here.'); |
||
84 | } |
||
85 | // this one should not pass the validation |
||
86 | $input = 'This string is too long.'; |
||
87 | if ($validator->validate($input) === false) { |
||
88 | $this->console("Input `$input` has not passed validation." . PHP_EOL); |
||
89 | $this->printErrors($validator->getErrors()); |
||
90 | } else { |
||
91 | assert(false, 'We should not be here.'); |
||
92 | } |
||
93 | |||
94 | // next example demonstrates |
||
95 | // - parsing strings as dates |
||
96 | // - validation for dates |
||
97 | // - data capture so you don't need to parse the input second time after validation |
||
98 | $fromDate = new DateTime('2001-02-03'); |
||
99 | $toDate = new DateTime('2001-04-05'); |
||
100 | $validator = v::validator( |
||
101 | r::isString(r::stringToDateTime(DATE_ATOM, r::between($fromDate, $toDate))) |
||
102 | ->setName('my_date')->enableCapture() |
||
103 | ); |
||
104 | $input = '2001-03-04T05:06:07+08:00'; |
||
105 | if ($validator->validate($input) === true) { |
||
106 | $this->console("Validation OK for `$input`." . PHP_EOL); |
||
107 | $myDate = $validator->getCaptures()->get()['my_date']; |
||
108 | // note that captured date is already DateTime |
||
109 | assert($myDate instanceof DateTimeInterface); |
||
110 | } else { |
||
111 | assert(false, 'We should not be here.'); |
||
112 | } |
||
113 | |||
114 | $this->console(PHP_EOL . PHP_EOL . PHP_EOL); |
||
115 | |||
116 | // The output would be |
||
117 | // ------------------------------------------------------------------------------------------------------- |
||
118 | // Basic usage sample. |
||
119 | // =================== |
||
120 | // Validation OK for `null`. |
||
121 | // Validation OK for `Hello`. |
||
122 | // Input `This string is too long to pass validation.` has not passed validation. |
||
123 | // Validation failed for `This string is too long.` with: The value should be between 5 and 10 characters. |
||
124 | // Validation OK for `2001-03-04T05:06:07+08:00`. |
||
125 | // ------------------------------------------------------------------------------------------------------- |
||
126 | } |
||
127 | |||
262 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.