Conditions | 23 |
Paths | 109 |
Total Lines | 71 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Tests | 28 |
CRAP Score | 28.8034 |
Changes | 4 | ||
Bugs | 3 | 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 |
||
4896 | 2 | public static function range( |
|
4897 | $var1, |
||
4898 | $var2, |
||
4899 | bool $use_ctype = true, |
||
4900 | string $encoding = 'UTF-8', |
||
4901 | $step = 1 |
||
4902 | ): array { |
||
4903 | 2 | if (!$var1 || !$var2) { |
|
4904 | 2 | return []; |
|
4905 | } |
||
4906 | |||
4907 | 2 | if ($step !== 1) { |
|
4908 | /** |
||
4909 | * @psalm-suppress RedundantConditionGivenDocblockType |
||
4910 | * @psalm-suppress DocblockTypeContradiction |
||
4911 | */ |
||
4912 | 1 | if (!\is_numeric($step)) { |
|
4913 | throw new \InvalidArgumentException('$step need to be a number, type given: ' . \gettype($step)); |
||
4914 | } |
||
4915 | |||
4916 | /** |
||
4917 | * @psalm-suppress RedundantConditionGivenDocblockType - false-positive from psalm? |
||
4918 | */ |
||
4919 | 1 | if ($step <= 0) { |
|
4920 | throw new \InvalidArgumentException('$step need to be a positive number, given: ' . $step); |
||
4921 | } |
||
4922 | } |
||
4923 | |||
4924 | 2 | if ($use_ctype && self::$SUPPORT['ctype'] === false) { |
|
4925 | throw new \RuntimeException('ext-ctype: is not installed'); |
||
4926 | } |
||
4927 | |||
4928 | 2 | $is_digit = false; |
|
4929 | 2 | $is_xdigit = false; |
|
4930 | |||
4931 | 2 | if ($use_ctype && \ctype_digit((string) $var1) && \ctype_digit((string) $var2)) { |
|
4932 | 2 | $is_digit = true; |
|
4933 | 2 | $start = (int) $var1; |
|
4934 | 2 | } elseif ($use_ctype && \ctype_xdigit($var1) && \ctype_xdigit($var2)) { |
|
4935 | $is_xdigit = true; |
||
4936 | $start = (int) self::hex_to_int((string) $var1); |
||
4937 | 2 | } elseif (!$use_ctype && \is_numeric($var1)) { |
|
4938 | 1 | $start = (int) $var1; |
|
4939 | } else { |
||
4940 | 2 | $start = self::ord((string) $var1); |
|
4941 | } |
||
4942 | |||
4943 | 2 | if (!$start) { |
|
4944 | return []; |
||
4945 | } |
||
4946 | |||
4947 | 2 | if ($is_digit) { |
|
4948 | 2 | $end = (int) $var2; |
|
4949 | 2 | } elseif ($is_xdigit) { |
|
4950 | $end = (int) self::hex_to_int((string) $var2); |
||
4951 | 2 | } elseif (!$use_ctype && \is_numeric($var2)) { |
|
4952 | 1 | $end = (int) $var2; |
|
4953 | } else { |
||
4954 | 2 | $end = self::ord((string) $var2); |
|
4955 | } |
||
4956 | |||
4957 | 2 | if (!$end) { |
|
4958 | return []; |
||
4959 | } |
||
4960 | |||
4961 | 2 | $array = []; |
|
4962 | 2 | foreach (\range($start, $end, $step) as $i) { |
|
4963 | 2 | $array[] = (string) self::chr((int) $i, $encoding); |
|
4964 | } |
||
4965 | |||
4966 | 2 | return $array; |
|
4967 | } |
||
13722 |