| Total Complexity | 40 |
| Total Lines | 234 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like PhpLintTask often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PhpLintTask, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class PhpLintTask extends Task |
||
| 39 | { |
||
| 40 | use FileSetAware; |
||
| 41 | use LogLevelAware; |
||
| 42 | |||
| 43 | protected $file; // the source file (from xml attribute) |
||
| 44 | |||
| 45 | protected $errorProperty; |
||
| 46 | protected $haltOnFailure = false; |
||
| 47 | protected $hasErrors = false; |
||
| 48 | protected $badFiles = []; |
||
| 49 | protected $interpreter = ''; // php interpreter to use for linting |
||
| 50 | |||
| 51 | protected $cache = null; |
||
| 52 | |||
| 53 | protected $tofile = null; |
||
| 54 | |||
| 55 | protected $deprecatedAsError = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Initialize the interpreter with the Phing property php.interpreter |
||
| 59 | */ |
||
| 60 | public function init() |
||
| 61 | { |
||
| 62 | $this->setInterpreter($this->project->getProperty('php.interpreter')); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Override default php interpreter |
||
| 67 | * |
||
| 68 | * @param string $sPhp |
||
| 69 | * @todo Do some sort of checking if the path is correct but would |
||
| 70 | * require traversing the systems executeable path too |
||
| 71 | */ |
||
| 72 | public function setInterpreter($sPhp) |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The haltonfailure property |
||
| 82 | * |
||
| 83 | * @param boolean $aValue |
||
| 84 | */ |
||
| 85 | public function setHaltOnFailure($aValue) |
||
| 86 | { |
||
| 87 | $this->haltOnFailure = $aValue; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * File to be performed syntax check on |
||
| 92 | * |
||
| 93 | * @param File $file |
||
| 94 | */ |
||
| 95 | public function setFile(File $file) |
||
| 96 | { |
||
| 97 | $this->file = $file; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Set an property name in which to put any errors. |
||
| 102 | * |
||
| 103 | * @param string $propname |
||
| 104 | */ |
||
| 105 | public function setErrorproperty($propname) |
||
| 106 | { |
||
| 107 | $this->errorProperty = $propname; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Whether to store last-modified times in cache |
||
| 112 | * |
||
| 113 | * @param File $file |
||
| 114 | */ |
||
| 115 | public function setCacheFile(File $file) |
||
| 116 | { |
||
| 117 | $this->cache = new DataStore($file); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * File to save error messages to |
||
| 122 | * |
||
| 123 | * @param File $tofile |
||
| 124 | * @internal param PhingFile $file |
||
| 125 | */ |
||
| 126 | public function setToFile(File $tofile) |
||
| 127 | { |
||
| 128 | $this->tofile = $tofile; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Sets whether to treat deprecated warnings (introduced in PHP 5.3) as errors |
||
| 133 | * |
||
| 134 | * @param boolean $deprecatedAsError |
||
| 135 | */ |
||
| 136 | public function setDeprecatedAsError($deprecatedAsError) |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Execute lint check against PhingFile or a FileSet |
||
| 143 | */ |
||
| 144 | public function main() |
||
| 145 | { |
||
| 146 | if (!isset($this->file) and count($this->filesets) == 0) { |
||
| 147 | throw new BuildException("Missing either a nested fileset or attribute 'file' set"); |
||
| 148 | } |
||
| 149 | |||
| 150 | if ($this->file instanceof File) { |
||
| 151 | $this->lint($this->file->getPath()); |
||
| 152 | } else { // process filesets |
||
| 153 | $project = $this->getProject(); |
||
| 154 | foreach ($this->filesets as $fs) { |
||
| 155 | $ds = $fs->getDirectoryScanner($project); |
||
| 156 | $files = $ds->getIncludedFiles(); |
||
| 157 | $dir = $fs->getDir($this->project)->getPath(); |
||
| 158 | foreach ($files as $file) { |
||
| 159 | $this->lint($dir . DIRECTORY_SEPARATOR . $file); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | // write list of 'bad files' to file (if specified) |
||
| 165 | if ($this->tofile) { |
||
| 166 | $writer = new FileWriter($this->tofile); |
||
| 167 | |||
| 168 | foreach ($this->badFiles as $file => $messages) { |
||
| 169 | foreach ($messages as $msg) { |
||
| 170 | $writer->write($file . "=" . $msg . PHP_EOL); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | $writer->close(); |
||
| 175 | } |
||
| 176 | |||
| 177 | $message = ''; |
||
| 178 | foreach ($this->badFiles as $file => $messages) { |
||
| 179 | foreach ($messages as $msg) { |
||
| 180 | $message .= $file . "=" . $msg . PHP_EOL; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | // save list of 'bad files' with errors to property errorproperty (if specified) |
||
| 185 | if ($this->errorProperty) { |
||
| 186 | $this->project->setProperty($this->errorProperty, $message); |
||
| 187 | } |
||
| 188 | |||
| 189 | if (!empty($this->cache)) { |
||
| 190 | $this->cache->commit(); |
||
| 191 | } |
||
| 192 | |||
| 193 | if ($this->haltOnFailure && $this->hasErrors) { |
||
| 194 | throw new BuildException('Syntax error(s) in PHP files: ' . $message); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Performs the actual syntax check |
||
| 200 | * |
||
| 201 | * @param string $file |
||
| 202 | * @throws BuildException |
||
| 203 | */ |
||
| 204 | protected function lint($file) |
||
| 272 | } |
||
| 273 | } |
||
| 274 | } |
||
| 275 | } |
||
| 276 |