Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 27 | class HelpAsciiDocHandler |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | private $path; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $lessBinary; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var ExecutableFinder |
||
| 41 | */ |
||
| 42 | private $executableFinder; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var ProcessLauncher |
||
| 46 | */ |
||
| 47 | private $processLauncher; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Creates the handler. |
||
| 51 | * |
||
| 52 | * @param string $path The path to the AsciiDoc file. |
||
| 53 | * @param ExecutableFinder $executableFinder The finder used to find the |
||
|
|
|||
| 54 | * "less" binary. |
||
| 55 | * @param ProcessLauncher $processLauncher The launcher for executing the |
||
| 56 | * "less" binary. |
||
| 57 | */ |
||
| 58 | 17 | View Code Duplication | public function __construct($path, ExecutableFinder $executableFinder = null, ProcessLauncher $processLauncher = null) |
| 59 | { |
||
| 60 | 17 | Assert::file($path); |
|
| 61 | |||
| 62 | 17 | $this->path = $path; |
|
| 63 | 17 | $this->executableFinder = $executableFinder ?: new ExecutableFinder(); |
|
| 64 | 17 | $this->processLauncher = $processLauncher ?: new ProcessLauncher(); |
|
| 65 | 17 | } |
|
| 66 | |||
| 67 | /** |
||
| 68 | * {@inheritdoc} |
||
| 69 | */ |
||
| 70 | 15 | public function handle(Args $args, IO $io) |
|
| 71 | { |
||
| 72 | 15 | if ($this->processLauncher->isSupported()) { |
|
| 73 | 13 | if (!$this->lessBinary) { |
|
| 74 | 12 | $this->lessBinary = $this->executableFinder->find('less'); |
|
| 75 | } |
||
| 76 | |||
| 77 | 13 | if ($this->lessBinary) { |
|
| 78 | 12 | return $this->processLauncher->launchProcess( |
|
| 79 | 12 | escapeshellcmd($this->lessBinary).' %path%', |
|
| 80 | 12 | array('path' => $this->path), |
|
| 81 | 12 | false |
|
| 82 | ); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | 3 | $io->write(file_get_contents($this->path)); |
|
| 87 | |||
| 88 | 3 | return 0; |
|
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Returns the "less" binary used to display the AsciiDoc page. |
||
| 93 | * |
||
| 94 | * @return string The "less" binary or `null` if the binary is auto-detected. |
||
| 95 | */ |
||
| 96 | public function getLessBinary() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Sets the "less" binary used to display the AsciiDoc page. |
||
| 103 | * |
||
| 104 | * @param string $lessBinary The "less" binary to use. |
||
| 105 | */ |
||
| 106 | 12 | View Code Duplication | public function setLessBinary($lessBinary) |
| 115 | } |
||
| 116 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.