Completed
Push — master ( 2738e7...2ba442 )
by Vitaly
04:01
created

FieldsTable::rowIDs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.4286
cc 1
eloc 8
nc 1
nop 0
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
use samsoncms\api\query\FieldNavigation;
10
use samsoncms\api\query\MaterialNavigation;
11
use samsonframework\orm\Condition;
12
use samsonframework\orm\ConditionInterface;
13
use samsonframework\orm\QueryInterface;
14
15
/**
16
 * Material additional fields table.
17
 * @package samsoncms\api
18
 */
19
class FieldsTable
20
{
21
    /** @var integer Navigation identifier for table structure */
22
    protected $navigationID;
23
24
    /** @var integer Table parent material identifier */
25
    protected $materialID;
26
27
    /** @var Field[] Collection field instances that correspond table columns */
28
    protected $fields;
29
30
    /** @var QueryInterface Database query interface */
31
    protected $query;
32
33
    /** @var string Locale identifier */
34
    protected $locale;
35
36
    /** @var array Fields table collection */
37
    protected $collection;
38
39
    /** @return array Get field table column names collection */
40
    public function columns()
41
    {
42
        return array_column($this->fields, Field::F_IDENTIFIER);
43
    }
44
45
    /**
46
     * Get collection of table column values as array.
47
     *
48
     * @param string $columnName Additional field name
49
     * @return array Collection of table column values as array
50
     */
51
    public function values($columnName)
52
    {
53
        return array_column($this->collection, $columnName);
54
    }
55
56
    /**
57
     * Get field table as multidimensional array.
58
     *
59
     * @return array Field table represented as array
60
     */
61
    public function toArray()
62
    {
63
        return $this->collection;
64
    }
65
66
    /** @return array Collection of table rows(materials) identifiers */
67
    protected function rowIDs()
68
    {
69
        // Get collection of nested materials
70
        return $this->query
71
            ->entity(Material::ENTITY)
72
            ->where(Material::F_DELETION, 1)
73
            ->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...
74
            ->where(Material::F_PARENT, $this->materialID)
75
            ->orderBy(Material::F_PRIORITY)
76
            ->fields(Material::F_PRIMARY);
77
    }
78
79
    /**
80
     * Build correct localized field request for retrieving additional fields records.
81
     *
82
     * @param Field[] $fields Collection of additional fields
83
     * @return Condition Built condition for query
84
     */
85
    protected function fieldsCondition($fields)
86
    {
87
        // Group fields by localization
88
        $localizedColumns = array();
89
        $notLocalizedColumns = array();
90
        /** @var Field $field Iterate table columns(fields) */
91
        foreach ($fields as $field) {
92
            if (!$field->localized()) {
93
                $localizedColumns[] = $field;
94
            } else {
95
                $notLocalizedColumns[] = $field;
96
            }
97
        }
98
99
        // Create field condition
100
        $fieldsCondition = new Condition(ConditionInterface::DISJUNCTION);
101
        // Create localized condition
102
        if (sizeof($localizedColumns)) {
103
            $localizedCondition = new Condition(ConditionInterface::CONJUNCTION);
104
            $localizedCondition->add(Field::F_PRIMARY, $localizedColumns)
105
                ->add(MaterialField::F_LOCALE, $this->locale);
106
107
            // Add this condition to condition group
108
            $fieldsCondition->addCondition($localizedCondition);
109
        }
110
111
        // Create not localized condition
112
        if (sizeof($notLocalizedColumns)) {
113
            $fieldsCondition->add(Field::F_PRIMARY, $notLocalizedColumns);
114
        }
115
116
        return $fieldsCondition;
117
    }
118
119
    /**
120
     * Fill table with data from database.
121
     */
122
    protected function load()
123
    {
124
        // Get table Fields instances
125
        $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...
126
127
        /** @var MaterialField $fieldValue Get additional field value instances */
128
        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...
129
                     // Get only needed rows(materials)
130
                     ->where(Material::F_PRIMARY, $this->rowIDs())
0 ignored issues
show
Documentation introduced by
$this->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...
131
                     // Get correct localizes field condition for columns
132
                     ->whereCondition($this->fieldsCondition($this->fields))
133
                     ->exec() as $fieldValue
134
        ) {
135
            /** @var Field $field Try to find Field instance by identifier */
136
            $field = &$this->fields[$fieldValue[Field::F_PRIMARY]];
137
            if (isset($field)) {
138
                /**
139
                 * Store table row(material) as it primary, store columns(Fields)
140
                 * by field primary. Use correct column for value.
141
                 */
142
                $this->collection[$fieldValue[Material::F_PRIMARY]][$fieldValue[Field::F_PRIMARY]]
143
                    = $fieldValue[$field->valueFieldName()];
144
            }
145
        }
146
147
    }
148
149
    /**
150
     * FieldsTable constructor.
151
     *
152
     * @param QueryInterface $query Database query interface
153
     * @param integer $navigationID Navigation identifier for table structure
154
     * @param integer $materialID Table parent material identifier
155
     * @param string $locale Locale identifier
156
     */
157
    public function __construct(QueryInterface $query, $navigationID, $materialID, $locale = DEFAULT_LOCALE)
158
    {
159
        $this->query = $query;
160
        $this->navigationID = $navigationID;
161
        $this->materialID = $materialID;
162
        $this->locale = $locale;
163
164
        $this->load();
165
    }
166
}
167