Passed
Push — master ( a5a1f1...923478 )
by Steven
01:18 queued 11s
created

SocialSentiment   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 12.16 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 9
loc 74
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setType() 0 6 1
A setDate() 0 8 1
A getFullEndpoint() 9 9 1
A validateParams() 0 6 2
A validateDate() 0 10 2

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 Digitonic\IexCloudSdk\AlternativeData;
4
5
use Digitonic\IexCloudSdk\Contracts\IEXCloud;
6
use Digitonic\IexCloudSdk\Exceptions\WrongData;
7
use Digitonic\IexCloudSdk\Requests\BaseGet;
8
9
class SocialSentiment extends BaseGet
10
{
11
    const ENDPOINT = 'stock/{symbol}/sentiment/{type}/{date}';
12
13
    /**
14
     * @var string
15
     */
16
    private $type = 'daily';
17
18
    /**
19
     * @var string
20
     */
21
    private $date;
22
23
    /**
24
     * Create constructor.
25
     *
26
     * @param  IEXCloud  $api
27
     */
28 5
    public function __construct(IEXCloud $api)
29
    {
30 5
        parent::__construct($api);
31 5
    }
32
33 1
    public function setType(string $type): self
34
    {
35 1
        $this->type = $type;
36
37 1
        return $this;
38
    }
39
40 3
    public function setDate(string $date): self
41
    {
42 3
        $this->validateDate($date);
43
44 2
        $this->date = $date;
45
46 2
        return $this;
47
    }
48
49
    /**
50
     * @return string
51
     */
52 2 View Code Duplication
    protected function getFullEndpoint(): string
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...
53
    {
54 2
        $endpoint = str_replace('{symbol}', $this->symbol, self::ENDPOINT);
55 2
        $endpoint = str_replace('{type}', $this->type, $endpoint);
56 2
        $endpoint = str_replace('{date}', $this->date, $endpoint);
57
58 2
        return $endpoint;
59
60
    }
61
62
    /**
63
     * @return bool|void
64
     */
65 3
    protected function validateParams(): void
66
    {
67 3
        if (empty($this->symbol)) {
68 1
            throw WrongData::invalidValuesProvided('Please provide a symbol to query!');
69
        }
70 2
    }
71
72 3
    private function validateDate(string $date)
73
    {
74 3
        $re = '/(20\d{2})(\d{2})(\d{2})/mi';
75
76 3
        preg_match_all($re, $date, $matches, PREG_SET_ORDER, 0);
77
78 3
        if (empty($matches)) {
79 1
            throw WrongData::invalidValuesProvided('Format date to use YYYYMMDD to fetch sentiment data.');
80
        }
81 2
    }
82
}
83