We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 14 |
| Paths | 41 |
| Total Lines | 61 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 27 |
| CRAP Score | 14 |
| Changes | 3 | ||
| Bugs | 1 | 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 |
||
| 110 | 10 | public function apply( array $tokens, Settings $settings, $is_title = false, \DOMText $textnode = null ) { |
|
| 111 | 10 | if ( empty( $settings[ Settings::URL_WRAP ] ) || empty( $settings[ Settings::URL_MIN_AFTER_WRAP ] ) ) { |
|
| 112 | 5 | return $tokens; |
|
| 113 | } |
||
| 114 | |||
| 115 | // Test for and parse urls. |
||
| 116 | 5 | foreach ( $tokens as $token_index => $text_token ) { |
|
| 117 | 5 | if ( \preg_match( $this->url_pattern, $text_token->value, $url_match ) ) { |
|
| 118 | |||
| 119 | // $url_match['schema'] holds "http://". |
||
| 120 | // $url_match['domain'] holds "subdomains.domain.tld". |
||
| 121 | // $url_match['path'] holds the path after the domain. |
||
| 122 | 5 | $http = ( $url_match['schema'] ) ? $url_match[1] . U::ZERO_WIDTH_SPACE : ''; |
|
| 123 | |||
| 124 | 5 | $domain_parts = \preg_split( self::WRAP_URLS_DOMAIN_PARTS, $url_match['domain'], -1, PREG_SPLIT_DELIM_CAPTURE ); |
|
| 125 | 5 | if ( false === $domain_parts ) { |
|
| 126 | // Should not happen. |
||
| 127 | continue; // @codeCoverageIgnore |
||
| 128 | } |
||
| 129 | |||
| 130 | // This is a hack, but it works. |
||
| 131 | // First, we hyphenate each part, we need it formated like a group of words. |
||
| 132 | 5 | $parsed_words_like = []; |
|
| 133 | 5 | foreach ( $domain_parts as $key => $part ) { |
|
| 134 | 5 | $parsed_words_like[ $key ] = new Text_Parser\Token( $part, Text_Parser\Token::OTHER ); |
|
| 135 | } |
||
| 136 | |||
| 137 | // Do the hyphenation. |
||
| 138 | 5 | $parsed_words_like = $this->do_hyphenate( $parsed_words_like, $settings, U::ZERO_WIDTH_SPACE ); |
|
| 139 | |||
| 140 | // Restore format. |
||
| 141 | 5 | foreach ( $parsed_words_like as $key => $parsed_word ) { |
|
| 142 | 5 | $value = $parsed_word->value; |
|
| 143 | |||
| 144 | 5 | if ( $key > 0 && 1 === \strlen( $value ) ) { |
|
| 145 | 5 | $domain_parts[ $key ] = U::ZERO_WIDTH_SPACE . $value; |
|
| 146 | } else { |
||
| 147 | 5 | $domain_parts[ $key ] = $value; |
|
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | // Lastly let's recombine. |
||
| 152 | 5 | $domain = \implode( '', $domain_parts ); |
|
| 153 | |||
| 154 | // Break up the URL path to individual characters. |
||
| 155 | 5 | $path_parts = \str_split( $url_match['path'], 1 ); |
|
| 156 | 5 | $path_count = \count( $path_parts ); |
|
| 157 | 5 | $path = ''; |
|
| 158 | 5 | foreach ( $path_parts as $index => $path_part ) { |
|
| 159 | 5 | if ( 0 === $index || $path_count - $index < $settings[ Settings::URL_MIN_AFTER_WRAP ] ) { |
|
| 160 | 5 | $path .= $path_part; |
|
| 161 | } else { |
||
| 162 | 1 | $path .= U::ZERO_WIDTH_SPACE . $path_part; |
|
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | 5 | $tokens[ $token_index ] = $text_token->with_value( $http . $domain . $path ); |
|
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | 5 | return $tokens; |
|
| 171 | } |
||
| 173 |