GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#688)
by
unknown
18:52
created

DateTime::setModelAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 9
loc 9
ccs 0
cts 7
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 6
1
<?php
2
3
namespace SleepingOwl\Admin\Display\Column\Editable;
4
5
use Carbon\Carbon;
6
use Illuminate\Http\Request;
7
use SleepingOwl\Admin\Form\FormDefault;
8
use SleepingOwl\Admin\Traits\DateFormat;
9
use SleepingOwl\Admin\Traits\DatePicker;
10
use SleepingOwl\Admin\Contracts\Display\ColumnEditableInterface;
11
12
class DateTime extends EditableColumn implements ColumnEditableInterface
13
{
14
    use DatePicker, DateFormat;
15
16
    /**
17
     * @var string
18
     */
19
    protected $format = 'Y-m-d H:i:s';
20
//    protected $format = 'YYYY-MM-DD';
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
21
22
    protected $type = 'combodate';
23
24
    /**
25
     * @var string
26
     */
27
    protected $timezone;
28
29
    /**
30
     * @var bool
31
     */
32
    protected $seconds = false;
33
34
    /**
35
     * @var string
36
     */
37
    protected $view = 'column.editable.datetime';
38
39
    /**
40
     * Text constructor.
41
     *
42
     * @param             $name
43
     * @param             $label
44
     */
45
    public function __construct($name, $label = null)
46
    {
47
        parent::__construct($name, $label);
48
    }
49
50
    /**
51
     * @return array
52
     */
53
    public function toArray()
54
    {
55
        $value = $this->getModelValue();
56
57
        return parent::toArray() + [
58
                'id'             => $this->getModel()->getKey(),
59
                'value'          => $this->getFormatedDate($value),
60
                'isEditable'     => $this->getModelConfiguration()->isEditable($this->getModel()),
61
                'url'            => $this->getUrl(),
62
63
                'format'          => $this->getJsPickerFormat(),
64
                'viewformat'      => $this->getJsPickerFormat(),
65
                'data-date-pickdate'   => 'true',
66
                'data-date-picktime'   => 'false',
67
                'data-date-useseconds' => $this->hasSeconds() ? 'true' : 'false',
68
                'type'                 => $this->type,
69
            ];
70
    }
71
72
    /**
73
     * @param string $date
74
     *
75
     * @return null|string
76
     */
77 View Code Duplication
    protected function getFormatedDate($date)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
78
    {
79
        if (! is_null($date)) {
80
            if (! $date instanceof Carbon) {
81
                $date = Carbon::parse($date);
82
            }
83
84
            $date = $date->timezone($this->getTimezone())->format($this->getFormat());
85
        }
86
87
        return $date;
88
    }
89
90
    /**
91
     * @return $this|NamedFormElement|mixed|null|string
92
     */
93
    public function getValueFromModel()
94
    {
95
        $value = parent::getValueFromModel();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SleepingOwl\Admin\Displa...Editable\EditableColumn as the method getValueFromModel() does only exist in the following sub-classes of SleepingOwl\Admin\Displa...Editable\EditableColumn: SleepingOwl\Admin\Display\Column\Editable\DateTime. 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...
96
        if (! empty($value)) {
97
            return $this->parseValue($value);
0 ignored issues
show
Bug introduced by
The method parseValue() does not seem to exist on object<SleepingOwl\Admin...lumn\Editable\DateTime>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
98
        }
99
    }
100
101
    /**
102
     * @return bool
103
     */
104
    public function hasSeconds()
105
    {
106
        return (bool) $this->seconds;
107
    }
108
109
    /**
110
     * @param bool $seconds
111
     *
112
     * @return $this
113
     */
114
    public function setSeconds($seconds)
115
    {
116
        $this->seconds = $seconds;
117
118
        return $this;
119
    }
120
121
    /**
122
     * @param mixed $value
123
     *
124
     * @return void
125
     */
126 View Code Duplication
    public function setModelAttribute($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
127
    {
128
        $value = ! empty($value)
129
            ? Carbon::createFromFormat($this->getPickerFormat(), $value, $this->getTimezone())
130
                ->timezone(config('app.timezone'))->format($this->getFormat())
131
            : null;
132
133
        parent::setModelAttribute($value);
0 ignored issues
show
Bug introduced by
The method setModelAttribute() does not exist on SleepingOwl\Admin\Displa...Editable\EditableColumn. Did you maybe mean setModel()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
134
    }
135
136
    /**
137
     * @param Request $request
138
     *
139
     * @return void
140
     */
141
    public function save(Request $request)
142
    {
143
        $form = new FormDefault([
144
            new \SleepingOwl\Admin\Form\Element\Text(
145
                $this->getName()
146
            ),
147
        ]);
148
149
        $model = $this->getModel();
150
151
        $request->offsetSet($this->getName(), $request->input('value', null));
152
153
        $form->setModelClass(get_class($model));
154
        $form->initialize();
155
        $form->setId($model->getKey());
156
157
        $form->saveForm($request);
158
    }
159
160
    /**
161
     * @return string
162
     */
163
    public function getPickerFormat()
164
    {
165
        return $this->pickerFormat ?: config('sleeping_owl.datetimeFormat');
166
    }
167
168
    /**
169
     * @return $this
170
     *
171
     * SMELLS This function does more than it says.
172
     */
173
    public function setCurrentDate()
174
    {
175
        $this->defaultValue = Carbon::now()->timezone($this->getTimezone())->format($this->getFormat());
0 ignored issues
show
Bug introduced by
The property defaultValue does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
176
177
        return $this;
178
    }
179
}
180