LinkedinPage   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 88
Duplicated Lines 37.5 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
c 3
b 0
f 0
lcom 1
cbo 7
dl 33
loc 88
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B post() 10 50 5
A getProfile() 23 23 1
A getStats() 0 7 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 Borfast\Socializr\Connectors;
4
5
use Borfast\Socializr\Exceptions\ExpiredTokenException;
6
use Borfast\Socializr\Exceptions\GenericPostingException;
7
use Borfast\Socializr\Post;
8
use Borfast\Socializr\Profile;
9
use Borfast\Socializr\Page;
10
use Borfast\Socializr\Response;
11
use Borfast\Socializr\Connectors\AbstractConnector;
12
use OAuth\Common\Storage\TokenStorageInterface;
13
14
class LinkedinPage extends AbstractConnector
15
{
16
    public static $provider = 'linkedin';
17
18
    public function post(Post $post)
19
    {
20
        $page_id = $post->options['page_id'];
21
        $path = '/companies/'.$page_id.'/shares?format=json';
22
        $method = 'POST';
23
        $params = [
24
            'visibility' => [
25
                'code' => 'anyone'
26
            ],
27
            'comment' => '',
28
            'content' => [
29
                'title' => $post->title,
30
                'submitted-url' => $post->url,
31
                'description' => $post->body,
32
            ],
33
        ];
34
35
        if (!empty($post->media)) {
36
            $params['content']['submitted-image-url'] = $post->media[0];
37
        }
38
39
        $params = json_encode($params);
40
41
        // Linkedin API requires the Content-Type header set to application/json
42
        $header = ['Content-Type' => 'application/json'];
43
        $result = $this->request($path, $method, $params, $header);
0 ignored issues
show
Documentation introduced by
$params is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
45
        // The response comes in JSON
46
        $json_result = json_decode($result, true);
47
48 View Code Duplication
        if (isset($json_result['status']) && $json_result['status'] != 200) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
49
            $msg = "Error posting to Linkedin page. Error code from Linkedin: %s. Error message from Linkedin: %s";
50
            $msg = sprintf($msg, $json_result['errorCode'], $json_result['message']);
51
52
            if ($json_result['status'] == '401') {
53
                throw new ExpiredTokenException($msg);
54
            } else {
55
                throw new GenericPostingException($msg, $json_result['status']);
56
            }
57
        }
58
59
        $response = new Response;
60
        $response->setRawResponse(json_encode($result));
61
        $response->setProvider(static::$provider);
62
        $result_json = json_decode($result);
63
        $response->setPostId($result_json->updateKey);
64
        $response->setPostUrl($result_json->updateUrl);
65
66
        return $response;
67
    }
68
69
70 View Code Duplication
    public function getProfile()
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...
71
    {
72
        $path = '/people/~:(id,first-name,last-name,maiden-name,public-profile-url,formatted-name,num-connections,email-address,num-recommenders)?format=json';
73
        $response = $this->service->request($path);
74
        $profile_json = json_decode($response, true);
75
76
        $mapping = [
77
            'id' => 'id',
78
            'email' => 'emailAddress',
79
            'name' => 'formattedName',
80
            'first_name' => 'firstName',
81
            'middle_name' => 'maidenName',
82
            'last_name' => 'lastName',
83
            // 'username' => 'username',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
84
            'link' => 'publicProfileUrl'
85
        ];
86
87
        $profile = Profile::create($mapping, $profile_json);
88
        $profile->provider = static::$provider;
89
        $profile->raw_response = $response;
90
91
        return $profile;
92
    }
93
94
    public function getStats()
95
    {
96
        $path = 'companies/'.$this->id.':(id,num-followers)?format=json';
97
        $response = json_decode($this->request($path));
98
99
        return $response->numFollowers;
100
    }
101
}
102