Completed
Branch develop (a31570)
by Mohamed
08:09 queued 04:45
created

AdministrationController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 5
c 5
b 0
f 1
lcom 1
cbo 8
dl 0
loc 65
ccs 9
cts 9
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getIndex() 0 12 1
A getSettings() 0 7 1
A postSettings() 0 16 3
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\Settings;
18
use Tinyissue\Model\Tag;
19
use Tinyissue\Model\User;
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 1
    public function getIndex(Tag $tag, Project $project, User $user)
38
    {
39 1
        return view('administration.index', [
40 1
            'users'             => $user->countUsers(),
41 1
            'active_projects'   => $project->countOpenProjects(),
42 1
            'archived_projects' => $project->countArchivedProjects(),
43 1
            'open_issues'       => $project->countOpenIssues(),
44 1
            'closed_issues'     => $project->countClosedIssues(),
45 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...
46 1
            '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
    public function getSettings(FormSettings $form)
58
    {
59
        return view('administration.settings', [
60
            'form'     => $form,
61
            '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 Settings $settings
69
     * @param FormRequest\Settings $request
70
     *
71
     * @return \Illuminate\Http\RedirectResponse
72
     */
73
    public function postSettings(Settings $settings, FormRequest\Settings $request)
0 ignored issues
show
Unused Code introduced by
The parameter $settings 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...
74
    {
75
        $settingsToSave = $request->except('_token');
76
77
        foreach ($settingsToSave as $name => $value) {
78
            $settings = new Settings();
79
            $setting = $settings->where('key', '=', $name)->first();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Tinyissue\Model\Settings>? 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...
80
            if ($setting) {
81
                $setting->value = $value;
82
                $setting->save();
83
            }
84
            unset($settings, $setting);
85
        }
86
87
        return redirect('administration/settings')->with('notice', trans('tinyissue.settings_saved'));
88
    }
89
90
}
91