gdbots/pbjc-php

View on GitHub
src/Validator/FieldLessOrEqualThan.php

Summary

Maintainability
C
7 hrs
Test Coverage
Similar blocks of code found in 2 locations. Consider refactoring.
<?php
 
namespace Gdbots\Pbjc\Validator;
 
use Gdbots\Pbjc\Exception\ValidatorException;
use Gdbots\Pbjc\SchemaDescriptor;
 
class FieldLessOrEqualThan implements Constraint
{
/**
* {@inheritdoc}
*/
Function `validate` has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
Avoid variables with short names like $b. Configured minimum length is 3.
Avoid variables with short names like $a. Configured minimum length is 3.
public function validate(SchemaDescriptor $a, SchemaDescriptor $b)
{
Avoid variables with short names like $fa. Configured minimum length is 3.
$fa = array_merge($a->getInheritedFields(), $a->getFields());
Avoid variables with short names like $fb. Configured minimum length is 3.
$fb = array_merge($b->getInheritedFields(), $b->getFields());
 
/** @var \Gdbots\Pbjc\FieldDescriptor $field */
/** @var \Gdbots\Pbjc\FieldDescriptor[] $fb */
foreach ($fa as $name => $field) {
if (!isset($fb[$name]) || !$fb[$name]->getMin()) {
continue;
}
 
if (($field->getMin() && $field->getMin() < $fb[$name]->getMin())
|| (!$field->getMin() && $field->getType()->getMin() < $fb[$name]->getMin())
) {
throw new ValidatorException(sprintf(
'The schema "%s" field "%s" min value "%d" must be less than or equal to "%d".',
$b,
$name,
$fb[$name]->getMin(),
$field->getMin() ?: $field->getType()->getMin()
));
}
}
}
}