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 |
||
| 36 | class PublishPlugin extends ServerPlugin { |
||
| 37 | const NS_CALENDARSERVER = 'http://calendarserver.org/ns/'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Reference to SabreDAV server object. |
||
| 41 | * |
||
| 42 | * @var \Sabre\DAV\Server |
||
| 43 | */ |
||
| 44 | protected $server; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Config instance to get instance secret. |
||
| 48 | * |
||
| 49 | * @var IConfig |
||
| 50 | */ |
||
| 51 | protected $config; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * URL Generator for absolute URLs. |
||
| 55 | * |
||
| 56 | * @var IURLGenerator |
||
| 57 | */ |
||
| 58 | protected $urlGenerator; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * PublishPlugin constructor. |
||
| 62 | * |
||
| 63 | * @param IConfig $config |
||
| 64 | * @param IURLGenerator $urlGenerator |
||
| 65 | */ |
||
| 66 | public function __construct(IConfig $config, IURLGenerator $urlGenerator) { |
||
| 67 | $this->config = $config; |
||
| 68 | $this->urlGenerator = $urlGenerator; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * This method should return a list of server-features. |
||
| 73 | * |
||
| 74 | * This is for example 'versioning' and is added to the DAV: header |
||
| 75 | * in an OPTIONS response. |
||
| 76 | * |
||
| 77 | * @return string[] |
||
| 78 | */ |
||
| 79 | public function getFeatures() { |
||
| 80 | // May have to be changed to be detected |
||
| 81 | return ['oc-calendar-publishing', 'calendarserver-sharing']; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Returns a plugin name. |
||
| 86 | * |
||
| 87 | * Using this name other plugins will be able to access other plugins |
||
| 88 | * using Sabre\DAV\Server::getPlugin |
||
| 89 | * |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | public function getPluginName() { |
||
| 93 | return 'oc-calendar-publishing'; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * This initializes the plugin. |
||
| 98 | * |
||
| 99 | * This function is called by Sabre\DAV\Server, after |
||
| 100 | * addPlugin is called. |
||
| 101 | * |
||
| 102 | * This method should set up the required event subscriptions. |
||
| 103 | * |
||
| 104 | * @param Server $server |
||
| 105 | */ |
||
| 106 | public function initialize(Server $server) { |
||
| 112 | |||
| 113 | public function propFind(PropFind $propFind, INode $node) { |
||
| 114 | if ($node instanceof Calendar) { |
||
| 115 | $propFind->handle('{'.self::NS_CALENDARSERVER.'}publish-url', function () use ($node) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * We intercept this to handle POST requests on calendars. |
||
| 133 | * |
||
| 134 | * @param RequestInterface $request |
||
| 135 | * @param ResponseInterface $response |
||
| 136 | * |
||
| 137 | * @return void|bool |
||
| 138 | */ |
||
| 139 | public function httpPost(RequestInterface $request, ResponseInterface $response) { |
||
| 227 | } |
||
| 228 |
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.