Completed
Push — master ( 025f1d...274f99 )
by Yaro
01:33
created

SelectFilter::isSelectField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yaro\Jarboe\Table\Filters;
4
5
class SelectFilter extends AbstractFilter
6
{
7
    const NO_INPUT_APPLIED = '__jarboe-no-search-input-applied';
8
9
    private $multiple = false;
10
    private $options = null;
11
    private $nullable = false;
12
13
    public function render()
14
    {
15
        $value = $this->value();
16
        if ($this->isMultiple()) {
17
            $value = $value ?: [];
18
        }
19
        $values = is_array($value) ? $value : [$value];
20
21
        return view('jarboe::crud.filters.select', [
22
            'filter' => $this,
23
            'values' => $values,
24
            'desearch' => self::NO_INPUT_APPLIED,
25
        ])->render();
26
    }
27
28
    public function multiple(bool $multiple = true)
29
    {
30
        $this->multiple = $multiple;
31
32
        return $this;
33
    }
34
35
    public function nullable(bool $nullable = true)
36
    {
37
        $this->nullable = $nullable;
38
39
        return $this;
40
    }
41
42
    public function isNullable(): bool
43
    {
44
        if (is_null($this->nullable)) {
45
            return $this->field()->isNullable();
46
        }
47
48
        return $this->nullable;
49
    }
50
51
    public function isMultiple(): bool
52
    {
53
        if (is_null($this->multiple)) {
54
            return $this->field()->isMultiple();
55
        }
56
57
        return $this->multiple;
58
    }
59
60
    public function options(array $options)
61
    {
62
        $this->options = $options;
63
64
        return $this;
65
    }
66
67
    public function getOptions()
68
    {
69
        $options = $this->options;
70
        if (is_null($options)) {
71
            $options = $this->field()->getOptions();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Yaro\Jarboe\Table\Fields\AbstractField as the method getOptions() does only exist in the following sub-classes of Yaro\Jarboe\Table\Fields\AbstractField: Yaro\Jarboe\Etc\CustomFields\PermissionField, Yaro\Jarboe\Etc\CustomFields\RoleField, Yaro\Jarboe\Table\Fields\Radio, Yaro\Jarboe\Table\Fields\Select, Yaro\Jarboe\Table\Fields\Tags. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
72
        }
73
74
        return $options;
75
    }
76
77
    public function getSelectedOptions(array $values, int $index = 0): array
78
    {
79
        $total = 0;
80
        $options = $this->field()->getOptions(null, null, null, $total, $index, function ($query, $related) use ($values) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Yaro\Jarboe\Table\Fields\AbstractField as the method getOptions() does only exist in the following sub-classes of Yaro\Jarboe\Table\Fields\AbstractField: Yaro\Jarboe\Etc\CustomFields\PermissionField, Yaro\Jarboe\Etc\CustomFields\RoleField, Yaro\Jarboe\Table\Fields\Radio, Yaro\Jarboe\Table\Fields\Select, Yaro\Jarboe\Table\Fields\Tags. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
81
            $query->whereIn($related->getTable() .'.'. $related->getKeyName(), $values);
82
        });
83
84
        return $options;
85
    }
86
87
    public function getSelectedGroupedOptions(): array
88
    {
89
        $value = $this->value();
90
        $values = is_array($value) ? $value : [$value];
91
92
        $options = [];
93
        foreach ($this->field()->getRelations() as $index => $relation) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Yaro\Jarboe\Table\Fields\AbstractField as the method getRelations() does only exist in the following sub-classes of Yaro\Jarboe\Table\Fields\AbstractField: Yaro\Jarboe\Table\Fields\Radio, Yaro\Jarboe\Table\Fields\Select. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
94
            $groupValues = [];
95 View Code Duplication
            foreach ($values as $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
96
                list($groupHash, $groupValue) = explode('~~~', $value);
97
                if ($groupHash == crc32($relation['group'])) {
98
                    $groupValues[] = $groupValue;
99
                }
100
            }
101
102
            if ($groupValues) {
103
                $options[$relation['group']] = $this->getSelectedOptions($groupValues, $index);
104
            }
105
        }
106
107
        return array_filter($options);
108
    }
109
110
    public function isSelectField(): bool
111
    {
112
        return method_exists($this->field(), 'isSelect2Type');
113
    }
114
115
    public function apply($query)
116
    {
117
        $value = $this->value();
118
        if (is_null($value) || $value == self::NO_INPUT_APPLIED) {
119
            return;
120
        }
121
122
        if ($this->field()->isRelationField()) {
123
            $values = is_array($value) ? $value : [$value];
124
            $model = $this->field()->getModel();
125
            $model = new $model;
126
127
            foreach ($this->field()->getRelations() as $index => $relation) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Yaro\Jarboe\Table\Fields\AbstractField as the method getRelations() does only exist in the following sub-classes of Yaro\Jarboe\Table\Fields\AbstractField: Yaro\Jarboe\Table\Fields\Radio, Yaro\Jarboe\Table\Fields\Select. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
128
                $groupValues = $values;
129
                if ($this->field()->isGroupedRelation()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Yaro\Jarboe\Table\Fields\AbstractField as the method isGroupedRelation() does only exist in the following sub-classes of Yaro\Jarboe\Table\Fields\AbstractField: Yaro\Jarboe\Table\Fields\Radio, Yaro\Jarboe\Table\Fields\Select. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
130
                    $groupValues = [];
131 View Code Duplication
                    foreach ($values as $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
132
                        list($groupHash, $groupValue) = explode('~~~', $value);
133
                        if ($groupHash == crc32($relation['group'])) {
134
                            $groupValues[] = $groupValue;
135
                        }
136
                    }
137
                }
138
139
                if ($groupValues) {
140
                    $this->applyRelationValues($model, $query, $groupValues, $index);
141
                }
142
            }
143
144
            return;
145
        }
146
147
        if ($this->field()->isMultiple()) {
148
            $query->whereIn($this->field()->name(), $value);
149
            return;
150
        }
151
152
        $query->where(
153
            $this->field()->name(),
154
            $this->sign,
155
            $value
156
        );
157
    }
158
159
    private function applyRelationValues($model, $query, $values, int $index = 0)
160
    {
161
        $relationQuery = $model->{$this->field()->getRelationMethod($index)}()->getRelated();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Yaro\Jarboe\Table\Fields\AbstractField as the method getRelationMethod() does only exist in the following sub-classes of Yaro\Jarboe\Table\Fields\AbstractField: Yaro\Jarboe\Etc\CustomFields\PermissionField, Yaro\Jarboe\Etc\CustomFields\RoleField, Yaro\Jarboe\Table\Fields\Radio, Yaro\Jarboe\Table\Fields\Select, Yaro\Jarboe\Table\Fields\Tags. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
162
        $relationClass = get_class($relationQuery);
163
        $relationClass = new $relationClass;
164
165
        $query->whereHas($this->field()->getRelationMethod($index), function($query) use($values, $relationClass, $relationQuery) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Yaro\Jarboe\Table\Fields\AbstractField as the method getRelationMethod() does only exist in the following sub-classes of Yaro\Jarboe\Table\Fields\AbstractField: Yaro\Jarboe\Etc\CustomFields\PermissionField, Yaro\Jarboe\Etc\CustomFields\RoleField, Yaro\Jarboe\Table\Fields\Radio, Yaro\Jarboe\Table\Fields\Select, Yaro\Jarboe\Table\Fields\Tags. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
166
            $query->whereIn($relationQuery->getTable() .'.'. $relationClass->getKeyName(), $values);
167
        });
168
    }
169
}
170