Complex classes like JobEntityValidatorService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use JobEntityValidatorService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class JobEntityValidatorService implements JobEntityValidatorServiceInterface |
||
| 17 | { |
||
| 18 | const REG_EX_VALID_NAME = '/^[a-zA-Z0-9_-]*$/'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var DatePeriodFactoryInterface |
||
| 22 | */ |
||
| 23 | private $oDatePeriodFactory; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @param DatePeriodFactoryInterface $oDatePeriodFactory |
||
| 27 | */ |
||
| 28 | 13 | public function __construct( |
|
| 34 | |||
| 35 | /** |
||
| 36 | * @param JobEntity $oJobEntity |
||
| 37 | * @return bool |
||
| 38 | */ |
||
| 39 | 12 | public function isEntityValid(JobEntity $oJobEntity) |
|
| 43 | |||
| 44 | /** |
||
| 45 | * @param JobEntity $oJobEntity |
||
| 46 | * @return array |
||
| 47 | */ |
||
| 48 | 13 | public function validateJobEntity(JobEntity $oJobEntity) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @param JobEntity $oJobEntity |
||
| 105 | * @return array |
||
| 106 | */ |
||
| 107 | 1 | public function getInvalidProperties(JobEntity $oJobEntity) |
|
| 108 | { |
||
| 109 | 1 | $_aValidationFields = $this->validateJobEntity($oJobEntity); |
|
| 110 | |||
| 111 | 1 | $_aInvalidFields = []; |
|
| 112 | 1 | foreach ($_aValidationFields as $_sProperty => $_bIsValid) |
|
| 113 | { |
||
| 114 | 1 | if (false == $_bIsValid) |
|
| 115 | 1 | { |
|
| 116 | 1 | $_aInvalidFields[] = $_sProperty; |
|
| 117 | 1 | } |
|
| 118 | 1 | } |
|
| 119 | |||
| 120 | 1 | return $_aInvalidFields; |
|
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param string $sName |
||
| 125 | * @return bool |
||
| 126 | */ |
||
| 127 | 13 | private function isNamePropertyValid($sName) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @param JobEntity $oJobEntity |
||
| 134 | * @return bool |
||
| 135 | */ |
||
| 136 | 13 | private function isSchedulePropertyValid(JobEntity $oJobEntity) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * @param JobEntity $oJobEntity |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | 13 | private function isEpsilonPropertyValid(JobEntity $oJobEntity) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @param array $aConstraints |
||
| 198 | * @return bool |
||
| 199 | */ |
||
| 200 | 13 | private function isConstraintsPropertyValid(array $aConstraints) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * @param JobEntity\ContainerEntity $oContainer |
||
| 218 | * @return bool |
||
| 219 | * |
||
| 220 | * @see http://mesos.github.io/chronos/docs/api.html#adding-a-docker-job |
||
| 221 | * This contains the subfields for the Docker container: |
||
| 222 | * type (required), image (required), forcePullImage (optional), network (optional), |
||
| 223 | * and volumes (optional) |
||
| 224 | */ |
||
| 225 | 13 | private function isContainerPropertyValid($oContainer) |
|
| 257 | } |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.