GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

UpdateSecondHandProduct   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 0
loc 69
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A MyPermissionCheck() 0 6 1
A init() 0 7 2
A unpublish() 0 13 3
B archive() 0 20 5
1
<?php
2
3
4
class UpdateSecondHandProduct extends Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
5
{
6
    private static $allowed_actions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
7
        'unpublish' => '->MyPermissionCheck',
8
        'archive' => '->MyPermissionCheck'
9
    );
10
11
12
    /**
13
     * make the page less easy to access
14
     * (but still accessible)
15
     * - code => ip address
16
     * @var string
17
     */
18
    private static $secret_codes = array();
0 ignored issues
show
Unused Code introduced by
The property $secret_codes is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
19
20
    public function init()
21
    {
22
        parent::init();
23
        if (! $this->MyPermissionCheck()) {
24
            die('you do not have access');
0 ignored issues
show
Coding Style Compatibility introduced by
The method init() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
25
        }
26
    }
27
28
    public function unpublish($request)
29
    {
30
        $unpublished = false;
31
        $otherID = $request->param("OtherID");
32
        if (isset($otherID)) {
33
            $internalItemID = Convert::raw2sql($otherID);
34
            $secondHandProduct = DataObject::get_one('SecondHandProduct', ['InternalItemID' => $internalItemID]);
35
            if ($secondHandProduct) {
36
                $unpublished = $secondHandProduct->deleteFromStage('Live');
37
            }
38
        }
39
        return json_encode($unpublished);
40
    }
41
42
    public function archive($request)
43
    {
44
        $archived = false;
45
        $otherID = $request->param("OtherID");
46
        if (isset($otherID)) {
47
            $archived = null;
48
            $internalItemID = Convert::raw2sql($otherID);
49
            $secondHandProduct = DataObject::get_one('SecondHandProduct', ['InternalItemID' => $internalItemID]);
50
            if (!$secondHandProduct) {
51
                $secondHandProduct = Versioned::get_one_by_stage('SecondHandProduct', 'Stage', ['InternalItemID' => $internalItemID]);
0 ignored issues
show
Documentation introduced by
array('InternalItemID' => $internalItemID) is of type array<string,array|strin...temID":"array|string"}>, but the function expects a string.

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...
52
            }
53
            if (is_a($secondHandProduct, Object::getCustomClass('SiteTree'))) {
54
                $archived = $secondHandProduct->deleteFromStage('Live');
0 ignored issues
show
Unused Code introduced by
$archived is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
55
                $archived = $secondHandProduct->deleteFromStage('Stage');
56
            } elseif (! is_null($secondHandProduct)) {
57
                $archived = $secondHandProduct->delete();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $archived is correct as $secondHandProduct->delete() (which targets DataObject::delete()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
58
            }
59
        }
60
        return json_encode($archived);
61
    }
62
63
    /**
64
     * @return Boolean
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
65
     */
66
    public function MyPermissionCheck()
67
    {
68
        $codesWithIPs = $this->Config()->get('secret_codes');
69
        $code = $this->request->param('ID');
70
        return ControllerPermissionChecker::permissionCheck($codesWithIPs, $code);
71
    }
72
}
73