Completed
Push — master ( 6a6774...9df172 )
by Vitaly
04:29
created

FieldsTable::entities()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 15
rs 9.4286
cc 3
eloc 9
nc 2
nop 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A FieldsTable::rowIDs() 0 11 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: VITALYIEGOROV
5
 * Date: 09.12.15
6
 * Time: 09:57
7
 */
8
namespace samsoncms\api;
9
10
use samsoncms\api\query\FieldNavigation;
11
use samsoncms\api\query\MaterialNavigation;
12
use samsonframework\orm\Condition;
13
use samsonframework\orm\ConditionInterface;
14
use samsonframework\orm\QueryInterface;
15
16
/**
17
 * Material additional fields table.
18
 * @package samsoncms\api
19
 */
20
class FieldsTable
21
{
22
    /** @var integer Navigation identifier for table structure */
23
    protected $navigationID;
24
25
    /** @var integer Table parent material identifier */
26
    protected $materialID;
27
28
    /** @var Field[] Collection field instances that correspond table columns */
29
    protected $fields;
30
31
    /** @var QueryInterface Database query interface */
32
    protected $query;
33
34
    /** @var string Locale identifier */
35
    protected $locale;
36
37
    /** @var array Fields table collection */
38
    protected $collection;
39
40
    /** @return array Get field table column names collection */
41
    public function columns()
42
    {
43
        return array_column($this->fields, Field::F_IDENTIFIER);
44
    }
45
46
    /**
47
     * Get collection of table column values as array.
48
     *
49
     * @param string $fieldID Additional field identifier
50
     * @return array Collection of table column values as array
51
     */
52
    public function values($fieldID)
53
    {
54
        $return = array();
55
        if (isset($this->fields[$fieldID])) {
56
            $return = array_column($this->collection, $fieldID);
57
        }
58
        return $return;
59
    }
60
61
    /**
62
     * Get field table as multidimensional array.
63
     *
64
     * @return array Field table represented as array
65
     */
66
    public function toArray()
67
    {
68
        return $this->collection;
69
    }
70
71
    /** @return array Collection of table rows(materials) identifiers */
72
    protected function rowIDs()
73
    {
74
        // Get collection of nested materials
75
        return $this->query
76
            ->entity(Material::ENTITY)
77
            ->where(Material::F_DELETION, 1)
78
            ->where(Material::F_PRIMARY, (new MaterialNavigation())->idsByRelationID($this->navigationID))
0 ignored issues
show
Documentation introduced by
(new \samsoncms\api\quer...ID($this->navigationID) is of type boolean|object<samsonfra...rk\orm\RecordInterface>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79
            ->where(Material::F_PARENT, $this->materialID)
80
            ->orderBy(Material::F_PRIORITY)
81
            ->fields(Material::F_PRIMARY);
82
    }
83
84
    /**
85
     * Build correct localized field request for retrieving additional fields records.
86
     *
87
     * @param Field[] $fields Collection of additional fields
88
     * @return Condition Built condition for query
89
     */
90
    protected function fieldsCondition($fields)
91
    {
92
        // Group fields by localization
93
        $localizedColumns = array();
94
        $notLocalizedColumns = array();
95
        /** @var Field $field Iterate table columns(fields) */
96
        foreach ($fields as $field) {
97
            if ($field->localized()) {
98
                $localizedColumns[] = $field->id;
99
            } else {
100
                $notLocalizedColumns[] = $field->id;
101
            }
102
        }
103
104
        // Create field condition
105
        $fieldsCondition = new Condition(ConditionInterface::DISJUNCTION);
106
        // Create localized condition
107
        if (sizeof($localizedColumns)) {
108
            $localizedCondition = new Condition(ConditionInterface::CONJUNCTION);
109
            $localizedCondition->add(Field::F_PRIMARY, $localizedColumns)
110
                ->add(MaterialField::F_LOCALE, $this->locale);
111
112
            // Add this condition to condition group
113
            $fieldsCondition->addCondition($localizedCondition);
114
        }
115
116
        // Create not localized condition
117
        if (sizeof($notLocalizedColumns)) {
118
            $fieldsCondition->add(Field::F_PRIMARY, $notLocalizedColumns);
119
        }
120
121
        return $fieldsCondition;
122
    }
123
124
    /**
125
     * Fill table with data from database.
126
     */
127
    protected function load()
128
    {
129
        // Get table Fields instances
130
        $this->fields = (new FieldNavigation())->byRelationID($this->navigationID);
0 ignored issues
show
Documentation Bug introduced by
It seems like (new \samsoncms\api\quer...ID($this->navigationID) of type array<integer,*> is incompatible with the declared type array<integer,object<samsoncms\api\Field>> of property $fields.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
131
132
        if (sizeof($rowIDs = $this->rowIDs())) {
133
            /** @var MaterialField $fieldValue Get additional field value instances */
134
            foreach ($this->query->entity(CMS::MATERIAL_FIELD_RELATION_ENTITY)
0 ignored issues
show
Bug introduced by
The expression $this->query->entity(\sa...$this->fields))->exec() of type boolean|object<samsonfra...rk\orm\RecordInterface> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. 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:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
Coding Style introduced by
Space found before closing bracket of FOREACH loop
Loading history...
135
                         // Get only needed rows(materials)
136
                         ->where(Material::F_PRIMARY, $rowIDs)
0 ignored issues
show
Documentation introduced by
$rowIDs is of type boolean|object<samsonfra...rk\orm\RecordInterface>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
137
                         ->where(Material::F_DELETION, 1)
138
                         // Get correct localizes field condition for columns
139
                         ->whereCondition($this->fieldsCondition($this->fields))
140
                         ->exec() as $fieldValue
141
            ) {
142
                /** @var Field $field Try to find Field instance by identifier */
143
                $field = &$this->fields[$fieldValue[Field::F_PRIMARY]];
144
                if (isset($field)) {
145
                    /**
146
                     * Store table row(material) as it primary, store columns(Fields)
147
                     * by field primary. Use correct column for value.
148
                     */
149
                    $this->collection[$fieldValue[Material::F_PRIMARY]][$fieldValue[Field::F_PRIMARY]]
150
                        = $fieldValue[$field->valueFieldName()];
151
                }
152
            }
153
        }
154
    }
155
156
    /**
157
     * FieldsTable constructor.
158
     *
159
     * @param QueryInterface $query Database query interface
160
     * @param mixed $navigationID Navigation identifier for table structure
161
     * @param integer $materialID Table parent material identifier
162
     * @param string $locale Locale identifier
163
     */
164
    public function __construct(QueryInterface $query, $navigationID, $materialID, $locale = '')
165
    {
166
        $this->query = $query;
167
        $this->navigationID = $navigationID;
168
        $this->materialID = $materialID;
169
        $this->locale = $locale;
170
171
        $this->load();
172
    }
173
}
174