Completed
Push — develop ( 1c3d8e...dc7d05 )
by Mohamed
07:44
created

UserMessagesSettings::actions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
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\Contracts\Auth\Guard;
15
use Illuminate\Support\Collection;
16
use Tinyissue\Model\Message;
17
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...
18
19
/**
20
 * UserMessagesSettings is a class to defines fields & rules for edit user message settings form.
21
 *
22
 * @author Mohamed Alsharaf <[email protected]>
23
 */
24
class UserMessagesSettings extends FormAbstract
25
{
26
    /**
27
     * @var Collection
28
     */
29
    protected $projects;
30
31
    /**
32
     * @var \Illuminate\Contracts\Auth\Authenticatable|null
33
     */
34
    protected $user;
35
36
    /**
37
     * @param Guard $model
38
     */
39
    public function __construct(Guard $model)
40
    {
41
        $this->user = $model->user();
42
    }
43
44
    /**
45
     * @param Collection $projects
46
     *
47
     * @return $this
48
     */
49
    public function setProjects(Collection $projects)
50
    {
51
        $this->projects = $projects;
52
53
        return $this;
54
    }
55
56
    /**
57
     * @return array
58
     */
59
    public function actions()
60
    {
61
        return [
62
            'submit' => 'update',
63
        ];
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    public function fields()
70
    {
71
        $fields = [];
72
73
        // Change label widths
74
        \Former::setOption('TwitterBootstrap3.labelWidths', ['large' => 4, 'small' => 4]);
75
76
        /** @var Collection $messages Available message options */
77
        $messages = Message::orderBy('id')->lists('name', 'id');
78
79
        // Create field for each project
80
        $this->projects->each(function (Project $project) use (&$fields, $messages) {
81
            $messageId = $project->projectUsers->first()->message_id;
0 ignored issues
show
Documentation introduced by
The property projectUsers does not exist on object<Tinyissue\Model\Project>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
82
            $fields['projects[' . $project->id . ']'] = [
83
                'type'    => 'select',
84
                'label'   => $project->name,
85
                'options' => $messages,
86
                'value'   => $messageId,
87
                'help'    => trans('tinyissue.messages_' . strtolower($messages->get($messageId) . '_help')),
88
            ];
89
        });
90
91
        return $fields;
92
    }
93
}
94