Completed
Push — develop ( e47b53...a63fce )
by Nate
08:38
created

MetaQuery::beforePrepare()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 17
cp 0
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 12
nc 2
nop 0
crap 30
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/meta/license
6
 * @link       https://www.flipboxfactory.com/software/meta/
7
 */
8
9
namespace flipbox\meta\db;
10
11
use Craft;
12
use craft\db\Query;
13
use craft\elements\db\ElementQuery;
14
use craft\models\Site;
15
use flipbox\craft\sortable\associations\db\SortableAssociationQueryInterface;
16
use flipbox\meta\elements\Meta as MetaElement;
17
use flipbox\meta\fields\Meta as MetaField;
18
use flipbox\meta\helpers\Field as FieldHelper;
19
use flipbox\meta\records\Meta as MetaRecord;
20
21
/**
22
 * @author Flipbox Factory <[email protected]>
23
 * @since 1.0.0
24
 *
25
 * @property string|string[]|Site $ownerSite The handle(s) of the site(s) that the owner element should be in
26
 *
27
 * @method MetaElement[]|array all($db = null)
28
 * @method MetaElement|null one($db = null)
29
 */
30
class MetaQuery extends ElementQuery implements SortableAssociationQueryInterface
31
{
32
    use traits\Attributes;
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public $orderBy = 'meta.sortOrder';
38
39
    /**
40
     * @inheritdoc
41
     */
42
    protected function beforePrepare(): bool
43
    {
44
        // If we don't have an owner, we won't have any results
45
        if (($this->ownerId !== null && empty($this->ownerId)) ||
46
            ($this->id !== null && empty($this->id))
47
        ) {
48
            return false;
49
        }
50
51
        $this->joinTables();
52
53
        $this->query->select([
54
            MetaRecord::tableAlias() . '.fieldId',
55
            MetaRecord::tableAlias() . '.ownerId',
56
            MetaRecord::tableAlias() . '.ownerSiteId',
57
            MetaRecord::tableAlias() . '.sortOrder',
58
        ]);
59
60
        $this->applyConditions($this->subQuery);
61
62
        return parent::beforePrepare();
63
    }
64
65
    /**
66
     * Sets the [[ownerSiteId]] and [[siteId]] properties.
67
     *
68
     * @param int|string|null $value The property value
69
     * @return static self reference
70
     */
71
    public function ownerSiteId($value)
72
    {
73
        $this->ownerSiteId = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value can also be of type string. However, the property $ownerSiteId is declared as type integer|array<integer,integer>|false|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
74
75
        if ($value && strtolower($value) !== ':empty:') {
76
            // A block will never exist in a site that is different than its ownerSiteId,
77
            // so let's set the siteId param here too.
78
            $this->siteId = (int)$value;
79
        }
80
81
        return $this;
82
    }
83
84
    /**
85
     * Join element/content tables
86
     */
87
    private function joinTables()
88
    {
89
        $this->joinElementTable(MetaRecord::tableAlias());
90
91
        // Figure out which content table to use
92
        $this->contentTable = null;
93
94
        if (!$this->fieldId && $this->id && is_numeric($this->id)) {
95
            $this->fieldId = (new Query())
0 ignored issues
show
Documentation Bug introduced by
It seems like (new \craft\db\Query())-...> $this->id))->scalar() can also be of type string. However, the property $fieldId is declared as type integer|array<integer,integer>|false|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
96
                ->select('fieldId')
97
                ->from(MetaRecord::tableName())
98
                ->where(['id' => $this->id])
99
                ->scalar();
100
        }
101
102
        if ($this->fieldId && is_numeric($this->fieldId)) {
103
            /** @var MetaField $field */
104
            $field = Craft::$app->getFields()->getFieldById($this->fieldId);
105
106
            if ($field) {
107
                $this->contentTable = FieldHelper::getContentTableName($field->id);
108
            }
109
        }
110
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115
    protected function customFields(): array
116
    {
117
        return Craft::$app->getFields()->getAllFields(
118
            FieldHelper::getContextById($this->fieldId)
119
        );
120
    }
121
}
122