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

UpdaterRepository::create()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 20
nc 1
nop 1
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\Project\Issue\Comment;
13
14
use Tinyissue\Contracts\Repository\Project\Issue\Comment\UpdaterRepository as CommentUpdater;
15
use Tinyissue\Model\Project\Issue\Comment;
16
use Tinyissue\Model\User;
17
use Tinyissue\Repository\RepositoryUpdater;
18
19
class UpdaterRepository extends RepositoryUpdater implements CommentUpdater
20
{
21
    public function __construct(Comment $model)
22
    {
23
        $this->model = $model;
24
    }
25
26
    /**
27
     * Create new comment.
28
     *
29
     * @param array $input
30
     *
31
     * @return $this
32
     */
33
    public function create(array $input)
34
    {
35
        $fill = [
36
            'created_by' => $this->model->user->id,
37
            'project_id' => $this->model->project->id,
38
            'issue_id'   => $this->model->issue->id,
39
            'comment'    => $input['comment'],
40
        ];
41
42
        $this->model->fill($fill);
43
44
        // Add event on successful save
45
        static::saved(function (Issue\Comment $comment) {
0 ignored issues
show
Bug introduced by
The method saved() does not seem to exist on object<Tinyissue\Reposit...ment\UpdaterRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
            $this->queueAdd($comment, $comment->user);
0 ignored issues
show
Bug introduced by
The method queueAdd() does not seem to exist on object<Tinyissue\Reposit...ment\UpdaterRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
        });
48
49
        $this->save();
50
51
        /* Add to user's activity log */
52
        $this->model->activity()->save(new User\Activity([
53
            'type_id'   => Activity::TYPE_COMMENT,
54
            'parent_id' => $this->project->id,
0 ignored issues
show
Bug introduced by
The property project 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...
55
            'item_id'   => $this->issue->id,
0 ignored issues
show
Bug introduced by
The property issue 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...
56
            'user_id'   => $this->user->id,
0 ignored issues
show
Bug introduced by
The property user 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...
57
        ]));
58
59
        /* Add attachments to issue */
60
        Attachment::where('upload_token', '=', $input['upload_token'])
61
            ->where('uploaded_by', '=', $this->user->id)
62
            ->update(['issue_id' => $this->issue->id, 'comment_id' => $this->id]);
0 ignored issues
show
Bug introduced by
The property id 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...
63
64
        /* Update the project */
65
        $this->model->issue->changeUpdatedBy($this->model->user->id);
66
67
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Tinyissue\Repository\Pro...mment\UpdaterRepository) is incompatible with the return type of the parent method Tinyissue\Repository\RepositoryUpdater::create of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
68
    }
69
70
    /**
71
     * Update comment body.
72
     *
73
     * @param string        $body
74
     * @param UserInterface $user
75
     *
76
     * @return Eloquent\Model
77
     */
78
    public function updateBody($body, UserInterface $user)
79
    {
80
        $this->model->fill([
81
            'comment' => $body,
82
        ]);
83
84
        // Add event on successful save
85
        static::saved(function (Issue\Comment $comment) use ($user) {
0 ignored issues
show
Bug introduced by
The method saved() does not seem to exist on object<Tinyissue\Reposit...ment\UpdaterRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
            $this->queueUpdate($comment, $user);
0 ignored issues
show
Bug introduced by
The method queueUpdate() does not exist on Tinyissue\Repository\Pro...mment\UpdaterRepository. Did you maybe mean update()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
87
        });
88
89
        return $this->save();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->save(); (boolean) is incompatible with the return type declared by the interface Tinyissue\Contracts\Repo...rRepository::updateBody of type Tinyissue\Model\Project\Issue\Comment.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
90
    }
91
92
    public function delete()
93
    {
94
        return $this->transaction('deleteComment');
95
    }
96
97
    /**
98
     * Delete a comment and its attachments.
99
     *
100
     * @param UserInterface $user
0 ignored issues
show
Bug introduced by
There is no parameter named $user. 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...
101
     *
102
     * @return Eloquent\Model
103
     *
104
     * @throws \Exception
105
     */
106
    protected function deleteComment()
107
    {
108
        $this->model->activity()->delete();
109
110 View Code Duplication
        foreach ($this->model->attachments as $attachment) {
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...
111
            $path = config('filesystems.disks.local.root')
112
                . '/' . config('tinyissue.uploads_dir')
113
                . '/' . $this->model->project_id
114
                . '/' . $attachment->upload_token;
115
            $attachment->deleteFile($path, $attachment->filename);
116
            $attachment->delete();
117
        }
118
119
        // Add event on successful delete
120
        static::deleted(function (Issue\Comment $comment) {
0 ignored issues
show
Bug introduced by
The method deleted() does not exist on Tinyissue\Repository\Pro...mment\UpdaterRepository. Did you maybe mean delete()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
121
            $this->queueDelete($comment, $this->getLoggedUser());
0 ignored issues
show
Bug introduced by
The method queueDelete() does not exist on Tinyissue\Repository\Pro...mment\UpdaterRepository. Did you maybe mean delete()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
122
        });
123
124
        return $this->model->delete();
125
    }
126
}
127