Completed
Push — develop ( 335c93...e4583a )
by Mohamed
12:58
created

AdministrationController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 5
c 5
b 1
f 0
lcom 1
cbo 7
dl 0
loc 71
ccs 16
cts 16
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getIndex() 0 12 1
A getSettings() 0 7 1
A postSettings() 0 6 1
A getChangeMaintenanceMode() 0 12 2
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 Tinyissue\Form\Settings as FormSettings;
15
use Tinyissue\Http\Requests\FormRequest;
16
use Tinyissue\Model\Project;
17
use Tinyissue\Model\Tag;
18
use Tinyissue\Model\User;
19
use Tinyissue\Services\SettingsManager;
20
21
/**
22
 * AdministrationController is the controller class for managing request related the application system admin.
23
 *
24
 * @author Mohamed Alsharaf <[email protected]>
25
 */
26
class AdministrationController extends Controller
27
{
28
    /**
29
     * Show general application stats.
30
     *
31
     * @param Tag     $tag
32
     * @param Project $project
33
     * @param User    $user
34
     *
35
     * @return \Illuminate\View\View
36
     */
37 2
    public function getIndex(Tag $tag, Project $project, User $user)
38
    {
39 2
        return view('administration.index', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('administration.ind...gs' => $tag->count())); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 39 which is incompatible with the return type documented by Tinyissue\Http\Controlle...ionController::getIndex of type Illuminate\View\View.
Loading history...
40 2
            'users'             => $user->countUsers(),
41 2
            'active_projects'   => $project->countOpenProjects(),
42 2
            'archived_projects' => $project->countArchivedProjects(),
43 2
            'open_issues'       => $project->countOpenIssues(),
44 2
            'closed_issues'     => $project->countClosedIssues(),
45 2
            '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...
46 2
            'tags'              => $tag->count(),
47
        ]);
48
    }
49
50
    /**
51
     * Add new tag page.
52
     *
53
     * @param FormSettings $form
54
     *
55
     * @return \Illuminate\View\View
56
     */
57 1
    public function getSettings(FormSettings $form)
58
    {
59 1
        return view('administration.settings', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('administration.set...)->projects()->get())); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 59 which is incompatible with the return type documented by Tinyissue\Http\Controlle...Controller::getSettings of type Illuminate\View\View.
Loading history...
60 1
            'form'     => $form,
61 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...
62
        ]);
63
    }
64
65
    /**
66
     * To create new tag.
67
     *
68
     * @param FormRequest\Settings $request
69
     *
70
     * @return \Illuminate\Http\RedirectResponse
71
     */
72 1
    public function postSettings(FormRequest\Settings $request)
73
    {
74 1
        app('tinyissue.settings')->save($request->except('_token'));
75
76 1
        return redirect('administration/settings')->with('notice', trans('tinyissue.settings_saved'));
77
    }
78
79
    /**
80
     * Toggle site maintenance mode.
81
     *
82
     * @return \Illuminate\Http\RedirectResponse
83
     */
84
    public function getChangeMaintenanceMode()
85
    {
86
        $action = 'down';
87
        if (app()->isDownForMaintenance()) {
88
            $action = 'up';
89
            app('session')->remove('notice-error');
90
        }
91
92
        \Artisan::call($action);
93
94
        return redirect('administration');
95
    }
96
}
97