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.
Passed
Push — develop ( 50df9e...3f084e )
by Toby
13:35
created

RequiredPositionsFilled::isComplete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.7861

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 2
b 0
f 0
nc 3
nop 3
dl 0
loc 18
ccs 5
cts 12
cp 0.4167
crap 4.7861
rs 9.8666
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\LogicRoleRepository;
11
use BristolSU\Module\AssignRoles\Support\RequiredSettingRetrieval;
12
use BristolSU\Module\AssignRoles\Support\SettingRetrievalException;
13
use BristolSU\Support\ActivityInstance\ActivityInstance;
14
use BristolSU\Support\Completion\Contracts\CompletionCondition;
15
use BristolSU\Support\Logic\Contracts\Audience\LogicAudience;
16
use BristolSU\Support\Logic\Contracts\LogicRepository;
17
use BristolSU\Support\Logic\Facade\LogicTester;
18
use BristolSU\Support\ModuleInstance\Contracts\ModuleInstance;
19
use FormSchema\Generator\Field;
20
use FormSchema\Schema\Form;
21
use Illuminate\Support\Collection;
22
use Illuminate\Support\Facades\Log;
23
24
class RequiredPositionsFilled extends CompletionCondition
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class RequiredPositionsFilled
Loading history...
25
{
26
27
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
28
     * @var RoleRepository
29
     */
30
    private $roleRepository;
0 ignored issues
show
Coding Style introduced by
Private member variable "roleRepository" must be prefixed with an underscore
Loading history...
31
32 2
    public function __construct(string $moduleAlias, RoleRepository $roleRepository)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
33
    {
34 2
        parent::__construct($moduleAlias);
35 2
        $this->roleRepository = $roleRepository;
36 2
    }
37
38
    /**
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...
39
     * @inheritDoc
40
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
41 2
    public function isComplete($settings, ActivityInstance $activityInstance, ModuleInstance $moduleInstance): bool
42
    {
43
        try {
44 2
            Log::info('--- TESTING ---');
45 2
            $remainingPositions = $this->positionsStillToFill($settings, $activityInstance, $moduleInstance);
46
        } catch (SettingRetrievalException $e) {
47
            Log::debug(
48
              sprintf(
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 14.
Loading history...
Coding Style introduced by
Opening statement of multi-line function call not indented correctly; expected 12 spaces but found 14
Loading history...
49
                'Setting could not be found for activity instance %d and module instance %d',
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 18 spaces, but found 16.
Loading history...
50
                $activityInstance->id,
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 18 spaces, but found 16.
Loading history...
51
                $moduleInstance->id()
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 18 spaces, but found 16.
Loading history...
52
              )
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 14.
Loading history...
53
            );
54
            return false;
55
        }
56
       
57 2
        Log::info('Result is ' . (count($remainingPositions) === 0 ? 'true' : 'false'));
58 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...
59
    }
60
    
61
    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...
62
    {
63
        $group = $this->getGroup($activityInstance);
64
65
        try {
66
            $requiredPositions = $this->getRequiredPositions($settings, $group);
67
            $remainingPositions = $this->positionsStillToFill($settings, $activityInstance, $moduleInstance);
68
        } catch (SettingRetrievalException $e) {
69
            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...
70
        }
71
        
72
        if(count($requiredPositions) === 0) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
73
            return 100;
74
        }
75
        
76
        $filled = count($requiredPositions) - count($remainingPositions);
77
        
78
        $percentage = (int) round(($filled/count($requiredPositions)) * 100, 0);
79
80
        if($percentage > 100) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
81
            return 100;
82
        }
83
        return $percentage;
84
    }
85
86 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...
87
    {
88 2
        $group = $this->getGroup($activityInstance);
89 2
        Log::info('Using group ' . $group->id());
90
        $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...
91 2
            return $role->users()->count() > 0;
92 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...
93
        
94
        Log::info('Roles to be used: ' . json_encode($roles->map(function($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...
95 2
            return $role->id();
96 2
          })));
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 10.
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 10
Loading history...
Coding Style introduced by
Closing brace indented incorrectly; expected 8 spaces, found 10
Loading history...
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...
97
        
98 2
        Log::info('Required positions ' . (string) collect($this->getRequiredPositions($settings, $group)));
99
        return collect($this->getRequiredPositions($settings, $group))->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...
100
            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...
101 2
                return $role->positionId() === $positionId;
102 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...
103 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...
104
    }
105
    
106
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
107
     * @inheritDoc
108
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
109
    public function options(): Form
110
    {
111
        return \FormSchema\Generator\Form::make()->withField(
112
            Field::make(RequiredPositions::class, 'required')
113
                ->label('Required Positions')->featured(true)->required(true)
114
                ->default([])->hint('Define which positions are required')
115
                ->help('Define the positions that are required for any given logic group')
116
                ->logic($this->getLogic())
117
                ->positions($this->getPositions())
118
        )->getSchema();
119
    }
120
121
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
122
     * @inheritDoc
123
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
124
    public function name(): string
125
    {
126
        return 'Required Positions are filled';
127
    }
128
129
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
130
     * @inheritDoc
131
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
132
    public function description(): string
133
    {
134
        return 'Positions that are marked as required for a group have been filled';
135
    }
136
137
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
138
     * @inheritDoc
139
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
140
    public function alias(): string
141
    {
142
        return 'required_positions_are_filled';
143
    }
144
145
    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...
146
    {
147
        try {
148
            return collect(app(LogicRepository::class)->all());
149
        } catch (\Exception $e) {
150
            return collect();
151
        }
152
    }
153
154
    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...
155
    {
156
        try {
157
            return collect(app(Position::class)->all());
158
        } catch (\Exception $e) {
159
            return collect();
160
        }
161
    }
162
163
    /**
164
     * Get the group from an activity instance
165
     * 
166
     * @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...
167
     * @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...
168
     */
169 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...
170
    {
171 2
        if($activityInstance->resource_type === 'group') {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
172 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...
173
        }
174
        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...
175
    }
176
177 2
    private function getRequiredPositions(array $settings, Group $group)
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...
178
    {
179 2
        return app(RequiredSettingRetrieval::class)->getSettings($group, $settings);
180
    }
181
182 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...
183
    {
184 2
        $roles = $group->roles();
185
        Log::info('Roles through group pre logic filtering: ' . json_encode($roles->map(function($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...
186 2
              return $role->id();
187 2
          })));
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 10.
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 10
Loading history...
Coding Style introduced by
Closing brace indented incorrectly; expected 8 spaces, found 10
Loading history...
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...
188 2
        if($this->logicGroupId($moduleInstance) !== null) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
189
            $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

189
            $logicGroup = app(LogicRepository::class)->getById(/** @scrutinizer ignore-type */ $this->logicGroupId($moduleInstance));
Loading history...
190
            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...
191
                return LogicTester::evaluate($logicGroup, null, $role->group(), $role);
192
            })->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...
193
        }
194
195
        Log::info('Roles through group post logic filtering: ' . json_encode($roles->map(function($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...
196 2
              return $role->id();
197 2
          })));
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 10.
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 10
Loading history...
Coding Style introduced by
Closing brace indented incorrectly; expected 8 spaces, found 10
Loading history...
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...
198
        
199 2
        return $roles;
200
    }
201
202 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...
203
    {
204 2
        $id = $moduleInstance->setting('logic_group', null);
205 2
        if($id === null) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
206 2
            return null;
207
        }
208
        return (int) $id;
209
    }
210
    
211
}