Completed
Push — master ( 91f8d9...64265e )
by Mohamed
09:45 queued 06:57
created

ProjectsController::getIndex()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 34
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 5.0128

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 34
ccs 23
cts 25
cp 0.92
rs 8.439
cc 5
eloc 28
nc 8
nop 1
crap 5.0128
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\Http\Controllers;
13
14
use Illuminate\Http\Request;
15
use Tinyissue\Form\Project as Form;
16
use Tinyissue\Form\GlobalIssue as IssueForm;
17
use Tinyissue\Http\Requests\FormRequest;
18
use Tinyissue\Model\Project;
19
use Tinyissue\Model\User;
20
21
/**
22
 * ProjectsController is the controller class for managing request related to projects.
23
 *
24
 * @author Mohamed Alsharaf <[email protected]>
25
 */
26
class ProjectsController extends Controller
27
{
28
    /**
29
     * Display list of active/archived projects.
30
     *
31
     * @param int $status
32
     *
33
     * @return \Illuminate\View\View
34
     */
35 5
    public function getIndex($status = Project::STATUS_OPEN)
36
    {
37 5
        $viewData = [];
38 5
        if (!$this->auth->guest()) {
39 3
            $projects = $this->auth->user()->projectsWithCountOpenIssues($status)->get();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Authenticatable as the method projectsWithCountOpenIssues() does only exist in the following implementations of said interface: Tinyissue\Model\User.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
40 3
            if ($status) {
41 3
                $countActive   = $projects->count();
42 3
                $countArchived = $this->auth->user()->projectsWithCountOpenIssues(Project::STATUS_ARCHIVED)->count();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Authenticatable as the method projectsWithCountOpenIssues() does only exist in the following implementations of said interface: Tinyissue\Model\User.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
43
            } else {
44 2
                $countActive   = $this->auth->user()->projectsWithCountOpenIssues(Project::STATUS_OPEN)->count();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Authenticatable as the method projectsWithCountOpenIssues() does only exist in the following implementations of said interface: Tinyissue\Model\User.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
45 2
                $countArchived = $projects->count();
46
            }
47 3
            $viewData['projects'] = $this->auth->user()->projects()->get();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Authenticatable as the method projects() does only exist in the following implementations of said interface: Tinyissue\Model\User.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
48
        } else {
49 2
            $viewData['sidebar'] = 'public';
50 2
            $project             = new Project();
51 2
            $projects            = $project->projectsWithOpenIssuesCount($status, Project::PRIVATE_NO)->get();
52 2
            if ($status) {
53 2
                $countActive   = $projects->count();
54 2
                $countArchived = $project->projectsWithOpenIssuesCount(Project::STATUS_ARCHIVED, Project::PRIVATE_NO)->count();
55
            } else {
56
                $countActive   = $project->projectsWithOpenIssuesCount(Project::STATUS_OPEN, Project::PRIVATE_NO)->count();
57
                $countArchived = $projects->count();
58
            }
59 2
            $user                    = new User();
60 2
            $viewData['activeUsers'] = $user->activeUsers();
61
        }
62 5
        $viewData['content_projects'] = $projects;
63 5
        $viewData['active']           = $status === Project::STATUS_OPEN ? 'active' : 'archived';
64 5
        $viewData['active_count']     = $countActive;
65 5
        $viewData['archived_count']   = $countArchived;
66
67 5
        return view('projects.index', $viewData);
0 ignored issues
show
Bug Compatibility introduced by
The expression view('projects.index', $viewData); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 67 which is incompatible with the return type documented by Tinyissue\Http\Controlle...ctsController::getIndex of type Illuminate\View\View.
Loading history...
68
    }
69
70
    /**
71
     * Add new project form.
72
     *
73
     * @param Form $form
74
     *
75
     * @return \Illuminate\View\View
76
     */
77 1
    public function getNew(Form $form)
78
    {
79 1
        return view('projects.new', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('projects.new', arr...)->projects()->get())); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 79 which is incompatible with the return type documented by Tinyissue\Http\Controlle...jectsController::getNew of type Illuminate\View\View.
Loading history...
80 1
            'form'     => $form,
81 1
            'projects' => $this->auth->user()->projects()->get(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Authenticatable as the method projects() does only exist in the following implementations of said interface: Tinyissue\Model\User.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
82
        ]);
83
    }
84
85
    /**
86
     * To create a new project.
87
     *
88
     * @param Project             $project
89
     * @param FormRequest\Project $request
90
     *
91
     * @return \Illuminate\Http\RedirectResponse
92
     */
93 1
    public function postNew(Project $project, FormRequest\Project $request)
94
    {
95 1
        $project->createProject($request->all());
96
97 1
        return redirect($project->to());
98
    }
99
100
    /**
101
     * Ajax: Calculate the progress of user projects.
102
     *
103
     * @param Request $request
104
     * @param Project $project
105
     *
106
     * @return \Symfony\Component\HttpFoundation\Response
107
     */
108 1
    public function postProgress(Request $request, Project $project)
109
    {
110
        // Get all projects with count of closed and opened issues
111 1
        $projects = $project->projectsWithCountIssues((array) $request->input('ids'));
112
113
        // The project progress Html and value
114 1
        $progress = $projects->transform(function (Project $project) {
115 1
            $progress = $project->getProgress();
116 1
            $view = view('partials/progress', ['text' => $progress . '%', 'progress' => $progress])->render();
1 ignored issue
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
117
118
            return [
119 1
                'id'       => $project->id,
120
                'progress' => [
121 1
                    'html'  => $view,
122 1
                    'value' => $progress,
123
                ],
124
            ];
125 1
        })->lists('progress', 'id')->all();
126
127 1
        return response()->json(['status' => true, 'progress' => $progress]);
128
    }
129
130
    /**
131
     * Add new issue form.
132
     *
133
     * @param IssueForm $form
134
     *
135
     * @return \Illuminate\View\View
136
     */
137 1
    public function getNewIssue(IssueForm $form)
138
    {
139 1
        return view('projects.new-issue', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('projects.new-issue...)->projects()->get())); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 139 which is incompatible with the return type documented by Tinyissue\Http\Controlle...Controller::getNewIssue of type Illuminate\View\View.
Loading history...
140 1
            'form'     => $form,
141 1
            'projects' => $this->auth->user()->projects()->get(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Authenticatable as the method projects() does only exist in the following implementations of said interface: Tinyissue\Model\User.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
142
        ]);
143
    }
144
145
    /**
146
     * To create a new issue.
147
     *
148
     * @param Project\Issue           $issue
149
     * @param FormRequest\GlobalIssue $request
150
     *
151
     * @return \Illuminate\Http\RedirectResponse
152
     */
153 1
    public function postNewIssue(Project\Issue $issue, FormRequest\GlobalIssue $request)
154
    {
155 1
        $project = Project::find((int) $request->input('project'));
156
157 1
        $issue->setRelation('project', $project);
158 1
        $issue->setRelation('user', $this->auth->user());
159 1
        $issue->createIssue([
160 1
            'title'        => $request->input('title'),
161 1
            'body'         => $request->input('body'),
162 1
            'tag_type'     => $request->input('tag_type'),
163 1
            'upload_token' => $request->input('upload_token'),
164 1
            'assigned_to'  => (int) $project->default_assignee,
165 1
            'time_quote'   => 0,
166
        ]);
167
168 1
        return redirect($issue->to())
169 1
            ->with('notice', trans('tinyissue.issue_has_been_created'));
170
    }
171
}
172