Completed
Branch develop-3.0 (4fe777)
by Mohamed
11:06
created

FormAbstract::isEditing()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
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 Illuminate\Foundation\Application;
16
use Tinyissue\Contracts\Form\FormInterface;
17
use Tinyissue\Contracts\Model\UserInterface;
18
use Tinyissue\Contracts\Repository\Project\ProjectRepository;
19
use Tinyissue\Contracts\Repository\RepositoryInterface;
20
use Tinyissue\Extensions\Auth\LoggedUser;
21
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...
22
23
/**
24
 * FormAbstract is an abstract class for Form classes.
25
 *
26
 * @author Mohamed Alsharaf <[email protected]>
27
 */
28
abstract class FormAbstract implements FormInterface
29
{
30
    use LoggedUser;
31
32
    /**
33
     * An instance of model repository.
34
     *
35
     * @var RepositoryInterface
36
     */
37
    protected $repository;
38
39
    /**
40
     * @var Application
41
     */
42
    protected $app;
43
44
    public function __construct(Application $app)
45
    {
46
        $this->app = $app;
47
    }
48
49
    /**
50
     * Set an instance of the repository that own the model being edited.
51
     *
52
     * @param RepositoryInterface $repository
53
     *
54
     * @return void|FormInterface
55
     */
56
    public function setRepository(RepositoryInterface $repository = null)
57
    {
58
        $this->repository = $repository;
59
60
        return $this;
61
    }
62
63
    /**
64
     * Return an instance of the repository that own the model being edited.
65
     *
66
     * @return RepositoryInterface
67
     */
68
    public function getRepository()
69
    {
70
        return $this->repository;
71
    }
72
73
    public function getModel()
74
    {
75
        if ($this->getRepository() instanceof RepositoryInterface) {
76
            return $this->getRepository()->getModel();
77
        }
78
79
        return null;
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
        // Get the first repository instance from param & set it as the owner of the form.
92
        $repository = array_first($params, function ($key, $value) {
93
            return $value instanceof RepositoryInterface;
94
        });
95
        $this->setRepository($repository);
96
97
        return $this;
98
    }
99
100
    /**
101
     * Whether or not the form is in editing of a model.
102
     *
103
     * @return bool
104
     */
105
    public function isEditing()
106
    {
107
        return $this->getModel() instanceof Model && $this->getModel()->id > 0;
108
    }
109
110
    /**
111
     * Returns form type.
112
     *
113
     * @return string
114
     */
115
    public function openType()
116
    {
117
        return 'open';
118
    }
119
120
    /**
121
     * Returns an array of form actions.
122
     *
123
     * @return array
124
     */
125
    public function actions()
126
    {
127
        return [];
128
    }
129
130
    /**
131
     * Returns an array of form fields.
132
     *
133
     * @return array
134
     */
135
    public function fields()
136
    {
137
        return [];
138
    }
139
140
    /**
141
     * Returns an array form rules.
142
     *
143
     * @return array
144
     */
145
    public function rules()
146
    {
147
        return [];
148
    }
149
150
    /**
151
     * Returns the form redirect url on error.
152
     *
153
     * @return string
154
     */
155
    public function getRedirectUrl()
156
    {
157
        return '';
158
    }
159
160
    /**
161
     * Returns project upload fields.
162
     *
163
     * @param string            $name
164
     * @param ProjectRepository $project
165
     * @param UserInterface     $user
166
     *
167
     * @return array
168
     */
169
    protected function projectUploadFields($name, ProjectRepository $project, UserInterface $user)
170
    {
171
        return [
172
            $name => [
173
                'type'                 => 'FileUpload',
174
                'data_message_success' => trans('tinyissue.success_upload'),
175
                'data_message_failed'  => trans('tinyissue.error_uploadfailed'),
176
                'multiple'             => null,
177
            ],
178
            $name . '_token' => [
179
                'type'  => 'hidden',
180
                'value' => md5($project->getModel()->id . time() . $user->id . rand(1, 100)),
0 ignored issues
show
Bug introduced by
Accessing id on the interface Tinyissue\Contracts\Model\UserInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
181
            ],
182
        ];
183
    }
184
}
185