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 |
||
39 | class OCSController extends \OCP\AppFramework\OCSController { |
||
40 | |||
41 | /** @var IRootFolder */ |
||
42 | private $rootFolder; |
||
43 | |||
44 | /** @var string */ |
||
45 | private $userId; |
||
46 | |||
47 | /** @var DirectMapper */ |
||
48 | private $directMapper; |
||
49 | |||
50 | /** @var IURLGenerator */ |
||
51 | private $urlGenerator; |
||
52 | |||
53 | /** @var TemplateManager */ |
||
54 | private $manager; |
||
55 | |||
56 | /** @var FederationService */ |
||
57 | private $federationService; |
||
58 | |||
59 | /** |
||
60 | * OCS controller |
||
61 | * |
||
62 | * @param string $appName |
||
63 | * @param IRequest $request |
||
64 | * @param IRootFolder $rootFolder |
||
65 | * @param string $userId |
||
66 | * @param DirectMapper $directMapper |
||
67 | * @param IURLGenerator $urlGenerator |
||
68 | * @param TemplateManager $manager |
||
69 | */ |
||
70 | View Code Duplication | public function __construct(string $appName, |
|
|
|||
71 | IRequest $request, |
||
72 | IRootFolder $rootFolder, |
||
73 | $userId, |
||
74 | DirectMapper $directMapper, |
||
75 | IURLGenerator $urlGenerator, |
||
76 | TemplateManager $manager, |
||
77 | FederationService $federationService |
||
78 | ) { |
||
79 | parent::__construct($appName, $request); |
||
80 | |||
81 | $this->rootFolder = $rootFolder; |
||
82 | $this->userId = $userId; |
||
83 | $this->directMapper = $directMapper; |
||
84 | $this->urlGenerator = $urlGenerator; |
||
85 | $this->manager = $manager; |
||
86 | $this->federationService = $federationService; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @NoAdminRequired |
||
91 | * |
||
92 | * Init an editing session |
||
93 | * |
||
94 | * @param int $fileId |
||
95 | * @return DataResponse |
||
96 | * @throws OCSNotFoundException|OCSBadRequestException |
||
97 | */ |
||
98 | public function create($fileId) { |
||
123 | |||
124 | /** |
||
125 | * @NoAdminRequired |
||
126 | * |
||
127 | * @param string $type The template type |
||
128 | * @return DataResponse |
||
129 | * @throws OCSBadRequestException |
||
130 | */ |
||
131 | public function getTemplates($type) { |
||
138 | |||
139 | /** |
||
140 | * @NoAdminRequired |
||
141 | * |
||
142 | * @param string $path Where to create the document |
||
143 | * @param int $template The template id |
||
144 | */ |
||
145 | public function createFromTemplate($path, $template) { |
||
173 | |||
174 | private function mb_pathinfo($filepath) { |
||
191 | } |
||
192 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.