nnx-framework /
data-grid
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @company MTE Telecom, Ltd. |
||
| 4 | * @author Roman Malashin <[email protected]> |
||
| 5 | */ |
||
| 6 | |||
| 7 | namespace Nnx\DataGrid\Column; |
||
| 8 | |||
| 9 | use Nnx\DataGrid\Column\Exception\InvalidColumnException; |
||
| 10 | use Nnx\DataGrid\Column\Exception\InvalidNameException; |
||
| 11 | use Nnx\DataGrid\Column\Exception\InvalidSpecificationException; |
||
| 12 | use Zend\ServiceManager\FactoryInterface; |
||
| 13 | use Nnx\DataGrid\Mutator\MutatorInterface; |
||
| 14 | use Traversable; |
||
| 15 | use Zend\Http\Header\HeaderInterface; |
||
| 16 | use Zend\ServiceManager\MutableCreationOptionsInterface; |
||
| 17 | use Zend\ServiceManager\MutableCreationOptionsTrait; |
||
| 18 | use Zend\ServiceManager\ServiceLocatorInterface; |
||
| 19 | use ReflectionClass; |
||
| 20 | use Nnx\DataGrid\Mutator\Exception\RuntimeException; |
||
| 21 | |||
| 22 | |||
| 23 | /** |
||
| 24 | * Class Factory |
||
| 25 | * @package Nnx\DataGrid\Column |
||
| 26 | */ |
||
| 27 | class Factory implements MutableCreationOptionsInterface, FactoryInterface |
||
| 28 | { |
||
| 29 | use GridColumnPluginManagerAwareTrait; |
||
| 30 | use MutableCreationOptionsTrait; |
||
| 31 | |||
| 32 | |||
| 33 | /** |
||
| 34 | * Конструктор класса |
||
| 35 | * @param array $options |
||
| 36 | */ |
||
| 37 | public function __construct(array $options) |
||
| 38 | { |
||
| 39 | $this->setCreationOptions($options); |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param array | Traversable $spec |
||
| 44 | * @throws InvalidColumnException |
||
| 45 | * @throws InvalidNameException |
||
| 46 | * @throws InvalidSpecificationException |
||
| 47 | */ |
||
| 48 | protected function validate($spec) |
||
| 49 | { |
||
| 50 | View Code Duplication | if (!is_array($spec) && !$spec instanceof Traversable) { |
|
|
0 ignored issues
–
show
|
|||
| 51 | throw new InvalidSpecificationException( |
||
| 52 | sprintf('Передана некорректная спецификация для создания колонки. Ожидается array или %s, прищел: %s', |
||
| 53 | Traversable::class, |
||
| 54 | gettype($spec) |
||
| 55 | ) |
||
| 56 | ); |
||
| 57 | } |
||
| 58 | if (!array_key_exists('type', $spec) || !$spec['type']) { |
||
| 59 | throw new InvalidColumnException('Не передан тип создаваемого столбца.'); |
||
| 60 | } |
||
| 61 | if (!array_key_exists('name', $spec) || !$spec['name']) { |
||
| 62 | throw new InvalidNameException('Не задано имя для колонки.'); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Возвращщает набор мутаторов |
||
| 68 | * @param array $spec |
||
| 69 | * @return array |
||
| 70 | * @throws RuntimeException |
||
| 71 | */ |
||
| 72 | protected function getMutators($spec) |
||
| 73 | { |
||
| 74 | $mutators = []; |
||
| 75 | if (array_key_exists('mutators', $spec) && $spec['mutators']) { |
||
| 76 | $mutatorPluginManager = $this->getColumnPluginManager()->getServiceLocator()->get('GridMutatorManager'); |
||
| 77 | foreach ($spec['mutators'] as $mutator) { |
||
| 78 | if (!$mutator instanceof MutatorInterface) { |
||
| 79 | if (is_array($mutator) && (!array_key_exists('type', $mutator) || !$mutator['type'])) { |
||
| 80 | throw new RuntimeException('Для создания экземпляра мутатора должен быть передан его type'); |
||
| 81 | } |
||
| 82 | $mutator['options'] = array_key_exists('options', $mutator) ? $mutator['options'] : []; |
||
| 83 | $mutator = $mutatorPluginManager->get($mutator['type'], $mutator['options']); |
||
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. Loading history...
|
|||
| 84 | } |
||
| 85 | $mutators[] = $mutator; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | return $mutators; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Метод осуществляет подготовку данных предустановленных мутаторов по умолчанию. |
||
| 94 | * Преобразует массив данных для предустановленных мутаторов в общий формат. |
||
| 95 | * @param ColumnInterface $column |
||
| 96 | * @param array | Traversable $spec |
||
| 97 | * @return array | Traversable |
||
| 98 | */ |
||
| 99 | protected function prepareMutatorsSpecification(ColumnInterface $column, $spec) |
||
| 100 | { |
||
| 101 | $mutatorsNames = $column->getInvokableMutators(); |
||
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. Loading history...
|
|||
| 102 | $mutatorsOptions = []; |
||
| 103 | if (count($mutatorsNames)) { |
||
| 104 | if (array_key_exists('options', $spec) |
||
| 105 | && is_array($spec['options']) |
||
| 106 | && array_key_exists('mutatorsOptions', $spec['options']) |
||
| 107 | ) { |
||
| 108 | $mutatorsOptions = $spec['options']['mutatorsOptions']; |
||
| 109 | } |
||
| 110 | foreach ($mutatorsNames as $k => $mutator) { |
||
| 111 | $spec['mutators'][] = [ |
||
| 112 | 'type' => $mutator, |
||
| 113 | 'options' => array_key_exists($k, $mutatorsOptions) ? $mutatorsOptions[$k] : [] |
||
| 114 | ]; |
||
| 115 | } |
||
| 116 | unset($spec['options']['mutatorsOptions']); |
||
| 117 | } |
||
| 118 | |||
| 119 | return $spec; |
||
| 120 | } |
||
| 121 | |||
| 122 | protected function createHeader($spec) |
||
|
0 ignored issues
–
show
The return type could not be reliably inferred; please add a
@return annotation.
Our type inference engine in quite powerful, but sometimes the code does not
provide enough clues to go by. In these cases we request you to add a Loading history...
|
|||
| 123 | { |
||
| 124 | $header = null; |
||
| 125 | if (array_key_exists('header', $spec) |
||
| 126 | && $spec['header'] |
||
| 127 | ) { |
||
| 128 | if (!$spec['header'] instanceof HeaderInterface) { |
||
| 129 | /** @var Header\Factory $headerFactory */ |
||
| 130 | $headerFactory = $this->getColumnPluginManager()->getServiceLocator()->get(Header\Factory::class); |
||
| 131 | $header = $headerFactory->create($spec['header']); |
||
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. Loading history...
|
|||
| 132 | } else { |
||
| 133 | $header = $spec['header']; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | return $header; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Create service |
||
| 141 | * |
||
| 142 | * @param ServiceLocatorInterface $serviceLocator |
||
| 143 | * @return mixed |
||
| 144 | * @throws Exception\RuntimeException |
||
| 145 | * @throws InvalidColumnException |
||
| 146 | * @throws InvalidNameException |
||
| 147 | * @throws InvalidSpecificationException |
||
| 148 | * @throws RuntimeException |
||
| 149 | */ |
||
| 150 | public function createService(ServiceLocatorInterface $serviceLocator) |
||
| 151 | { |
||
| 152 | $spec = $this->getCreationOptions(); |
||
| 153 | $this->setColumnPluginManager($serviceLocator); |
||
|
0 ignored issues
–
show
$serviceLocator of type object<Zend\ServiceManag...erviceLocatorInterface> is not a sub-type of object<Nnx\DataGrid\Colu...ridColumnPluginManager>. It seems like you assume a concrete implementation of the interface Zend\ServiceManager\ServiceLocatorInterface to be always present.
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass. Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type. Loading history...
|
|||
| 154 | $this->validate($spec); |
||
| 155 | $className = __NAMESPACE__ . '\\' . ucfirst($spec['type']); |
||
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. Loading history...
|
|||
| 156 | $reflectionColumn = new ReflectionClass($className); |
||
| 157 | if (!$reflectionColumn->isInstantiable()) { |
||
| 158 | throw new Exception\RuntimeException(sprintf('Класс %s не найден', $className)); |
||
| 159 | } |
||
| 160 | unset($spec['columnPluginManager']); |
||
| 161 | $column = $reflectionColumn->newInstance($spec); |
||
| 162 | $header = $this->createHeader($spec); |
||
| 163 | $column->setHeader($header); |
||
| 164 | $spec = $this->prepareMutatorsSpecification($column, $spec); |
||
| 165 | $column->setMutators($this->getMutators($spec)); |
||
| 166 | |||
| 167 | return $column; |
||
| 168 | } |
||
| 169 | } |
||
|
0 ignored issues
–
show
|
|||
| 170 |
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.