Conditions | 3 |
Paths | 2 |
Total Lines | 62 |
Code Lines | 2 |
Lines | 0 |
Ratio | 0 % |
Tests | 8 |
CRAP Score | 3 |
Changes | 3 | ||
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
35 | public function __construct( |
||
36 | /** |
||
37 | * @var string The regular expression used to validate the value. |
||
38 | * The pattern may contain a `{schemes}` token that will be replaced |
||
39 | * by a regular expression which represents the {@see $schemes}. |
||
40 | * |
||
41 | * Note that if you want to reuse the pattern in HTML5 input, it should have `^` and `$`, should not have any |
||
42 | * modifiers, and should not be case-insensitive. |
||
43 | */ |
||
44 | private string $pattern = '/^{schemes}:\/\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(?::\d{1,5})?([?\/#].*$|$)/', |
||
45 | /** |
||
46 | * @var string[] List of URI schemes which should be considered valid. By default, http and https |
||
47 | * are considered to be valid schemes. |
||
48 | */ |
||
49 | private array $validSchemes = ['http', 'https'], |
||
50 | /** |
||
51 | * @var bool Whether validation process should take into account IDN (internationalized |
||
52 | * domain names). Defaults to `false` meaning that validation of URLs containing IDN will always |
||
53 | * fail. Note that in order to use IDN validation you have to install and enable `intl` PHP |
||
54 | * extension, otherwise an exception would be thrown. |
||
55 | */ |
||
56 | private bool $enableIDN = false, |
||
57 | /** |
||
58 | * @var string A message used when the input is incorrect. |
||
59 | * |
||
60 | * You may use the following placeholders in the message: |
||
61 | * |
||
62 | * - `{attribute}`: the label of the attribute being validated. |
||
63 | * - `{value}`: the value of the attribute being validated. |
||
64 | */ |
||
65 | private string $incorrectInputMessage = 'The value must have a string type.', |
||
66 | /** |
||
67 | * @var string A message used when the value is not valid. |
||
68 | * |
||
69 | 4 | * You may use the following placeholders in the message: |
|
70 | * |
||
71 | * - `{attribute}`: the label of the attribute being validated. |
||
72 | * - `{value}`: the value of the attribute being validated. |
||
73 | */ |
||
74 | private string $message = 'This value is not a valid URL.', |
||
75 | /** |
||
76 | * @var bool|callable|null Whether to skip this rule if the value validated is empty. |
||
77 | 1 | * |
|
78 | * @see SkipOnEmptyInterface |
||
79 | 1 | */ |
|
80 | private $skipOnEmpty = null, |
||
81 | /** |
||
82 | 44 | * @var bool Whether to skip this rule if any of the previous rules gave an error. |
|
83 | */ |
||
84 | 44 | private bool $skipOnError = false, |
|
85 | /** |
||
86 | * @var Closure|null A callable to define a condition for applying the rule. |
||
87 | * @psalm-var WhenType |
||
88 | * |
||
89 | * @see WhenInterface |
||
90 | 2 | */ |
|
91 | private Closure|null $when = null, |
||
92 | 2 | ) { |
|
93 | if ($enableIDN && !function_exists('idn_to_ascii')) { |
||
94 | // Tested via separate CI configuration (see ".github/workflows/build.yml"). |
||
95 | 40 | // @codeCoverageIgnoreStart |
|
96 | throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.'); |
||
97 | 40 | // @codeCoverageIgnoreEnd |
|
182 |