GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

RequiredPositionsFilled::rolesThroughGroup()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.3149

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 10
ccs 4
cts 7
cp 0.5714
crap 2.3149
rs 10
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace BristolSU\Module\AssignRoles\CompletionConditions;
4
5
use BristolSU\ControlDB\Contracts\Models\Group;
6
use BristolSU\ControlDB\Contracts\Models\Role;
7
use BristolSU\ControlDB\Contracts\Repositories\Position;
8
use BristolSU\ControlDB\Contracts\Repositories\Role as RoleRepository;
9
use BristolSU\Module\AssignRoles\Fields\RequiredPositions;
10
use BristolSU\Module\AssignRoles\Support\RequiredSettingRetrieval;
11
use BristolSU\Module\AssignRoles\Support\SettingRetrievalException;
12
use BristolSU\Support\ActivityInstance\ActivityInstance;
13
use BristolSU\Support\Completion\Contracts\CompletionCondition;
14
use BristolSU\Support\Logic\Contracts\LogicRepository;
15
use BristolSU\Support\Logic\Facade\LogicTester;
16
use BristolSU\Support\ModuleInstance\Contracts\ModuleInstance;
17
use FormSchema\Generator\Field;
18
use FormSchema\Schema\Form;
19
20
class RequiredPositionsFilled extends CompletionCondition
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class RequiredPositionsFilled
Loading history...
21
{
22
23
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
24
     * @var RoleRepository
25
     */
26
    private $roleRepository;
0 ignored issues
show
Coding Style introduced by
Private member variable "roleRepository" must be prefixed with an underscore
Loading history...
27
28 2
    public function __construct(string $moduleAlias, RoleRepository $roleRepository)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
29
    {
30 2
        parent::__construct($moduleAlias);
31 2
        $this->roleRepository = $roleRepository;
32 2
    }
33
34
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $settings should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $activityInstance should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $moduleInstance should have a doc-comment as per coding-style.
Loading history...
35
     * @inheritDoc
36
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
37 2
    public function isComplete($settings, ActivityInstance $activityInstance, ModuleInstance $moduleInstance): bool
38
    {
39
        try {
40 2
            $remainingPositions = $this->positionsStillToFill($settings, $activityInstance, $moduleInstance);
41
        } catch (SettingRetrievalException $e) {
42
            return false;
43
        }
44
       
45 2
       return count($remainingPositions) === 0;
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 7
Loading history...
46
    }
47
    
48
    public function percentage($settings, ActivityInstance $activityInstance, ModuleInstance $moduleInstance): int
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function percentage()
Loading history...
49
    {
50
        $group = $this->getGroup($activityInstance);
51
52
        try {
53
            $requiredPositions = $this->getRequiredPositions($settings, $group, $moduleInstance);
54
            $remainingPositions = $this->positionsStillToFill($settings, $activityInstance, $moduleInstance);
55
        } catch (SettingRetrievalException $e) {
56
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the type-hinted return integer.
Loading history...
57
        }
58
        
59
        if(count($requiredPositions) === 0) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
60
            return 100;
61
        }
62
        
63
        $filled = count($requiredPositions) - count($remainingPositions);
64
        
65
        $percentage = (int) round(($filled/count($requiredPositions)) * 100, 0);
66
67
        if($percentage > 100) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
68
            return 100;
69
        }
70
        return $percentage;
71
    }
72
73 2
    protected function positionsStillToFill($settings, ActivityInstance $activityInstance, ModuleInstance $moduleInstance)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function positionsStillToFill()
