Conditions | 16 |
Paths | 42 |
Total Lines | 146 |
Code Lines | 82 |
Lines | 0 |
Ratio | 0 % |
Tests | 85 |
CRAP Score | 16 |
Changes | 3 | ||
Bugs | 0 | Features | 1 |
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 |
||
7047 | public static function str_pad( |
||
7048 | string $str, |
||
7049 | int $pad_length, |
||
7050 | string $pad_string = ' ', |
||
7051 | $pad_type = \STR_PAD_RIGHT, |
||
7052 | string $encoding = 'UTF-8' |
||
7053 | ): string { |
||
7054 | 41 | if ($pad_length === 0 || $pad_string === '') { |
|
7055 | 1 | return $str; |
|
7056 | } |
||
7057 | |||
7058 | 41 | if ($pad_type !== (int) $pad_type) { |
|
7059 | 13 | if ($pad_type === 'left') { |
|
7060 | 3 | $pad_type = \STR_PAD_LEFT; |
|
7061 | 10 | } elseif ($pad_type === 'right') { |
|
7062 | 6 | $pad_type = \STR_PAD_RIGHT; |
|
7063 | 4 | } elseif ($pad_type === 'both') { |
|
7064 | 3 | $pad_type = \STR_PAD_BOTH; |
|
7065 | } else { |
||
7066 | 1 | throw new \InvalidArgumentException( |
|
7067 | 1 | 'Pad expects $pad_type to be "STR_PAD_*" or ' . "to be one of 'left', 'right' or 'both'" |
|
7068 | ); |
||
7069 | } |
||
7070 | } |
||
7071 | |||
7072 | 40 | if ($encoding === 'UTF-8') { |
|
7073 | 25 | $str_length = (int) \mb_strlen($str); |
|
7074 | |||
7075 | 25 | if ($pad_length >= $str_length) { |
|
7076 | switch ($pad_type) { |
||
7077 | 25 | case \STR_PAD_LEFT: |
|
7078 | 8 | $ps_length = (int) \mb_strlen($pad_string); |
|
7079 | |||
7080 | 8 | $diff = ($pad_length - $str_length); |
|
7081 | |||
7082 | 8 | $pre = (string) \mb_substr( |
|
7083 | 8 | \str_repeat($pad_string, (int) \ceil($diff / $ps_length)), |
|
7084 | 8 | 0, |
|
7085 | 8 | $diff |
|
7086 | ); |
||
7087 | 8 | $post = ''; |
|
7088 | |||
7089 | 8 | break; |
|
7090 | |||
7091 | 20 | case \STR_PAD_BOTH: |
|
7092 | 14 | $diff = ($pad_length - $str_length); |
|
7093 | |||
7094 | 14 | $ps_length_left = (int) \floor($diff / 2); |
|
7095 | |||
7096 | 14 | $ps_length_right = (int) \ceil($diff / 2); |
|
7097 | |||
7098 | 14 | $pre = (string) \mb_substr( |
|
7099 | 14 | \str_repeat($pad_string, $ps_length_left), |
|
7100 | 14 | 0, |
|
7101 | 14 | $ps_length_left |
|
7102 | ); |
||
7103 | 14 | $post = (string) \mb_substr( |
|
7104 | 14 | \str_repeat($pad_string, $ps_length_right), |
|
7105 | 14 | 0, |
|
7106 | 14 | $ps_length_right |
|
7107 | ); |
||
7108 | |||
7109 | 14 | break; |
|
7110 | |||
7111 | 9 | case \STR_PAD_RIGHT: |
|
7112 | default: |
||
7113 | 9 | $ps_length = (int) \mb_strlen($pad_string); |
|
7114 | |||
7115 | 9 | $diff = ($pad_length - $str_length); |
|
7116 | |||
7117 | 9 | $post = (string) \mb_substr( |
|
7118 | 9 | \str_repeat($pad_string, (int) \ceil($diff / $ps_length)), |
|
7119 | 9 | 0, |
|
7120 | 9 | $diff |
|
7121 | ); |
||
7122 | 9 | $pre = ''; |
|
7123 | } |
||
7124 | |||
7125 | 25 | return $pre . $str . $post; |
|
7126 | } |
||
7127 | |||
7128 | 3 | return $str; |
|
7129 | } |
||
7130 | |||
7131 | 15 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
|
7132 | |||
7133 | 15 | $str_length = (int) self::strlen($str, $encoding); |
|
7134 | |||
7135 | 15 | if ($pad_length >= $str_length) { |
|
7136 | switch ($pad_type) { |
||
7137 | 14 | case \STR_PAD_LEFT: |
|
7138 | 5 | $ps_length = (int) self::strlen($pad_string, $encoding); |
|
7139 | |||
7140 | 5 | $diff = ($pad_length - $str_length); |
|
7141 | |||
7142 | 5 | $pre = (string) self::substr( |
|
7143 | 5 | \str_repeat($pad_string, (int) \ceil($diff / $ps_length)), |
|
7144 | 5 | 0, |
|
7145 | 5 | $diff, |
|
7146 | 5 | $encoding |
|
7147 | ); |
||
7148 | 5 | $post = ''; |
|
7149 | |||
7150 | 5 | break; |
|
7151 | |||
7152 | 9 | case \STR_PAD_BOTH: |
|
7153 | 3 | $diff = ($pad_length - $str_length); |
|
7154 | |||
7155 | 3 | $ps_length_left = (int) \floor($diff / 2); |
|
7156 | |||
7157 | 3 | $ps_length_right = (int) \ceil($diff / 2); |
|
7158 | |||
7159 | 3 | $pre = (string) self::substr( |
|
7160 | 3 | \str_repeat($pad_string, $ps_length_left), |
|
7161 | 3 | 0, |
|
7162 | 3 | $ps_length_left, |
|
7163 | 3 | $encoding |
|
7164 | ); |
||
7165 | 3 | $post = (string) self::substr( |
|
7166 | 3 | \str_repeat($pad_string, $ps_length_right), |
|
7167 | 3 | 0, |
|
7168 | 3 | $ps_length_right, |
|
7169 | 3 | $encoding |
|
7170 | ); |
||
7171 | |||
7172 | 3 | break; |
|
7173 | |||
7174 | 6 | case \STR_PAD_RIGHT: |
|
7175 | default: |
||
7176 | 6 | $ps_length = (int) self::strlen($pad_string, $encoding); |
|
7177 | |||
7178 | 6 | $diff = ($pad_length - $str_length); |
|
7179 | |||
7180 | 6 | $post = (string) self::substr( |
|
7181 | 6 | \str_repeat($pad_string, (int) \ceil($diff / $ps_length)), |
|
7182 | 6 | 0, |
|
7183 | 6 | $diff, |
|
7184 | 6 | $encoding |
|
7185 | ); |
||
7186 | 6 | $pre = ''; |
|
7187 | } |
||
7188 | |||
7189 | 14 | return $pre . $str . $post; |
|
7190 | } |
||
7191 | |||
7192 | 1 | return $str; |
|
7193 | } |
||
13723 |