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 |
||
| 15 | class CRLDistributionPointsExtension extends Extension implements \Countable, |
||
|
|
|||
| 16 | \IteratorAggregate |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Distribution points. |
||
| 20 | * |
||
| 21 | * @var DistributionPoint[] $_distributionPoints |
||
| 22 | */ |
||
| 23 | protected $_distributionPoints; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Constructor |
||
| 27 | * |
||
| 28 | * @param bool $critical |
||
| 29 | * @param DistributionPoint ...$distribution_points |
||
| 30 | */ |
||
| 31 | 11 | public function __construct($critical, |
|
| 32 | DistributionPoint ...$distribution_points) { |
||
| 33 | 11 | parent::__construct(self::OID_CRL_DISTRIBUTION_POINTS, $critical); |
|
| 34 | 11 | $this->_distributionPoints = $distribution_points; |
|
| 35 | 11 | } |
|
| 36 | |||
| 37 | 11 | View Code Duplication | protected static function _fromDER($data, $critical) { |
| 38 | 11 | $dps = array_map( |
|
| 39 | function (UnspecifiedType $el) { |
||
| 40 | 10 | return DistributionPoint::fromASN1($el->asSequence()); |
|
| 41 | 11 | }, Sequence::fromDER($data)->elements()); |
|
| 42 | 11 | if (!count($dps)) { |
|
| 43 | 1 | throw new \UnexpectedValueException( |
|
| 44 | "CRLDistributionPoints must have" . |
||
| 45 | 1 | " at least one DistributionPoint."); |
|
| 46 | } |
||
| 47 | // late static bound, extended by Freshest CRL extension |
||
| 48 | 10 | return new static($critical, ...$dps); |
|
| 49 | } |
||
| 50 | |||
| 51 | 17 | View Code Duplication | protected function _valueASN1() { |
| 61 | |||
| 62 | /** |
||
| 63 | * Get distribution points. |
||
| 64 | * |
||
| 65 | * @return DistributionPoint[] |
||
| 66 | */ |
||
| 67 | 1 | public function distributionPoints() { |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Get the number of distribution points. |
||
| 73 | * |
||
| 74 | * @see Countable::count() |
||
| 75 | * @return int |
||
| 76 | */ |
||
| 77 | 1 | public function count() { |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Get iterator for distribution points. |
||
| 83 | * |
||
| 84 | * @see IteratorAggregate::getIterator() |
||
| 85 | * @return \ArrayIterator |
||
| 86 | */ |
||
| 87 | 3 | public function getIterator() { |
|
| 90 | } |
||
| 91 |