| Conditions | 13 |
| Paths | 48 |
| Total Lines | 46 |
| Code Lines | 23 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 57 | public function validate($value, Constraint $constraint) |
||
| 58 | { |
||
| 59 | if (!$constraint instanceof ValidPartLot) { |
||
| 60 | throw new UnexpectedTypeException($constraint, ValidPartLot::class); |
||
| 61 | } |
||
| 62 | |||
| 63 | if (!$value instanceof PartLot) { |
||
| 64 | throw new UnexpectedTypeException($value, PartLot::class); |
||
| 65 | } |
||
| 66 | |||
| 67 | //We can only validate the values if we know the storelocation |
||
| 68 | if ($value->getStorageLocation()) { |
||
| 69 | $parts = $value->getStorageLocation()->getParts(); |
||
| 70 | |||
| 71 | //Check for isFull() attribute |
||
| 72 | if ($value->getStorageLocation()->isFull()) { |
||
| 73 | //Compare with saved amount value |
||
| 74 | $db_lot = $this->em->getUnitOfWork()->getOriginalEntityData($value); |
||
| 75 | |||
| 76 | //Amount increasment is not allowed |
||
| 77 | if ($db_lot && $value->getAmount() > $db_lot['amount']) { |
||
|
|
|||
| 78 | $this->context->buildViolation('validator.part_lot.location_full.no_increasment') |
||
| 79 | ->setParameter('{{ old_amount }}', $db_lot['amount']) |
||
| 80 | ->atPath('amount')->addViolation(); |
||
| 81 | } |
||
| 82 | |||
| 83 | if (!$parts->contains($value->getPart())) { |
||
| 84 | $this->context->buildViolation('validator.part_lot.location_full') |
||
| 85 | ->atPath('storage_location')->addViolation(); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | |||
| 90 | //Check for onlyExisting |
||
| 91 | if ($value->getStorageLocation()->isLimitToExistingParts()) { |
||
| 92 | if (!$parts->contains($value->getPart())) { |
||
| 93 | $this->context->buildViolation('validator.part_lot.only_existing') |
||
| 94 | ->atPath('storage_location')->addViolation(); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | //Check for only single part |
||
| 99 | if ($value->getStorageLocation()->isLimitToExistingParts()) { |
||
| 100 | if (($parts->count() > 0) && !$parts->contains($value->getPart())) { |
||
| 101 | $this->context->buildViolation('validator.part_lot.single_part') |
||
| 102 | ->atPath('storage_location')->addViolation(); |
||
| 103 | } |
||
| 107 | } |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.