Loading history...
74
    {
75 2
        $group = $this->getGroup($activityInstance);
76
        $roles = $this->rolesThroughGroup($group, $moduleInstance)->filter(function(Role $role) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
77 2
            return $role->users()->count() > 0;
78 2
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
79
        
80
        return collect($this->getRequiredPositions($settings, $group, $moduleInstance))->filter(function(int $positionId) use ($roles) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
81
            return $roles->filter(function(Role $role) use ($positionId) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
82 2
                return $role->positionId() === $positionId;
83 2
            })->count() === 0;
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
84 2
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
85
    }
86
    
87
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
88
     * @inheritDoc
89
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
90
    public function options(): Form
91
    {
92
        return \FormSchema\Generator\Form::make()->withField(
93
            Field::make(RequiredPositions::class, 'required')
94
                ->label('Required Positions')->featured(true)->required(true)
95
                ->default([])->hint('Define which positions are required')
96
                ->help('Define the positions that are required for any given logic group')
97
                ->logic($this->getLogic())
98
                ->positions($this->getPositions())
99
        )->getSchema();
100
    }
101
102
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
103
     * @inheritDoc
104
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
105
    public function name(): string
106
    {
107
        return 'Required Positions are filled';
108
    }
109
110
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
111
     * @inheritDoc
112
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
113
    public function description(): string
114
    {
115
        return 'Positions that are marked as required for a group have been filled';
116
    }
117
118
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
119
     * @inheritDoc
120
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
121
    public function alias(): string
122
    {
123
        return 'required_positions_are_filled';
124
    }
125
126
    private function getLogic()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getLogic()
Loading history...
Coding Style introduced by
Private method name "RequiredPositionsFilled::getLogic" must be prefixed with an underscore
Loading history...
127
    {
128
        try {
129
            return collect(app(LogicRepository::class)->all());
130
        } catch (\Exception $e) {
131
            return collect();
132
        }
133
    }
134
135
    private function getPositions()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getPositions()
Loading history...
Coding Style introduced by
Private method name "RequiredPositionsFilled::getPositions" must be prefixed with an underscore
Loading history...
136
    {
137
        try {
138
            return collect(app(Position::class)->all());
139
        } catch (\Exception $e) {
140
            return collect();
141
        }
142
    }
143
144
    /**
145
     * Get the group from an activity instance
146
     * 
147
     * @param ActivityInstance $activityInstance
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
148
     * @return Group|\BristolSU\ControlDB\Models\Group
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
149
     */
150 2
    private function getGroup(ActivityInstance $activityInstance)
0 ignored issues
show
Coding Style introduced by
Private method name "RequiredPositionsFilled::getGroup" must be prefixed with an underscore
Loading history...
151
    {
152 2
        if($activityInstance->resource_type === 'group') {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
153 2
            return $activityInstance->participant();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $activityInstance->participant() also could return the type BristolSU\ControlDB\Mode...U\ControlDB\Models\User which is incompatible with the documented return type BristolSU\ControlDB\Cont...\ControlDB\Models\Group.
Loading history...
154
        }
155
        return $activityInstance->participant()->group();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $activityInstance->participant()->group() also could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the documented return type BristolSU\ControlDB\Cont...\ControlDB\Models\Group.
Loading history...
156
    }
157
158 2
    private function getRequiredPositions(array $settings, Group $group, ModuleInstance $moduleInstance)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getRequiredPositions()
Loading history...
Coding Style introduced by
Private method name "RequiredPositionsFilled::getRequiredPositions" must be prefixed with an underscore
Loading history...
159
    {
160 2
        return app(RequiredSettingRetrieval::class)->getSettings($group, $settings, $moduleInstance);
161
    }
162
163 2
    private function rolesThroughGroup(Group $group, ModuleInstance $moduleInstance)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function rolesThroughGroup()
Loading history...
Coding Style introduced by
Private method name "RequiredPositionsFilled::rolesThroughGroup" must be prefixed with an underscore
Loading history...
164
    {
165 2
        $roles = $group->roles();
166 2
        if($this->logicGroupId($moduleInstance) !== null) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
167
            $logicGroup = app(LogicRepository::class)->getById($this->logicGroupId($moduleInstance));
0 ignored issues
show
Bug introduced by
It seems like $this->logicGroupId($moduleInstance) can also be of type null; however, parameter $id of BristolSU\Support\Logic\...icRepository::getById() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

167
            $logicGroup = app(LogicRepository::class)->getById(/** @scrutinizer ignore-type */ $this->logicGroupId($moduleInstance));
Loading history...
168
            return $roles->filter(function(\BristolSU\ControlDB\Contracts\Models\Role $role) use ($moduleInstance, $logicGroup) {
0 ignored issues
show
Unused Code introduced by
The import $moduleInstance is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
169
                return LogicTester::evaluate($logicGroup, null, $role->group(), $role);
170
            })->values();
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
171
        }
172 2
        return $roles;
173
    }
174
175 2
    private function logicGroupId(ModuleInstance $moduleInstance)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function logicGroupId()
Loading history...
Coding Style introduced by
Private method name "RequiredPositionsFilled::logicGroupId" must be prefixed with an underscore
Loading history...
176
    {
177 2
        $id = $moduleInstance->setting('logic_group', null);
178 2
        if($id === null) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
179 2
            return null;
180
        }
181
        return (int) $id;
182
    }
183
    
184
}