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 |
||
| 49 | class RequestSharedSecret extends Job { |
||
| 50 | |||
| 51 | /** @var IClient */ |
||
| 52 | private $httpClient; |
||
| 53 | |||
| 54 | /** @var IJobList */ |
||
| 55 | private $jobList; |
||
| 56 | |||
| 57 | /** @var IURLGenerator */ |
||
| 58 | private $urlGenerator; |
||
| 59 | |||
| 60 | /** @var DbHandler */ |
||
| 61 | private $dbHandler; |
||
| 62 | |||
| 63 | /** @var TrustedServers */ |
||
| 64 | private $trustedServers; |
||
| 65 | |||
| 66 | /** @var IDiscoveryService */ |
||
| 67 | private $ocsDiscoveryService; |
||
| 68 | |||
| 69 | /** @var ILogger */ |
||
| 70 | private $logger; |
||
| 71 | |||
| 72 | /** @var bool */ |
||
| 73 | protected $retainJob = false; |
||
| 74 | |||
| 75 | private $format = '?format=json'; |
||
| 76 | |||
| 77 | private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/request-shared-secret'; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * RequestSharedSecret constructor. |
||
| 81 | * |
||
| 82 | * @param IClient $httpClient |
||
| 83 | * @param IURLGenerator $urlGenerator |
||
| 84 | * @param IJobList $jobList |
||
| 85 | * @param TrustedServers $trustedServers |
||
| 86 | * @param DbHandler $dbHandler |
||
| 87 | * @param IDiscoveryService $ocsDiscoveryService |
||
| 88 | */ |
||
| 89 | public function __construct( |
||
| 90 | IClient $httpClient = null, |
||
| 91 | IURLGenerator $urlGenerator = null, |
||
| 92 | IJobList $jobList = null, |
||
| 93 | TrustedServers $trustedServers = null, |
||
| 94 | DbHandler $dbHandler = null, |
||
| 95 | IDiscoveryService $ocsDiscoveryService = null |
||
| 96 | ) { |
||
| 97 | $this->httpClient = $httpClient ? $httpClient : \OC::$server->getHTTPClientService()->newClient(); |
||
| 98 | $this->jobList = $jobList ? $jobList : \OC::$server->getJobList(); |
||
| 99 | $this->urlGenerator = $urlGenerator ? $urlGenerator : \OC::$server->getURLGenerator(); |
||
| 100 | $this->dbHandler = $dbHandler ? $dbHandler : new DbHandler(\OC::$server->getDatabaseConnection(), \OC::$server->getL10N('federation')); |
||
| 101 | $this->logger = \OC::$server->getLogger(); |
||
| 102 | $this->ocsDiscoveryService = $ocsDiscoveryService ? $ocsDiscoveryService : \OC::$server->query(\OCP\OCS\IDiscoveryService::class); |
||
| 103 | View Code Duplication | if ($trustedServers) { |
|
| 104 | $this->trustedServers = $trustedServers; |
||
| 105 | } else { |
||
| 106 | $this->trustedServers = new TrustedServers( |
||
| 107 | $this->dbHandler, |
||
| 108 | \OC::$server->getHTTPClientService(), |
||
| 109 | $this->logger, |
||
| 110 | $this->jobList, |
||
| 111 | \OC::$server->getSecureRandom(), |
||
| 112 | \OC::$server->getConfig(), |
||
| 113 | \OC::$server->getEventDispatcher() |
||
| 114 | ); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * run the job, then remove it from the joblist |
||
| 121 | * |
||
| 122 | * @param JobList $jobList |
||
| 123 | * @param ILogger $logger |
||
| 124 | */ |
||
| 125 | View Code Duplication | public function execute($jobList, ILogger $logger = null) { |
|
| 136 | |||
| 137 | /** |
||
| 138 | * call execute() method of parent |
||
| 139 | * |
||
| 140 | * @param JobList $jobList |
||
| 141 | * @param ILogger $logger |
||
| 142 | */ |
||
| 143 | protected function parentExecute($jobList, $logger) { |
||
| 146 | |||
| 147 | protected function run($argument) { |
||
| 201 | } |
||
| 202 |
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.