Parameters::getParameters()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php declare(strict_types=1);
2
3
namespace PeeHaa\AsyncTwitter\Oauth;
4
5
use PeeHaa\AsyncTwitter\Credentials\Application;
6
use PeeHaa\AsyncTwitter\Credentials\AccessToken;
7
use PeeHaa\AsyncTwitter\Request\Parameter;
8
use PeeHaa\AsyncTwitter\Request\Url;
9
10
class Parameters
11
{
12
    private $consumer;
13
14
    private $accessToken;
15
16
    private $nonce;
17
18
    private $timestamp;
19
20
    private $signatureMethod;
21
22
    private $version;
23
24
    private $parameters;
25
26
    private $url;
27
28 16
    public function __construct(Application $consumer, AccessToken $accessToken, Url $url, Parameter ...$parameters)
29
    {
30 16
        $this->consumer        = $consumer->getKey();
31 16
        $this->accessToken     = $accessToken->getToken();
32 16
        $this->nonce           = bin2hex(random_bytes(16));
33 16
        $this->timestamp       = (new \DateTimeImmutable())->format('U');
34 16
        $this->signatureMethod = 'HMAC-SHA1';
35 16
        $this->version         = '1.0';
36 16
        $this->parameters      = $parameters;
37 16
        $this->url             = $url;
38
    }
39
40 6
    public function getConsumerKey(): string
41
    {
42 6
        return $this->consumer;
43
    }
44
45 8
    public function getNonce(): string
46
    {
47 8
        return $this->nonce;
48
    }
49
50 6
    public function getSignatureMethod(): string
51
    {
52 6
        return $this->signatureMethod;
53
    }
54
55 6
    public function getTimestamp(): string
56
    {
57 6
        return $this->timestamp;
58
    }
59
60 6
    public function getToken(): string
61
    {
62 6
        return $this->accessToken;
63
    }
64
65 6
    public function getVersion(): string
66
    {
67 6
        return $this->version;
68
    }
69
70 9
    public function getAll(): array
71
    {
72 9
        return array_merge($this->getBaseParameters(), $this->getParameters());
73
    }
74
75 9
    private function getBaseParameters(): array
76
    {
77
        return [
78 9
            'oauth_consumer_key'     => $this->consumer,
79 9
            'oauth_token'            => $this->accessToken,
80 9
            'oauth_nonce'            => $this->nonce,
81 9
            'oauth_timestamp'        => $this->timestamp,
82 9
            'oauth_signature_method' => $this->signatureMethod,
83 9
            'oauth_version'          => $this->version,
84
        ];
85
    }
86
87 9 View Code Duplication
    private function getParameters(): array
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...
88
    {
89 9
        $parameters = [];
90
91 9
        foreach ($this->parameters as $parameter) {
92 9
            $parameters[$parameter->getKey()] = $parameter->getValue();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class PeeHaa\AsyncTwitter\Request\Parameter as the method getValue() does only exist in the following sub-classes of PeeHaa\AsyncTwitter\Request\Parameter: PeeHaa\AsyncTwitter\Request\FieldParameter. 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...
93
        }
94
95 9
        return $parameters;
96
    }
97
}
98