ItFetchesSites::getSite()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 9
Ratio 81.82 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 9
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Acacha\ForgePublish\Commands\Traits;
4
5
/**
6
 * Trait ItFetchesSites
7
 *
8
 * @package Acacha\ForgePublish\Commands
9
 */
10
trait ItFetchesSites
11
{
12
    /**
13
     * Fetch sites
14
     */
15
    protected function fetchSites($server_id)
16
    {
17
        $url = config('forge-publish.url') . config('forge-publish.user_sites_uri') . '/' . $server_id;
18
        try {
19
            $response = $this->http->get($url, [
0 ignored issues
show
Bug introduced by
The property http does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20
                'headers' => [
21
                    'X-Requested-With' => 'XMLHttpRequest',
22
                    'Authorization' => 'Bearer ' . fp_env('ACACHA_FORGE_ACCESS_TOKEN')
23
                ]
24
            ]);
25
        } catch (\Exception $e) {
26
            $this->error('And error occurs connecting to the api url: ' . $url);
0 ignored issues
show
Bug introduced by
It seems like error() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
27
            $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...
Bug introduced by
It seems like error() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
28
            return [];
29
        }
30
        return json_decode((string) $response->getBody());
31
    }
32
33
    /**
34
     * Get forge site from sites by name.
35
     *
36
     * @param $sites
37
     * @param $site_id
38
     * @return mixed
39
     */
40 View Code Duplication
    protected function getSiteByName($sites, $site_name)
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...
41
    {
42
        $site_found = collect($sites)->filter(function ($site) use ($site_name) {
43
            return $site->name === $site_name;
44
        })->first();
45
46
        if ($site_found) {
47
            return $site_found;
48
        }
49
        return null;
50
    }
51
52
    /**
53
     * Get forge site from sites.
54
     *
55
     * @param $sites
56
     * @param $site_id
57
     * @return mixed
58
     */
59 View Code Duplication
    protected function getSite($sites, $site_id)
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...
60
    {
61
        $site_found = collect($sites)->filter(function ($site) use ($site_id) {
62
            return $site->id === $site_id;
63
        })->first();
64
65
        if ($site_found) {
66
            return $site_found;
67
        }
68
        return null;
69
    }
70
71
    /**
72
     * Get forge site name from site id.
73
     *
74
     * @param $sites
75
     * @param $site_id
76
     * @return mixed
77
     */
78
    protected function getSiteName($sites, $site_id)
79
    {
80
        if ($site = $this->getSite($sites, $site_id)) {
81
            return $site->name;
82
        }
83
        return null;
84
    }
85
86
    /**
87
     * Get forge site id from site name.
88
     *
89
     * @param $sites
90
     * @param $site_name
91
     * @return mixed
92
     */
93
    protected function getSiteId($sites, $site_name)
94
    {
95
        if ($site = $this->getSiteByName($sites, $site_name)) {
96
            return $site->id;
97
        }
98
        return null;
99
    }
100
}
101