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 |
||
33 | class FilesSearchReportPlugin extends ServerPlugin { |
||
34 | // namespace |
||
35 | const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
||
36 | const REPORT_NAME = '{http://owncloud.org/ns}search-files'; |
||
37 | |||
38 | /** |
||
39 | * Reference to main server object |
||
40 | * |
||
41 | * @var Server |
||
42 | */ |
||
43 | private $server; |
||
44 | |||
45 | /** @var ISearch */ |
||
46 | private $searchService; |
||
47 | |||
48 | public function __construct(ISearch $searchService) { |
||
51 | |||
52 | /** |
||
53 | * This initializes the plugin. |
||
54 | * |
||
55 | * This function is called by \Sabre\DAV\Server, after |
||
56 | * addPlugin is called. |
||
57 | * |
||
58 | * This method should set up the required event subscriptions. |
||
59 | * |
||
60 | * @param Server $server |
||
61 | * @return void |
||
62 | */ |
||
63 | public function initialize(Server $server) { |
||
71 | |||
72 | /** |
||
73 | * Returns a list of reports this plugin supports. |
||
74 | * |
||
75 | * This will be used in the {DAV:}supported-report-set property. |
||
76 | * |
||
77 | * @param string $uri |
||
78 | * @return array |
||
79 | */ |
||
80 | public function getSupportedReportSet($uri) { |
||
83 | |||
84 | /** |
||
85 | * REPORT operations to look for files |
||
86 | * |
||
87 | * @param string $reportName |
||
88 | * @param mixed $report |
||
89 | * @param string $uri |
||
90 | * @return bool |
||
91 | * @throws BadRequest |
||
92 | * @throws NotImplemented |
||
93 | * @internal param $ [] $report |
||
94 | */ |
||
95 | public function onReport($reportName, $report, $uri) { |
||
139 | |||
140 | /** |
||
141 | * @param string $filesUri the base uri for this user's files directory, |
||
142 | * usually /files/username |
||
143 | * @param File[] $searchResults the results coming from the search service, |
||
144 | * within the files app |
||
145 | * @param array $requestedProps the list of requested webDAV properties |
||
146 | * @return \Generator a generator to traverse over the properties of the |
||
147 | * search result, suitable for server's multistatus response |
||
148 | */ |
||
149 | private function getSearchResultIterator($filesUri, $searchResults, $requestedProps) { |
||
172 | |||
173 | /** |
||
174 | * Returns the base uri of the files root by removing |
||
175 | * the subpath from the URI |
||
176 | * |
||
177 | * @param string $uri URI from this request |
||
178 | * @param string $subPath subpath to remove from the URI |
||
179 | * |
||
180 | * @return string files base uri |
||
181 | */ |
||
182 | private function getFilesBaseUri($uri, $subPath) { |
||
196 | } |
||
197 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: