Conditions | 33 |
Paths | 1822 |
Total Lines | 151 |
Code Lines | 66 |
Lines | 0 |
Ratio | 0 % |
Tests | 44 |
CRAP Score | 43.7175 |
Changes | 11 | ||
Bugs | 6 | Features | 2 |
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 |
||
9856 | 52 | public static function strpos( |
|
9857 | string $haystack, |
||
9858 | $needle, |
||
9859 | int $offset = 0, |
||
9860 | string $encoding = 'UTF-8', |
||
9861 | bool $clean_utf8 = false |
||
9862 | ) { |
||
9863 | 52 | if ($haystack === '') { |
|
9864 | 4 | if (\PHP_VERSION_ID >= 80000) { |
|
9865 | 4 | if ($needle === '') { |
|
9866 | 4 | return 0; |
|
9867 | } |
||
9868 | } else { |
||
9869 | return false; |
||
9870 | } |
||
9871 | } |
||
9872 | |||
9873 | // iconv and mbstring do not support integer $needle |
||
9874 | 52 | if ((int) $needle === $needle) { |
|
9875 | $needle = (string) self::chr($needle); |
||
9876 | } |
||
9877 | 52 | $needle = (string) $needle; |
|
9878 | |||
9879 | 52 | if ($haystack === '') { |
|
9880 | 2 | if (\PHP_VERSION_ID >= 80000 && $needle === '') { |
|
9881 | return 0; |
||
9882 | } |
||
9883 | |||
9884 | 2 | return false; |
|
9885 | } |
||
9886 | |||
9887 | 51 | if ($needle === '' && \PHP_VERSION_ID < 80000) { |
|
9888 | return false; |
||
9889 | } |
||
9890 | |||
9891 | 51 | if ($clean_utf8) { |
|
9892 | // "mb_strpos()" and "iconv_strpos()" returns wrong position, |
||
9893 | // if invalid characters are found in $haystack before $needle |
||
9894 | 3 | $needle = self::clean($needle); |
|
9895 | 3 | $haystack = self::clean($haystack); |
|
9896 | } |
||
9897 | |||
9898 | 51 | if ($encoding !== 'UTF-8' && $encoding !== 'CP850') { |
|
9899 | 10 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
|
9900 | } |
||
9901 | |||
9902 | // |
||
9903 | // fallback via mbstring |
||
9904 | // |
||
9905 | |||
9906 | 51 | if (self::$SUPPORT['mbstring'] === true) { |
|
9907 | 49 | if ($encoding === 'UTF-8') { |
|
9908 | /** @noinspection PhpUsageOfSilenceOperatorInspection - Offset not contained in string */ |
||
9909 | 49 | return @\mb_strpos($haystack, $needle, $offset); |
|
9910 | } |
||
9911 | |||
9912 | /** @noinspection PhpUsageOfSilenceOperatorInspection - Offset not contained in string */ |
||
9913 | 2 | return @\mb_strpos($haystack, $needle, $offset, $encoding); |
|
9914 | } |
||
9915 | |||
9916 | // |
||
9917 | // fallback for binary || ascii only |
||
9918 | // |
||
9919 | if ( |
||
9920 | 4 | $encoding === 'CP850' |
|
9921 | || |
||
9922 | 4 | $encoding === 'ASCII' |
|
9923 | ) { |
||
9924 | 2 | return \strpos($haystack, $needle, $offset); |
|
9925 | } |
||
9926 | |||
9927 | if ( |
||
9928 | 4 | $encoding !== 'UTF-8' |
|
9929 | && |
||
9930 | 4 | self::$SUPPORT['iconv'] === false |
|
9931 | && |
||
9932 | 4 | self::$SUPPORT['mbstring'] === false |
|
9933 | ) { |
||
9934 | /** |
||
9935 | * @psalm-suppress ImpureFunctionCall - is is only a warning |
||
9936 | */ |
||
9937 | 2 | \trigger_error('UTF8::strpos() without mbstring / iconv cannot handle "' . $encoding . '" encoding', \E_USER_WARNING); |
|
9938 | } |
||
9939 | |||
9940 | // |
||
9941 | // fallback via intl |
||
9942 | // |
||
9943 | |||
9944 | if ( |
||
9945 | 4 | $encoding === 'UTF-8' // INFO: "grapheme_strpos()" can't handle other encodings |
|
9946 | && |
||
9947 | 4 | $offset >= 0 // grapheme_strpos() can't handle negative offset |
|
9948 | && |
||
9949 | 4 | self::$SUPPORT['intl'] === true |
|
9950 | ) { |
||
9951 | $return_tmp = \grapheme_strpos($haystack, $needle, $offset); |
||
9952 | if ($return_tmp !== false) { |
||
9953 | return $return_tmp; |
||
9954 | } |
||
9955 | } |
||
9956 | |||
9957 | // |
||
9958 | // fallback via iconv |
||
9959 | // |
||
9960 | |||
9961 | if ( |
||
9962 | 4 | $offset >= 0 // iconv_strpos() can't handle negative offset |
|
9963 | && |
||
9964 | 4 | self::$SUPPORT['iconv'] === true |
|
9965 | ) { |
||
9966 | // ignore invalid negative offset to keep compatibility |
||
9967 | // with php < 5.5.35, < 5.6.21, < 7.0.6 |
||
9968 | $return_tmp = \iconv_strpos($haystack, $needle, $offset > 0 ? $offset : 0, $encoding); |
||
9969 | if ($return_tmp !== false) { |
||
9970 | return $return_tmp; |
||
9971 | } |
||
9972 | } |
||
9973 | |||
9974 | // |
||
9975 | // fallback for ascii only |
||
9976 | // |
||
9977 | |||
9978 | 4 | if (ASCII::is_ascii($haystack . $needle)) { |
|
9979 | /** @noinspection PhpUsageOfSilenceOperatorInspection - Offset not contained in string */ |
||
9980 | 2 | return @\strpos($haystack, $needle, $offset); |
|
9981 | } |
||
9982 | |||
9983 | // |
||
9984 | // fallback via vanilla php |
||
9985 | // |
||
9986 | |||
9987 | 4 | $haystack_tmp = self::substr($haystack, $offset, null, $encoding); |
|
9988 | 4 | if ($haystack_tmp === false) { |
|
9989 | $haystack_tmp = ''; |
||
9990 | } |
||
9991 | 4 | $haystack = (string) $haystack_tmp; |
|
9992 | |||
9993 | 4 | if ($offset < 0) { |
|
9994 | $offset = 0; |
||
9995 | } |
||
9996 | |||
9997 | 4 | $pos = \strpos($haystack, $needle); |
|
9998 | 4 | if ($pos === false) { |
|
9999 | 3 | return false; |
|
10000 | } |
||
10001 | |||
10002 | 4 | if ($pos) { |
|
10003 | 4 | return $offset + (int) self::strlen(\substr($haystack, 0, $pos), $encoding); |
|
10004 | } |
||
10005 | |||
10006 | 4 | return $offset + 0; |
|
10007 | } |
||
13694 |