Completed
Push — master ( 9abbc7...a687e5 )
by Fabrizio
03:03
created

GroupCategoryRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 78
Duplicated Lines 28.21 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 4
Bugs 4 Features 1
Metric Value
wmc 5
c 4
b 4
f 1
lcom 1
cbo 0
dl 22
loc 78
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A addCategoryToGroupById() 0 7 1
A addCategoryToGroupByName() 10 10 1
A addMultipleCategoriesToGroup() 12 12 2

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 Fenos\Notifynder\Groups;
4
5
use Fenos\Notifynder\Contracts\NotifynderCategory;
6
use Fenos\Notifynder\Contracts\NotifynderGroupCategoryDB;
7
use Fenos\Notifynder\Models\NotificationCategory;
8
use Fenos\Notifynder\Models\NotificationGroup;
9
10
/**
11
 * Class NotificationGroupCategoryRepository.
12
 */
13
class GroupCategoryRepository implements NotifynderGroupCategoryDB
14
{
15
    /**
16
     * @var NotificationGroup
17
     */
18
    protected $notificationGroup;
19
20
    /**
21
     * @var NotificationCategory
22
     */
23
    protected $notificationCategory;
24
25
    /**
26
     * @param NotifynderCategory $notificationCategory
27
     * @param NotificationGroup  $notificationGroup
28
     */
29
    public function __construct(NotifynderCategory $notificationCategory,
30
                         NotificationGroup $notificationGroup)
31
    {
32
        $this->notificationCategory = $notificationCategory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $notificationCategory of type object<Fenos\Notifynder\...cts\NotifynderCategory> is incompatible with the declared type object<Fenos\Notifynder\...s\NotificationCategory> of property $notificationCategory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
33
        $this->notificationGroup = $notificationGroup;
34
    }
35
36
    /**
37
     * Add a category in a group.
38
     *
39
     * @param  $groupId
40
     * @param  $categoryId
41
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|static
42
     */
43
    public function addCategoryToGroupById($groupId, $categoryId)
44
    {
45
        $group = $this->notificationGroup->find($groupId);
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<Fenos\Notifynder\Models\NotificationGroup>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
46
        $group->categories()->attach($categoryId);
47
48
        return $group;
49
    }
50
51
    /**
52
     * Add a category in a group
53
     * by names given.
54
     *
55
     * @param $groupName
56
     * @param $categoryName
57
     * @return mixed
58
     */
59 View Code Duplication
    public function addCategoryToGroupByName($groupName, $categoryName)
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...
60
    {
61
        $group = $this->notificationGroup->where('name', $groupName)->first();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Fenos\Notifynder\Models\NotificationGroup>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
62
63
        $category = $this->notificationCategory->findByName($categoryName);
0 ignored issues
show
Documentation Bug introduced by
The method findByName does not exist on object<Fenos\Notifynder\...s\NotificationCategory>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
64
65
        $group->categories()->attach($category->id);
66
67
        return $group;
68
    }
69
70
    /**
71
     * Add multiple categories by them names
72
     * to a group.
73
     *
74
     * @param $groupName
75
     * @param $names
76
     * @return mixed
77
     */
78 View Code Duplication
    public function addMultipleCategoriesToGroup($groupName, array $names)
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...
79
    {
80
        $group = $this->notificationGroup->where('name', $groupName)->first();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Fenos\Notifynder\Models\NotificationGroup>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
81
82
        $categories = $this->notificationCategory->findByNames($names);
0 ignored issues
show
Documentation Bug introduced by
The method findByNames does not exist on object<Fenos\Notifynder\...s\NotificationCategory>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
83
84
        foreach ($categories as $category) {
85
            $group->categories()->attach($category->id);
86
        }
87
88
        return $group;
89
    }
90
}
91