GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 414922...a9bc98 )
by butschster
12:35
created

CreatePublicDirectory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 39
loc 39
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A showInfo() 4 4 1
A install() 8 8 1
A installed() 4 4 1
A getDirectory() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SleepingOwl\Admin\Console\Installation;
4
5 View Code Duplication
class CreatePublicDirectory extends Installator
0 ignored issues
show
Duplication introduced by
This class 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...
6
{
7
    public function showInfo()
8
    {
9
        $this->command->line('Creating public directory: <info>✔</info>');
10
    }
11
12
    /**
13
     * Install the components.
14
     *
15
     * @return void
16
     */
17
    public function install()
18
    {
19
        $directory = $this->getDirectory();
20
21
        $this->command->files()->makeDirectory($directory, 0755, true, true);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Illuminate\Console\Command as the method files() does only exist in the following sub-classes of Illuminate\Console\Command: SleepingOwl\Admin\Console\Commands\InstallCommand, SleepingOwl\Admin\Console\Commands\UpdateCommand, SleepingOwl\Admin\Console\Installation\Command. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
22
        $directoryPath = str_replace(base_path(), '', $directory);
23
        $this->command->line("<info>Public upload directory is [{$directoryPath}]</info>");
24
    }
25
26
    /**
27
     * При возврате методом true данный компонент будет пропущен.
28
     *
29
     * @return bool
30
     */
31
    public function installed()
32
    {
33
        return is_dir($this->getDirectory());
34
    }
35
36
    /**
37
     * @return string
38
     */
39
    protected function getDirectory()
40
    {
41
        return public_path('images/uploads');
42
    }
43
}
44