Completed
Branch develop-3.0 (4fe777)
by Mohamed
11:06
created

UpdaterRepository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 82
Duplicated Lines 51.22 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 42
loc 82
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 15 15 1
A delete() 11 11 1
A update() 5 10 3
A updateMessagesSettings() 11 11 1

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
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Repository\User;
13
14
use Hash;
15
use Tinyissue\Contracts\Model\UserInterface;
16
use Tinyissue\Contracts\Repository\User\UpdaterRepository as UserUpdater;
17
use Tinyissue\Model\Project\User as ProjectUser;
18
use Tinyissue\Model\User;
19
use Tinyissue\Repository\RepositoryUpdater;
20
21
/**
22
 * Class UpdaterRepository.
23
 *
24
 * @package Tinyissue\Repository\User
25
 */
26
class UpdaterRepository extends RepositoryUpdater implements UserUpdater
27
{
28
    public function __construct(UserInterface $model)
29
    {
30
        $this->model = $model;
0 ignored issues
show
Documentation Bug introduced by
It seems like $model of type object<Tinyissue\Contracts\Model\UserInterface> is incompatible with the declared type object<Illuminate\Database\Eloquent\Model> of property $model.

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...
31
    }
32
33
    /**
34
     * Add a new user.
35
     *
36
     * @param array $info
37
     *
38
     * @return bool
39
     */
40 View Code Duplication
    public function create(array $info)
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
        $insert = [
43
            'email'     => $info['email'],
44
            'firstname' => $info['firstname'],
45
            'lastname'  => $info['lastname'],
46
            'role_id'   => $info['role_id'],
47
            'private'   => (boolean) $info['private'],
48
            'password'  => Hash::make($info['password']),
49
            'status'    => $info['status'],
50
            'language'  => app('tinyissue.settings')->getLanguage(),
51
        ];
52
53
        return $this->model->fill($insert)->save();
54
    }
55
56
    /**
57
     * Soft deletes a user and empties the email.
58
     *
59
     * @return bool
60
     */
61 View Code Duplication
    public function delete()
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...
62
    {
63
        $this->model->update([
64
            'email'   => $this->email . '_deleted',
0 ignored issues
show
Bug introduced by
The property email does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
65
            'deleted' => User::DELETED_USERS,
66
        ]);
67
        $this->model->projects()->delete();
68
//        Project\User::where('user_id', '=', $this->id)->delete();
69
70
        return true;
71
    }
72
73
    /**
74
     * Updates the users settings, validates the fields.
75
     *
76
     * @param array $info
0 ignored issues
show
Bug introduced by
There is no parameter named $info. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
77
     *
78
     * @return Eloquent\Model
79
     */
80
    public function update(array $attributes = [], array $options = [])
81
    {
82 View Code Duplication
        if ($attributes['password']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
83
            $attributes['password'] = Hash::make($attributes['password']);
84
        } elseif (empty($attributes['password'])) {
85
            unset($attributes['password']);
86
        }
87
88
        return $this->model->update($attributes, $options);
89
    }
90
91
    /**
92
     * Update user messages setting.
93
     *
94
     * @param array $input
95
     */
96 View Code Duplication
    public function updateMessagesSettings(array $input)
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...
97
    {
98
        return (new ProjectUser())
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Tinyissue\Model\Project\User>? 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...
99
            ->where('user_id', '=', $this->model->id)
100
            ->whereIn('project_id', array_keys($input))
101
            ->get()
102
            ->each(function (ProjectUser $project) use ($input) {
103
                $project->message_id = $input[$project->project_id];
104
                $project->save();
105
            });
106
    }
107
}
108