Completed
Push — master ( 274b6a...1a9054 )
by Sergi Tur
02:37
created

PublishDeleteAssignment   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 89
Duplicated Lines 7.87 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 7
loc 89
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 7 7 2
A delete_assignment() 0 17 2
A abortCommandExecution() 0 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 Acacha\ForgePublish\Commands;
4
5
use Acacha\ForgePublish\Commands\Traits\AborstIfEnvVariableIsnotInstalled;
6
use Acacha\ForgePublish\Commands\Traits\InteractsWithAssignments;
7
use Acacha\ForgePublish\Commands\Traits\InteractsWithEnvironment;
8
use Acacha\ForgePublish\Commands\Traits\InteractsWithLocalGithub;
9
use Acacha\ForgePublish\Commands\Traits\ItFetchesAssignments;
10
use GuzzleHttp\Client;
11
use Illuminate\Console\Command;
12
13
/**
14
 * Class PublishDeleteAssignment.
15
 *
16
 * @package Acacha\ForgePublish\Commands
17
 */
18
class PublishDeleteAssignment extends Command
19
{
20
    use InteractsWithEnvironment,
21
        AborstIfEnvVariableIsnotInstalled,
22
        InteractsWithAssignments,
23
        ItFetchesAssignments;
24
25
    /**
26
     * The name and signature of the console command.
27
     *
28
     * @var string
29
     */
30
    protected $signature = 'publish:delete_assignment {assignment? : The assignment to remove}' ;
31
32
    /**
33
     * The console command description.
34
     *
35
     * @var string
36
     */
37
    protected $description = 'Deletes an assignment';
38
39
    /**
40
     * Assignment.
41
     *
42
     * @var integer
43
     */
44
    protected $assignment;
45
46
47
    /**
48
     * Server names.
49
     *
50
     * @var Client
51
     */
52
    protected $http;
53
54
    /**
55
     * SaveEnvVariable constructor.
56
     *
57
     */
58
    public function __construct(Client $http)
59
    {
60
        parent::__construct();
61
        $this->http = $http;
62
    }
63
64
    /**
65
     * Execute the console command.
66
     *
67
     */
68 View Code Duplication
    public function handle()
0 ignored issues
show
Duplication introduced by
This method 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...
69
    {
70
        $this->abortCommandExecution();
71
        $this->assignment = $this->argument('assignment') ? $this->argument('assignment') : $this->askAssignment();
72
73
        $this->delete_assignment();
74
    }
75
76
    /**
77
     * Delete assignment.
78
     *
79
     * @return array|mixed
80
     */
81
    protected function delete_assignment()
82
    {
83
        $uri = str_replace('{assignment}', $this->assignment, config('forge-publish.update_assignment_uri'));
84
        $url = config('forge-publish.url') . $uri;
85
        try {
86
            $response = $this->http->delete($url, [
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
87
                'headers' => [
88
                    'X-Requested-With' => 'XMLHttpRequest',
89
                    'Authorization' => 'Bearer ' . fp_env('ACACHA_FORGE_ACCESS_TOKEN')
90
                ]
91
            ]);
92
        } catch (\Exception $e) {
93
            $this->error('And error occurs connecting to the api url: ' . $url);
94
            $this->error('Status code: ' . $e->getResponse()->getStatusCode() . ' | Reason : ' . $e->getResponse()->getReasonPhrase());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Exception as the method getResponse() does only exist in the following sub-classes of Exception: GuzzleHttp\Exception\BadResponseException, GuzzleHttp\Exception\ClientException, GuzzleHttp\Exception\ConnectException, GuzzleHttp\Exception\RequestException, GuzzleHttp\Exception\ServerException, GuzzleHttp\Exception\TooManyRedirectsException. 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...
95
            return [];
96
        }
97
    }
98
99
    /**
100
     * Abort command execution.
101
     */
102
    protected function abortCommandExecution()
103
    {
104
        $this->abortsIfEnvVarIsNotInstalled('ACACHA_FORGE_ACCESS_TOKEN');
105
    }
106
}
107