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 |
||
| 30 | final class ResolverStack extends AbstractResolver |
||
| 31 | { |
||
| 32 | /** @var ResolverInterface[] */ |
||
| 33 | protected $resolvers = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * ResolverStack constructor. |
||
| 37 | * |
||
| 38 | * @param ResolverInterface[] $resolvers |
||
| 39 | * |
||
| 40 | * @param bool $cacheEnabled |
||
| 41 | * @throws InvalidArgumentException |
||
| 42 | */ |
||
| 43 | 115 | public function __construct(array $resolvers, $cacheEnabled = true) |
|
| 44 | { |
||
| 45 | 115 | foreach ($resolvers as $resolver) { |
|
| 46 | 115 | View Code Duplication | if (!is_object($resolver) || !$resolver instanceof ResolverInterface) { |
|
|
|||
| 47 | 1 | throw new InvalidArgumentException(sprintf( |
|
| 48 | 1 | 'Invalid resolver of type "%s". Expected instance of "%s".', |
|
| 49 | 1 | is_object($resolver) ? get_class($resolver) : gettype($resolver), |
|
| 50 | ResolverInterface::class |
||
| 51 | )); |
||
| 52 | } |
||
| 53 | } |
||
| 54 | $this->resolvers = $resolvers; |
||
| 55 | parent::__construct($cacheEnabled); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Resolves an alias |
||
| 60 | * |
||
| 61 | * @param string $alias |
||
| 62 | * @param Collection $collection |
||
| 63 | * |
||
| 64 | * @return \Baleen\Migrations\Version\VersionInterface|null |
||
| 65 | * @throws \Baleen\Migrations\Exception\Version\Collection\ResolverException |
||
| 66 | */ |
||
| 67 | public function doResolve($alias, Collection $collection) |
||
| 77 | } |
||
| 78 |
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.