Completed
Push — master ( 3b0b6a...263c36 )
by Guillaume
02:53
created

ChooseProject   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 34.04 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 16
loc 47
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getNoProject() 0 8 1
A getServiceProjects() 16 16 2
A scriptFilter() 0 18 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Godbout\Alfred\Time\Menus;
4
5
use Godbout\Alfred\Time\Workflow;
6
use Godbout\Alfred\Workflow\Icon;
7
use Godbout\Alfred\Workflow\Item;
8
use Godbout\Alfred\Workflow\ScriptFilter;
9
10
class ChooseProject extends Menu
11
{
12
    public static function scriptFilter()
13
    {
14
        $service = Workflow::serviceEnabled();
15
16
        if ($service->allowsEmptyProject) {
17
            ScriptFilter::add(self::getNoProject());
18
        }
19
20
        foreach (self::getServiceProjects($service) as $project) {
21
            ScriptFilter::add($project);
22
        }
23
24
        if (self::userInput()) {
25
            ScriptFilter::filterItems(self::userInput());
26
        }
27
28
        ScriptFilter::sortItems('asc', 'match');
29
    }
30
31
    private static function getNoProject()
32
    {
33
        return Item::create()
34
            ->title('No project')
35
            ->subtitle('Timer will be created without a project')
36
            ->match('')
37
            ->arg('choose_tag');
38
    }
39
40 View Code Duplication
    private static function getServiceProjects($service)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        $projects = [];
43
44
        foreach ($service->projects() as $id => $name) {
45
            $projects[] = Item::create()
46
                ->title($name)
47
                ->variable('timer_project_id', $id)
48
                ->variable('timer_project_name', $name)
49
                ->match($name)
50
                ->arg('choose_tag')
51
                ->icon(Icon::create("resources/icons/$service.png"));
52
        }
53
54
        return $projects;
55
    }
56
}
57