Completed
Push — master ( 6f6236...71f8b3 )
by Ashleigh
06:55
created

Salesforce   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 111
Duplicated Lines 39.64 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 1
dl 44
loc 111
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B __call() 0 20 5
A callCreateOnObject() 11 11 2
A callUpdateOnObject() 11 11 2
A callDeleteOnObject() 11 11 2
A callGetOnObject() 11 11 2
A runReport() 0 9 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 Surge\LaravelSalesforce;
4
5
use Event;
6
use Surge\LaravelSalesforce\Objects\BaseObject;
7
8
class Salesforce
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $objName;
14
15
    /**
16
     * @var SalesforceAuth
17
     */
18
    private $auth;
19
20
    /**
21
     * Salesforce constructor.
22
     *
23
     * @param                $client
24
     * @param SalesforceAuth $auth
25
     */
26
    public function __construct($client, SalesforceAuth $auth)
27
    {
28
        $this->client = $client;
0 ignored issues
show
Bug introduced by
The property client 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...
29
        $this->auth = $auth;
30
    }
31
32
33
    public function __call($method, $args)
34
    {
35
        if (0 === strpos($method, 'create')) {
36
            return $this->callCreateOnObject($method, $args);
37
        }
38
39
        if (0 === strpos($method, 'update')) {
40
            return $this->callUpdateOnObject($method, $args);
41
        }
42
43
        if (0 === strpos($method, 'delete')) {
44
            return $this->callDeleteOnObject($method, $args);
45
        }
46
47
        if (0 === strpos($method, 'get')) {
48
            return $this->callGetOnObject($method, $args);
49
        }
50
51
        return '';
52
    }
53
54 View Code Duplication
    private function callCreateOnObject($method, $args)
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...
55
    {
56
        $type = substr($method, 6);
57
        $class = '\\Surge\\LaravelSalesforce\\Objects\\'.$type;
58
59
        if (class_exists($class)) {
60
            return (new $class())->create($args[0]);
61
        }
62
63
        return (new BaseObject())->create($type, $args[0]);
0 ignored issues
show
Bug introduced by
The call to BaseObject::__construct() misses a required argument $type.

This check looks for function calls that miss required arguments.

Loading history...
Unused Code introduced by
The call to BaseObject::create() has too many arguments starting with $args[0].

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
64
    }
65
66 View Code Duplication
    private function callUpdateOnObject($method, $args)
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...
67
    {
68
        $type = substr($method, 6);
69
        $class = '\\Surge\\LaravelSalesforce\\Objects\\'.$type;
70
71
        if (class_exists($class)) {
72
            return (new $class())->update($args[0]);
73
        }
74
75
        return (new BaseObject())->update($type, $args[0]);
0 ignored issues
show
Bug introduced by
The call to BaseObject::__construct() misses a required argument $type.

This check looks for function calls that miss required arguments.

Loading history...
76
    }
77
78 View Code Duplication
    private function callDeleteOnObject($method, $args)
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...
79
    {
80
        $type = substr($method, 6);
81
        $class = '\\Surge\\LaravelSalesforce\\Objects\\'.$type;
82
83
        if (class_exists($class)) {
84
            return (new $class())->delete($args[0]);
85
        }
86
87
        return (new BaseObject())->delete($type, $args[0]);
0 ignored issues
show
Bug introduced by
The call to BaseObject::__construct() misses a required argument $type.

This check looks for function calls that miss required arguments.

Loading history...
88
    }
89
90 View Code Duplication
    private function callGetOnObject($method, $args)
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...
91
    {
92
        $type = substr($method, 3);
93
        $class = '\\Surge\\LaravelSalesforce\\Objects\\'.$type;
94
95
        if (class_exists($class)) {
96
            return (new $class())->record($args[0]);
97
        }
98
99
        return (new BaseObject())->record($type, $args[0]);
0 ignored issues
show
Bug introduced by
The call to BaseObject::__construct() misses a required argument $type.

This check looks for function calls that miss required arguments.

Loading history...
Bug introduced by
The method record() does not seem to exist on object<Surge\LaravelSale...rce\Objects\BaseObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100
    }
101
102
    /**
103
     * Run report.
104
     *
105
     * @param $params
106
     *
107
     * @return mixed
108
     */
109
    public function runReport($params)
110
    {
111
        return $this->sendRequest(
0 ignored issues
show
Documentation Bug introduced by
The method sendRequest does not exist on object<Surge\LaravelSalesforce\Salesforce>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
112
            'GET',
113
            '/analytics/reports/'.$params['id'],
114
            ['query' => ['includeDetails' => $params['includeDetails']]],
115
            false
116
        );
117
    }
118
}
119