Completed
Branch master (585519)
by Jeremy
40:45 queued 37:23
created

src/App/Services/PermissionFormFields.php (6 issues)

1
<?php
2
3
namespace jeremykenedy\LaravelRoles\App\Services;
4
5
use jeremykenedy\LaravelRoles\Traits\RolesAndPermissionsHelpersTrait;
6
7
class PermissionFormFields
8
{
9
    use RolesAndPermissionsHelpersTrait;
0 ignored issues
show
The trait jeremykenedy\LaravelRole...PermissionsHelpersTrait requires some properties which are not provided by jeremykenedy\LaravelRole...es\PermissionFormFields: $role_id, $user_id, $roles, $permission_id
Loading history...
10
11
    /**
12
     * List of fields and default value for each field.
13
     *
14
     * @var array
15
     */
16
    protected $fieldList = [
17
        'name'          => '',
18
        'slug'          => '',
19
        'description'   => '',
20
        'model'         => '',
21
    ];
22
23
    /**
24
     * Create a new job instance.
25
     *
26
     * @param int $id
27
     *
28
     * @return void
29
     */
30
    public function __construct($id = null)
31
    {
32
        $this->id = $id;
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
33
    }
34
35
    /**
36
     * Execute the job.
37
     *
38
     * @return void
39
     */
40
    public function handle()
41
    {
42
        $fields = $this->fieldList;
43
44
        if ($this->id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->id of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
45
            $fields = $this->fieldsFromModel($this->id, $fields);
46
        }
47
48
        foreach ($fields as $fieldName => $fieldValue) {
49
            $fields[$fieldName] = old($fieldName, $fieldValue);
50
        }
51
52
        // Get the additional data for the form fields
53
        $permissionFormFieldData = $this->permissionFormFieldData();
54
55
        return array_merge(
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_merge($fiel...ermissionFormFieldData) returns the type array which is incompatible with the documented return type void.
Loading history...
56
            $fields,
57
            $permissionFormFieldData
58
        );
59
60
        return $fields;
0 ignored issues
show
return $fields is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
61
    }
62
63
    /**
64
     * Return the field values from the model.
65
     *
66
     * @param int   $id
67
     * @param array $fields
68
     *
69
     * @return array
70
     */
71
    protected function fieldsFromModel($id, array $fields)
72
    {
73
        $permission = config('roles.models.permission')::findOrFail($id);
74
75
        $fieldNames = array_keys(array_except($fields, ['permissions']));
0 ignored issues
show
Deprecated Code introduced by
The function array_except() has been deprecated: Arr::except() should be used directly instead. Will be removed in Laravel 6.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

75
        $fieldNames = array_keys(/** @scrutinizer ignore-deprecated */ array_except($fields, ['permissions']));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
76
77
        $fields = [
78
            'id' => $id,
79
        ];
80
        foreach ($fieldNames as $field) {
81
            $fields[$field] = $permission->{$field};
82
        }
83
84
        return $fields;
85
    }
86
87
    /**
88
     * Get the additonal form fields data.
89
     *
90
     * @return array
91
     */
92
    protected function permissionFormFieldData()
93
    {
94
        return [
95
            'permissionModels' => $this->getPermissionModels(),
96
        ];
97
    }
98
}
99