Completed
Push — develop ( d3eb6f...a3b428 )
by Mohamed
08:20
created

FormAbstract::getLoggedUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Form;
13
14
use Illuminate\Database\Eloquent\Model;
15
use Tinyissue\Model\Project;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Tinyissue\Form\Project.

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...
16
use Tinyissue\Model\User;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Tinyissue\Form\User.

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...
17
use Illuminate\Contracts\Auth\Guard;
18
19
/**
20
 * FormAbstract is an abstract class for Form classes.
21
 *
22
 * @author Mohamed Alsharaf <[email protected]>
23
 */
24
abstract class FormAbstract implements FormInterface
25
{
26
    /**
27
     * An instance of Model.
28
     *
29
     * @var Model
30
     */
31
    protected $model;
32
33
    /**
34
     * An instance of Current logged user.
35
     *
36
     * @var User
37
     */
38
    protected $user;
39
40
    /**
41
     * Set an instance of current logged user.
42
     *
43
     * @param User $user
44
     *
45
     * @return $this
46
     */
47 50
    public function setLoggedUser(User $user = null)
48
    {
49 50
        $this->user = $user;
50
51 50
        return $this;
52
    }
53
54
    /**
55
     * Returns an instance of current logged user.
56
     *
57
     * @return User
58
     */
59 15
    public function getLoggedUser()
60
    {
61 15
        if (null === $this->user) {
62 2
            $this->user = auth()->user();
0 ignored issues
show
Documentation Bug introduced by
It seems like auth()->user() can also be of type object<Illuminate\Contracts\Auth\Authenticatable>. However, the property $user is declared as type object<Tinyissue\Model\User>. 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...
63
        }
64
65 15
        return $this->user;
66
    }
67
68
    /**
69
     * Set an instance of model currently being edited.
70
     *
71
     * @param Model $model
72
     *
73
     * @return void|FormInterface
74
     */
75 11
    public function editingModel(Model $model)
76
    {
77 11
        $this->model = $model;
78
79 11
        return $this;
80
    }
81
82
    /**
83
     * Setup the object from the route parameters.
84
     *
85
     * @param array $params
86
     *
87
     * @return FormInterface
88
     */
89
    public function setup(array $params)
90
    {
91 24
        $model = array_first($params, function ($key, $value) {
92 3
            return $value instanceof Model;
93 24
        });
94 24
        if ($model) {
95 3
            $this->editingModel($model);
96
        }
97
98 24
        return $this;
99
    }
100
101
    /**
102
     * Whether or not the form is in editing of a model.
103
     *
104
     * @return bool
105
     */
106 30
    public function isEditing()
107
    {
108 30
        return $this->model instanceof Model;
109
    }
110
111
    /**
112
     * Return an instance of the model being edited.
113
     *
114
     * @return Model
115
     */
116 48
    public function getModel()
117
    {
118 48
        return $this->model;
119
    }
120
121
    /**
122
     * Returns form type.
123
     *
124
     * @return string
125
     */
126 39
    public function openType()
127
    {
128 39
        return 'open';
129
    }
130
131
    /**
132
     * Returns an array of form actions.
133
     *
134
     * @return array
135
     */
136 14
    public function actions()
137
    {
138 14
        return [];
139
    }
140
141
    /**
142
     * Returns an array of form fields.
143
     *
144
     * @return array
145
     */
146
    public function fields()
147
    {
148
        return [];
149
    }
150
151
    /**
152
     * Returns an array form rules.
153
     *
154
     * @return array
155
     */
156 29
    public function rules()
157
    {
158 29
        return [];
159
    }
160
161
    /**
162
     * Returns the form redirect url on error.
163
     *
164
     * @return string
165
     */
166
    public function getRedirectUrl()
167
    {
168
        return '';
169
    }
170
171
    /**
172
     * Returns project upload fields.
173
     *
174
     * @param string  $name
175
     * @param Project $project
176
     * @param User    $user
177
     *
178
     * @return array
179
     */
180 13
    protected function projectUploadFields($name, Project $project, User $user)
181
    {
182
        return [
183
            $name => [
184 13
                'type'                 => 'FileUpload',
185 13
                'data_message_success' => trans('tinyissue.success_upload'),
186 13
                'data_message_failed'  => trans('tinyissue.error_uploadfailed'),
187
                'multiple'             => null,
188 13
            ],
189 13
            $name . '_token' => [
190 13
                'type'  => 'hidden',
191 13
                'value' => md5($project->id . time() . $user->id . rand(1, 100)),
192
            ],
193
        ];
194
    }
195
}
196