Completed
Push — develop-3.0 ( 4fe777...24fc5d )
by Mohamed
09:15
created

AttachmentPolicy::view()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Tinyissue\Policies;
4
5
use Tinyissue\Contracts\Model\UserInterface;
6
use Tinyissue\Model\Project;
7
use Tinyissue\Model\User;
8
use Tinyissue\Model\Project\Issue\Attachment;
9
use Illuminate\Auth\Access\HandlesAuthorization;
10
11
class AttachmentPolicy
12
{
13
    use HandlesAuthorization;
14
15 View Code Duplication
    public function before(UserInterface $user)
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...
16
    {
17
        $this->dd(__METHOD__);
0 ignored issues
show
Bug introduced by
The method dd() does not seem to exist on object<Tinyissue\Policies\AttachmentPolicy>.

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...
18
        if ($user instanceof UserInterface && ($user->isAdmin() || $user->isManager())) {
19
            return true;
20
        }
21
    }
22
23
    /**
24
     * Determine whether the user can view the attachment.
25
     *
26
     * @param  Tinyissue\User  $user
27
     * @param  Tinyissue\Attachment  $attachment
28
     * @return mixed
29
     */
30
    public function view(User $user, Attachment $attachment)
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $attachment is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
    {
32
        //
33
        $this->dd(__METHOD__);
0 ignored issues
show
Bug introduced by
The method dd() does not seem to exist on object<Tinyissue\Policies\AttachmentPolicy>.

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...
34
    }
35
36
    /**
37
     * Determine whether the user can create attachments.
38
     *
39
     * @param  Tinyissue\User  $user
40
     * @return mixed
41
     */
42
    public function create(User $user, Project $project)
43
    {
44
        $this->dd(__METHOD__);
0 ignored issues
show
Bug introduced by
The method dd() does not seem to exist on object<Tinyissue\Policies\AttachmentPolicy>.

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...
45
        return $project->isMember($user->id);
46
    }
47
48
    /**
49
     * Determine whether the user can update the attachment.
50
     *
51
     * @param  Tinyissue\User  $user
52
     * @param  Tinyissue\Attachment  $attachment
0 ignored issues
show
Bug introduced by
There is no parameter named $attachment. 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...
53
     * @return mixed
54
     */
55
    public function update(User $user, Project $project)
56
    {
57
        $this->dd(__METHOD__);
0 ignored issues
show
Bug introduced by
The method dd() does not seem to exist on object<Tinyissue\Policies\AttachmentPolicy>.

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...
58
        return $this->create($user, $project);
59
    }
60
61
    /**
62
     * Determine whether the user can delete the attachment.
63
     *
64
     * @param  Tinyissue\User  $user
65
     * @param  Tinyissue\Attachment  $attachment
0 ignored issues
show
Bug introduced by
There is no parameter named $attachment. 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...
66
     * @return mixed
67
     */
68
    public function delete(User $user, Project $project)
69
    {
70
        $this->dd(__METHOD__);
0 ignored issues
show
Bug introduced by
The method dd() does not seem to exist on object<Tinyissue\Policies\AttachmentPolicy>.

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...
71
        return $this->create($user, $project);
72
    }
73
74
75
76
    /**
77
     * Whether a user can edit the attachment.
78
     *
79
     * @param UserInterface $user
80
     *
81
     * @return bool
82
     */
83
    public function canEdit(UserInterface $user)
84
    {
85
        return $user->id === $this->uploaded_by || ($this->canView($user) && $user->permission(Permission::PERM_ISSUE_MODIFY));
0 ignored issues
show
Bug introduced by
Accessing id on the interface Tinyissue\Contracts\Model\UserInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
The property uploaded_by 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...
Bug introduced by
The method canView() does not seem to exist on object<Tinyissue\Policies\AttachmentPolicy>.

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...
Bug introduced by
The method permission() does not seem to exist on object<Tinyissue\Contracts\Model\UserInterface>.

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
    }
87
88
    /**
89
     * @param string        $permission
90
     * @param UserInterface $user
91
     *
92
     * @return bool
93
     */
94
    public function can($permission, UserInterface $user)
95
    {
96
        $editPermissions = [
97
            Permission::PERM_ISSUE_CREATE,
98
            Permission::PERM_ISSUE_MODIFY,
99
        ];
100
101
        if (in_array($permission, $editPermissions)) {
102
            return $this->canEdit($user);
103
        }
104
105
        return $this->canView($user);
0 ignored issues
show
Bug introduced by
The method canView() does not seem to exist on object<Tinyissue\Policies\AttachmentPolicy>.

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...
106
    }
107
}
108