Passed
Pull Request — master (#22)
by
unknown
18:54
created

AbstractField::getErrorsCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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\Errors;
11
use Yaro\Jarboe\Table\Fields\Traits\Filter;
12
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...
13
use Yaro\Jarboe\Table\Fields\Traits\Model;
14
use Yaro\Jarboe\Table\Fields\Traits\Name;
15
use Yaro\Jarboe\Table\Fields\Traits\OldAndAttribute;
16
use Yaro\Jarboe\Table\Fields\Traits\Readonly;
17
use Yaro\Jarboe\Table\Fields\Traits\Tab;
18
use Yaro\Jarboe\Table\Fields\Traits\Title;
19
use Yaro\Jarboe\Table\Fields\Traits\Width;
20
21
abstract class AbstractField implements FieldPropsInterface
22
{
23
    use Hidden;
24
    use Width;
25
    use Column;
26
    use Tab;
27
    use Readonly;
28
    use DefaultTrait;
29
    use Title;
30
    use Name;
31
    use OldAndAttribute;
32
    use Model;
33
    use Filter;
34
    use Errors;
35
36
    const DEFAULT_TAB_IDENT = '[extra]';
37
38
    private $crud;
39
    public static $repeaterAdapterClass;
40
41 1004
    public static function make($name = '', $title = '')
42
    {
43 1004
        $field = new static;
44
45 1004
        if (!$title) {
46 953
            $title = preg_replace('~_~', ' ', ucfirst($name));
47
        }
48 1004
        $field->title($title);
49 1004
        $field->name($name);
50 1004
        $field->tab(self::DEFAULT_TAB_IDENT);
51
52 1004
        return $field;
53
    }
54
55 20
    public function value(Request $request)
56
    {
57 20
        $value = $request->get($this->name());
58 20
        if (!$value && $this->isNullable()) {
59 4
            return null;
60
        }
61
62 20
        return $value;
63
    }
64
65 14
    public function shouldSkip(Request $request)
66
    {
67 14
        return false;
68
    }
69
70 1
    public function afterStore($model, Request $request)
71
    {
72
        // dummy
73 1
    }
74
75 2
    public function afterUpdate($model, Request $request)
76
    {
77
        // dummy
78 2
    }
79
80 1
    public function beforeUpdate($model)
81
    {
82
        // dummy
83 1
    }
84
85 12
    public function isInline()
86
    {
87 12
        return false;
88
    }
89
90 19
    public function isRelationField()
91
    {
92 19
        return false;
93
    }
94
95 16
    public function isMultiple()
96
    {
97 16
        return false;
98
    }
99
100 17
    public function isEncode()
101
    {
102 17
        return false;
103
    }
104
105 74
    public function isMarkupRow()
106
    {
107 74
        return false;
108
    }
109
110 4
    public function isOrderable()
111
    {
112 4
        return false;
113
    }
114
115 17
    public function isAjax()
116
    {
117 17
        return false;
118
    }
119
120 3
    public function isNullable()
121
    {
122 3
        return false;
123
    }
124
125 13
    public function hasTooltip()
126
    {
127 13
        return false;
128
    }
129
130 14
    public function hasClipboardButton()
131
    {
132 14
        return false;
133
    }
134
135 69
    public function isTranslatable()
136
    {
137 69
        return false;
138
    }
139
140 16
    public function isMaskable()
141
    {
142 16
        return false;
143
    }
144
145 8
    public function getPlaceholder()
146
    {
147 8
        return null;
148
    }
149
150 15
    public function hasMaxlength(): bool
151
    {
152 15
        return false;
153
    }
154
155
    public function getErrorsCount($errors): int
156
    {
157
        return count($errors->get($this->name()));
158
    }
159
160 1
    public function getHistoryView($value)
161
    {
162 1
        return view('jarboe::crud.fields.abstract.history', [
163 1
            'value' => $value,
164
        ]);
165
    }
166
167
    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...
168
169
    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...
170
171
    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...
172
173
    // TODO: interface type-hinting
174 58
    public function prepare(CRUD $crud)
175
    {
176 58
        $this->crud = $crud;
177
178 58
        if ($this->filter()) {
179
            $this->filter()->value(
180
                $this->filter()->getValue($crud->tableIdentifier())
181
            );
182
        }
183
184 58
        $this->setModel($crud->getModel());
185
186 58
        if (method_exists($this, 'setRelationSearchUrl')) {
187
            $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\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...
188
        }
189 58
        if (method_exists($this, 'setInlineUrl')) {
190 58
            $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\Checkbox, 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...
191
        }
192 58
        if ($this->isTranslatable()) {
193
            $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\Markdown, 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...
194
            $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\Markdown, 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...
195
        }
196 58
    }
197
198
    protected function crud(): CRUD
199
    {
200
        return $this->crud;
201
    }
202
203 82
    public function belongsToArray(): bool
204
    {
205 82
        return false;
206
    }
207
208
    public function getAncestorName(): string
209
    {
210
        throw new \RuntimeException('Not available: must be inherited from BelongsToArray trait.');
211
    }
212
213
    public function getDescendantName(): string
214
    {
215
        throw new \RuntimeException('Not available: must be inherited from BelongsToArray trait.');
216
    }
217
218
    public function getDotPatternName(): string
219
    {
220
        throw new \RuntimeException('Not available: must be inherited from BelongsToArray trait.');
221
    }
222
}
223