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 |
||
| 32 | abstract class ProjectAwareCommand extends Command { |
||
| 33 | /** |
||
| 34 | * Project filename. |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | const PROJECT_FILENAME = 'componentmgr.json'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Project lock filename. |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | const PROJECT_LOCK_FILENAME = 'componentmgr.lock.json'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Filesystem. |
||
| 49 | * |
||
| 50 | * @var \Symfony\Component\Filesystem\Filesystem |
||
| 51 | */ |
||
| 52 | protected $filesystem; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Logger. |
||
| 56 | * |
||
| 57 | * @var \Psr\Log\LoggerInterface |
||
| 58 | */ |
||
| 59 | protected $logger; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Moodle bridge. |
||
| 63 | * |
||
| 64 | * @var \ComponentManager\Moodle |
||
| 65 | */ |
||
| 66 | protected $moodle; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Package format factory. |
||
| 70 | * |
||
| 71 | * @var \ComponentManager\PackageFormat\PackageFormatFactory |
||
| 72 | */ |
||
| 73 | protected $packageFormatFactory; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Package repository factory. |
||
| 77 | * |
||
| 78 | * @var \ComponentManager\PackageRepository\PackageRepositoryFactory |
||
| 79 | */ |
||
| 80 | protected $packageRepositoryFactory; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Package source factory. |
||
| 84 | * |
||
| 85 | * @var \ComponentManager\PackageSource\PackageSourceFactory |
||
| 86 | */ |
||
| 87 | protected $packageSourceFactory; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Project. |
||
| 91 | * |
||
| 92 | * Lazily loaded -- be sure to call getProject() in order to ensure the |
||
| 93 | * value is defined. |
||
| 94 | * |
||
| 95 | * @var \ComponentManager\Project\Project |
||
| 96 | */ |
||
| 97 | protected $project; |
||
| 98 | /** |
||
| 99 | * Platform support library. |
||
| 100 | * |
||
| 101 | * @var \ComponentManager\Platform\Platform |
||
| 102 | */ |
||
| 103 | protected $platform; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Initialiser. |
||
| 107 | * |
||
| 108 | * @param \ComponentManager\PackageRepository\PackageRepositoryFactory $packageRepositoryFactory |
||
|
|
|||
| 109 | * @param \ComponentManager\PackageSource\PackageSourceFactory $packageSourceFactory |
||
| 110 | * @param \ComponentManager\PackageFormat\PackageFormatFactory $packageFormatFactory |
||
| 111 | * @param \ComponentManager\Platform\Platform $platform |
||
| 112 | * @param \Symfony\Component\Filesystem\Filesystem $filesystem |
||
| 113 | * @param \Psr\Log\LoggerInterface $logger |
||
| 114 | */ |
||
| 115 | public function __construct(PackageRepositoryFactory $packageRepositoryFactory, |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get the Moodle bridge. |
||
| 134 | * |
||
| 135 | * @param string|null $moodleDirectory |
||
| 136 | * |
||
| 137 | * @return \ComponentManager\Moodle |
||
| 138 | */ |
||
| 139 | protected function getMoodle($moodleDirectory=null) { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Get project. |
||
| 152 | * |
||
| 153 | * @param string|null $projectFilename |
||
| 154 | * @param string|null $projectLockFilename |
||
| 155 | * |
||
| 156 | * @return \ComponentManager\Project\Project |
||
| 157 | */ |
||
| 158 | protected function getProject($projectFilename=null, $projectLockFilename=null) { |
||
| 195 | } |
||
| 196 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.