Completed
Push — develop ( 78dac2...5cd299 )
by Mohamed
06:27
created

AdministrationController::postSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 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\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
20
/**
21
 * AdministrationController is the controller class for managing request related the application system admin
22
 *
23
 * @author Mohamed Alsharaf <[email protected]>
24
 */
25
class AdministrationController extends Controller
26
{
27
    /**
28
     * Show general application stats
29
     *
30
     * @param Tag $tag
31
     * @param Project $project
32
     * @param User $user
33
     *
34
     * @return \Illuminate\View\View
35
     */
36 2
    public function getIndex(Tag $tag, Project $project, User $user)
37
    {
38 2
        return view('administration.index', [
39 2
            'users'             => $user->countUsers(),
40 2
            'active_projects'   => $project->countOpenProjects(),
41 2
            'archived_projects' => $project->countArchivedProjects(),
42 2
            'open_issues'       => $project->countOpenIssues(),
43 2
            'closed_issues'     => $project->countClosedIssues(),
44 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...
45 2
            'tags'              => $tag->count(),
46
        ]);
47
    }
48
49
    /**
50
     * Add new tag page
51
     *
52
     * @param FormSettings $form
53
     *
54
     * @return \Illuminate\View\View
55
     */
56 1
    public function getSettings(FormSettings $form)
57
    {
58 1
        return view('administration.settings', [
59 1
            'form'     => $form,
60 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...
61
        ]);
62
    }
63
64
    /**
65
     * To create new tag
66
     *
67
     * @param FormRequest\Settings $request
68
     *
69
     * @return \Illuminate\Http\RedirectResponse
70
     */
71 1
    public function postSettings(FormRequest\Settings $request)
72
    {
73 1
        app('tinyissue.settings')->save($request->except('_token'));
74
75 1
        return redirect('administration/settings')->with('notice', trans('tinyissue.settings_saved'));
76
    }
77
}
78