| Conditions | 20 |
| Paths | 132 |
| Total Lines | 87 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 82 | private static function findClasses($path) |
||
| 83 | { |
||
| 84 | $extraTypes = PHP_VERSION_ID < 50400 ? '' : '|trait'; |
||
| 85 | if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.3', '>=')) { |
||
| 86 | $extraTypes .= '|enum'; |
||
| 87 | } |
||
| 88 | |||
| 89 | // Use @ here instead of Silencer to actively suppress 'unhelpful' output |
||
| 90 | // @link https://github.com/composer/composer/pull/4886 |
||
| 91 | $contents = @php_strip_whitespace($path); |
||
| 92 | if (!$contents) { |
||
| 93 | if (!file_exists($path)) { |
||
| 94 | $message = 'File at "%s" does not exist, check your classmap definitions'; |
||
| 95 | } elseif (!is_readable($path)) { |
||
| 96 | $message = 'File at "%s" is not readable, check its permissions'; |
||
| 97 | } elseif ('' === trim(file_get_contents($path))) { |
||
| 98 | // The input file was really empty and thus contains no classes |
||
| 99 | return array(); |
||
| 100 | } else { |
||
| 101 | $message = 'File at "%s" could not be parsed as PHP, it may be binary or corrupted'; |
||
| 102 | } |
||
| 103 | $error = error_get_last(); |
||
| 104 | if (isset($error['message'])) { |
||
| 105 | $message .= PHP_EOL . 'The following message may be helpful:' . PHP_EOL . $error['message']; |
||
| 106 | } |
||
| 107 | throw new \RuntimeException(sprintf($message, $path)); |
||
| 108 | } |
||
| 109 | |||
| 110 | // return early if there is no chance of matching anything in this file |
||
| 111 | if (!preg_match('{\b(?:class|interface' . $extraTypes . ')\s}i', $contents)) { |
||
| 112 | return array(); |
||
| 113 | } |
||
| 114 | |||
| 115 | // strip heredocs/nowdocs |
||
| 116 | $contents = preg_replace('{<<<\s*(\'?)(\w+)\\1(?:\r\n|\n|\r)(?:.*?)(?:\r\n|\n|\r)\\2(?=\r\n|\n|\r|;)}s', 'null', $contents); |
||
| 117 | // strip strings |
||
| 118 | $contents = preg_replace('{"[^"\\\\]*+(\\\\.[^"\\\\]*+)*+"|\'[^\'\\\\]*+(\\\\.[^\'\\\\]*+)*+\'}s', 'null', $contents); |
||
| 119 | // strip leading non-php code if needed |
||
| 120 | if (substr($contents, 0, 2) !== '<?') { |
||
| 121 | $contents = preg_replace('{^.+?<\?}s', '<?', $contents, 1, $replacements); |
||
| 122 | if ($replacements === 0) { |
||
| 123 | return array(); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | // strip non-php blocks in the file |
||
| 127 | $contents = preg_replace('{\?>.+<\?}s', '?><?', $contents); |
||
| 128 | // strip trailing non-php code if needed |
||
| 129 | $pos = strrpos($contents, '?>'); |
||
| 130 | if (false !== $pos && false === strpos(substr($contents, $pos), '<?')) { |
||
| 131 | $contents = substr($contents, 0, $pos); |
||
| 132 | } |
||
| 133 | |||
| 134 | preg_match_all('{ |
||
| 135 | (?: |
||
| 136 | \b(?<![\$:>])(?P<type>class|interface' . $extraTypes . ') \s++ (?P<name>[a-zA-Z_\x7f-\xff:][a-zA-Z0-9_\x7f-\xff:\-]*+) |
||
| 137 | | \b(?<![\$:>])(?P<ns>namespace) (?P<nsname>\s++[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\s*+\\\\\s*+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+)? \s*+ [\{;] |
||
| 138 | ) |
||
| 139 | }ix', $contents, $matches); |
||
| 140 | |||
| 141 | $classes = array(); |
||
| 142 | $namespace = ''; |
||
| 143 | |||
| 144 | for ($i = 0, $len = count($matches['type']); $i < $len; $i++) { |
||
| 145 | if (!empty($matches['ns'][$i])) { |
||
| 146 | $namespace = str_replace(array(' ', "\t", "\r", "\n"), '', $matches['nsname'][$i]) . '\\'; |
||
| 147 | } else { |
||
| 148 | $name = $matches['name'][$i]; |
||
| 149 | // skip anon classes extending/implementing |
||
| 150 | if ($name === 'extends' || $name === 'implements') { |
||
| 151 | continue; |
||
| 152 | } |
||
| 153 | if ($name[0] === ':') { |
||
| 154 | // This is an XHP class, https://github.com/facebook/xhp |
||
| 155 | $name = 'xhp' . substr(str_replace(array('-', ':'), array('_', '__'), $name), 1); |
||
| 156 | } elseif ($matches['type'][$i] === 'enum') { |
||
| 157 | // In Hack, something like: |
||
| 158 | // enum Foo: int { HERP = '123'; } |
||
| 159 | // The regex above captures the colon, which isn't part of |
||
| 160 | // the class name. |
||
| 161 | $name = rtrim($name, ':'); |
||
| 162 | } |
||
| 163 | $classes[] = ltrim($namespace . $name, '\\'); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | return $classes; |
||
| 168 | } |
||
| 169 | } |
||
| 170 |