Completed
Push — develop ( 335c93...e4583a )
by Mohamed
12:58
created

UserMessagesSettings::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
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\Support\Collection;
15
use Tinyissue\Model\Message;
16
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...
17
18
/**
19
 * UserMessagesSettings is a class to defines fields & rules for edit user message settings form.
20
 *
21
 * @author Mohamed Alsharaf <[email protected]>
22
 */
23
class UserMessagesSettings extends FormAbstract
24
{
25
    /**
26
     * @var Collection
27
     */
28
    protected $projects;
29
30
    /**
31
     * @param Collection $projects
32
     *
33
     * @return $this
34
     */
35
    public function setProjects(Collection $projects)
36
    {
37
        $this->projects = $projects;
38
39
        return $this;
40
    }
41
42
    /**
43
     * @return array
44
     */
45
    public function actions()
46
    {
47
        return [
48
            'submit' => 'update',
49
        ];
50
    }
51
52
    /**
53
     * @return array
54
     */
55
    public function fields()
56
    {
57
        $fields = [];
58
59
        // Change label widths
60
        \Former::setOption('TwitterBootstrap3.labelWidths', ['large' => 4, 'small' => 4]);
61
62
        /** @var Collection $messages Available message options */
63
        $messages = Message::orderBy('id')->lists('name', 'id');
64
65
        // Create field for each project
66
        $this->projects->each(function (Project $project) use (&$fields, $messages) {
67
            $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...
68
            $fields['projects[' . $project->id . ']'] = [
69
                'type'    => 'select',
70
                'label'   => $project->name,
71
                'options' => $messages,
72
                'value'   => $messageId,
73
                'help'    => trans('tinyissue.messages_' . strtolower($messages->get($messageId) . '_help')),
74
            ];
75
        });
76
77
        return $fields;
78
    }
79
}
80