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 |
||
| 19 | View Code Duplication | class SearchService implements SearchServiceInterface |
|
|
|
|||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Aggregated service. |
||
| 23 | * |
||
| 24 | * @var \eZ\Publish\API\Repository\SearchService |
||
| 25 | */ |
||
| 26 | protected $service; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * SignalDispatcher. |
||
| 30 | * |
||
| 31 | * @var \eZ\Publish\Core\SignalSlot\SignalDispatcher |
||
| 32 | */ |
||
| 33 | protected $signalDispatcher; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Constructor. |
||
| 37 | * |
||
| 38 | * Construct service object from aggregated service and signal |
||
| 39 | * dispatcher |
||
| 40 | * |
||
| 41 | * @param \eZ\Publish\API\Repository\SearchService $service |
||
| 42 | * @param \eZ\Publish\Core\SignalSlot\SignalDispatcher $signalDispatcher |
||
| 43 | */ |
||
| 44 | public function __construct(SearchServiceInterface $service, SignalDispatcher $signalDispatcher) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Finds content objects for the given query. |
||
| 52 | * |
||
| 53 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
| 54 | * |
||
| 55 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 56 | * @param array $languageFilter Configuration for specifying prioritized languages query will be performed on. |
||
| 57 | * Currently supported: <code>array("languages" => array(<language1>,..))</code>. |
||
| 58 | * @param bool $filterOnUserPermissions if true only the objects which the user is allowed to read are returned. |
||
| 59 | * |
||
| 60 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
| 61 | */ |
||
| 62 | public function findContent(Query $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Finds contentInfo objects for the given query. |
||
| 69 | * |
||
| 70 | * @see SearchServiceInterface::findContentInfo() |
||
| 71 | * |
||
| 72 | * @since 5.4.5 |
||
| 73 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
| 74 | * |
||
| 75 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 76 | * @param array $languageFilter - a map of filters for the returned fields. |
||
| 77 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 78 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations. |
||
| 79 | * @param bool $filterOnUserPermissions if true (default) only the objects which is the user allowed to read are returned. |
||
| 80 | * |
||
| 81 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
| 82 | */ |
||
| 83 | public function findContentInfo(Query $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Performs a query for a single content object. |
||
| 90 | * |
||
| 91 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the object was not found by the query or due to permissions |
||
| 92 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if criterion is not valid |
||
| 93 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is more than than one result matching the criterions |
||
| 94 | * |
||
| 95 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
| 96 | * @param array $languageFilter Configuration for specifying prioritized languages query will be performed on. |
||
| 97 | * Currently supported: <code>array("languages" => array(<language1>,..))</code>. |
||
| 98 | * @param bool $filterOnUserPermissions if true only the objects which is the user allowed to read are returned. |
||
| 99 | * |
||
| 100 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 101 | */ |
||
| 102 | public function findSingle(Criterion $filter, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Suggests a list of values for the given prefix. |
||
| 109 | * |
||
| 110 | * @param string $prefix |
||
| 111 | * @param string[] $fieldPaths |
||
| 112 | * @param int $limit |
||
| 113 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
| 114 | */ |
||
| 115 | public function suggest($prefix, $fieldPaths = array(), $limit = 10, Criterion $filter = null) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Finds Locations for the given query. |
||
| 122 | * |
||
| 123 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
| 124 | * |
||
| 125 | * @param \eZ\Publish\API\Repository\Values\Content\LocationQuery $query |
||
| 126 | * @param array $languageFilter Configuration for specifying prioritized languages query will be performed on. |
||
| 127 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 128 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations |
||
| 129 | * @param bool $filterOnUserPermissions if true only the objects which is the user allowed to read are returned. |
||
| 130 | * |
||
| 131 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
| 132 | */ |
||
| 133 | public function findLocations(LocationQuery $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
| 137 | |||
| 138 | public function supports($capabilityFlag) |
||
| 142 | } |
||
| 143 |
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.