Completed
Pull Request — master (#14)
by
unknown
15:13 queued 07:44
created

AbstractField::isOrderable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Yaro\Jarboe\Table\Fields;
4
5
use Illuminate\Http\Request;
6
use Yaro\Jarboe\Table\CRUD;
7
use Yaro\Jarboe\Table\Fields\Interfaces\FieldPropsInterface;
8
use Yaro\Jarboe\Table\Fields\Traits\Column;
9
use Yaro\Jarboe\Table\Fields\Traits\DefaultTrait;
10
use Yaro\Jarboe\Table\Fields\Traits\Filter;
11
use Yaro\Jarboe\Table\Fields\Traits\Hidden;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Yaro\Jarboe\Table\Fields\Hidden.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
use Yaro\Jarboe\Table\Fields\Traits\Model;
13
use Yaro\Jarboe\Table\Fields\Traits\Name;
14
use Yaro\Jarboe\Table\Fields\Traits\OldAndAttribute;
15
use Yaro\Jarboe\Table\Fields\Traits\Readonly;
16
use Yaro\Jarboe\Table\Fields\Traits\Tab;
17
use Yaro\Jarboe\Table\Fields\Traits\Title;
18
use Yaro\Jarboe\Table\Fields\Traits\Width;
19
20
abstract class AbstractField implements FieldPropsInterface
21
{
22
    use Hidden;
23
    use Width;
24
    use Column;
25
    use Tab;
26
    use Readonly;
27
    use DefaultTrait;
28
    use Title;
29
    use Name;
30
    use OldAndAttribute;
31
    use Model;
32
    use Filter;
33
34
    const DEFAULT_TAB_IDENT = '[extra]';
35
36
    private $crud;
37
    public static $repeaterAdapterClass;
38
39 811
    public static function make($name = '', $title = '')
40
    {
41 811
        $field = new static;
42
43 811
        if (!$title) {
44 763
            $title = preg_replace('~_~', ' ', ucfirst($name));
45
        }
46 811
        $field->title($title);
47 811
        $field->name($name);
48 811
        $field->tab(self::DEFAULT_TAB_IDENT);
49
50 811
        return $field;
51
    }
52
53 18
    public function value(Request $request)
54
    {
55 18
        $value = $request->get($this->name());
56 18
        if (!$value && $this->isNullable()) {
57 3
            return null;
58
        }
59
60 18
        return $value;
61
    }
62
63 14
    public function shouldSkip(Request $request)
64
    {
65 14
        return false;
66
    }
67
68 1
    public function afterStore($model, Request $request)
69
    {
70
        // dummy
71 1
    }
72
73 2
    public function afterUpdate($model, Request $request)
74
    {
75
        // dummy
76 2
    }
77
78 1
    public function beforeUpdate($model)
79
    {
80
        // dummy
81 1
    }
82
83 12
    public function isInline()
84
    {
85 12
        return false;
86
    }
87
88 16
    public function isRelationField()
89
    {
90 16
        return false;
91
    }
92
93 15
    public function isMultiple()
94
    {
95 15
        return false;
96
    }
97
98 16
    public function isEncode()
99
    {
100 16
        return false;
101
    }
102
103 65
    public function isMarkupRow()
104
    {
105 65
        return false;
106
    }
107
108 3
    public function isOrderable()
109
    {
110 3
        return false;
111
    }
112
113 16
    public function isAjax()
114
    {
115 16
        return false;
116
    }
117
118 3
    public function isNullable()
119
    {
120 3
        return false;
121
    }
122
123 12
    public function hasTooltip()
124
    {
125 12
        return false;
126
    }
127
128 13
    public function hasClipboardButton()
129
    {
130 13
        return false;
131
    }
132
133 13
    public function isTranslatable()
134
    {
135 13
        return false;
136
    }
137
138 15
    public function isMaskable()
139
    {
140 15
        return false;
141
    }
142
143 7
    public function getPlaceholder()
144
    {
145 7
        return null;
146
    }
147
148 14
    public function hasMaxlength(): bool
149
    {
150 14
        return false;
151
    }
152
153
    abstract public function getListView($model);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
154
155
    abstract public function getEditFormView($model);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
156
157
    abstract public function getCreateFormView();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
158
159
    // TODO: interface type-hinting
160 49
    public function prepare(CRUD $crud)
161
    {
162 49
        $this->crud = $crud;
163
164 49
        if ($this->filter()) {
165
            $this->filter()->value(
166
                $this->filter()->getValue($crud->tableIdentifier())
167
            );
168
        }
169
170 49
        $this->setModel($crud->getModel());
171
172 49
        if (method_exists($this, 'setRelationSearchUrl')) {
173
            $this->setRelationSearchUrl($crud->relationSearchUrl());
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 setRelationSearchUrl() 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...
174
        }
175 49
        if (method_exists($this, 'setInlineUrl')) {
176 49
            $this->setInlineUrl($crud->inlineUrl());
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 setInlineUrl() does only exist in the following sub-classes of Yaro\Jarboe\Table\Fields\AbstractField: Yaro\Jarboe\Table\Fields\Date, Yaro\Jarboe\Table\Fields\Number, Yaro\Jarboe\Table\Fields\Text, Yaro\Jarboe\Table\Fields\Textarea. 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...
177
        }
178 49
        if ($this->isTranslatable()) {
179
            $this->locales($crud->getLocales());
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 locales() does only exist in the following sub-classes of Yaro\Jarboe\Table\Fields\AbstractField: Yaro\Jarboe\Table\Fields\Repeater, Yaro\Jarboe\Table\Fields\Text, Yaro\Jarboe\Table\Fields\Textarea, Yaro\Jarboe\Table\Fields\Wysiwyg. 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...
180
            $this->setCurrentLocale($crud->getCurrentLocale());
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 setCurrentLocale() does only exist in the following sub-classes of Yaro\Jarboe\Table\Fields\AbstractField: Yaro\Jarboe\Table\Fields\Repeater, Yaro\Jarboe\Table\Fields\Text, Yaro\Jarboe\Table\Fields\Textarea, Yaro\Jarboe\Table\Fields\Wysiwyg. 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...
181
        }
182 49
    }
183
184
    protected function crud(): CRUD
185
    {
186
        return $this->crud;
187
    }
188
}
189