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.
Completed
Push — master ( 7a7b9b...3fa611 )
by Orlando
01:13
created

Certificate::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of the cfdi-certificate project.
5
 *
6
 * (c) Kinedu
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Kinedu\CfdiCertificate;
13
14
use Kinedu\CfdiCertificate\Strategies\CerStrategy;
15
use Kinedu\CfdiCertificate\Strategies\KeyStrategy;
16
use Kinedu\CfdiCertificate\IO;
17
use Exception;
18
19
class Certificate
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $file;
25
26
    /**
27
     * @var string
28
     */
29
    protected $password;
30
31
    /**
32
     * Create a new certificate instance.
33
     *
34
     * @param string $file
35
     * @param string $password
36
     */
37
    public function __construct(string $file, string $password = null)
38
    {
39
        $this->file = new IO($file);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Kinedu\CfdiCertificate\IO($file) of type object<Kinedu\CfdiCertificate\IO> is incompatible with the declared type string of property $file.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40
        $this->password = $password;
41
    }
42
43
    /**
44
     * @return CerStrategy|KeyStrategy|null
45
     */
46
    protected function getStrategy()
47
    {
48
        switch ($this->file->getFileExstensionName()) {
0 ignored issues
show
Bug introduced by
The method getFileExstensionName cannot be called on $this->file (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
49
            case 'cer':
50
                $strategy = new CerStrategy(
51
                    $this->file->getOrginalRoute()
0 ignored issues
show
Bug introduced by
The method getOrginalRoute cannot be called on $this->file (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
52
                );
53
            break;
54
55
            case 'key':
56
                $strategy = new KeyStrategy(
57
                    $this->file->getOrginalRoute(),
0 ignored issues
show
Bug introduced by
The method getOrginalRoute cannot be called on $this->file (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
58
                    $this->password
59
                );
60
            break;
61
        }
62
63
        return $strategy;
0 ignored issues
show
Bug introduced by
The variable $strategy does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function decode() : string
70
    {
71
        $strategy = $this->getStrategy();
72
        $pem = $strategy->convertToPem();
73
74
        return $pem;
75
    }
76
77
    /**
78
     * @param string $filename
79
     *
80
     * @return
81
     */
82
    public function save(string $filename)
83
    {
84
        return file_put_contents($filename, $this->decode());
85
    }
86
87
    /**
88
     * @param string $name
89
     * @param array $arguments
90
     */
91
    public function __call(string $name, array $arguments)
92
    {
93
        $strategy = $this->getStrategy();
94
95
        if(method_exists($strategy, $name)) {
96
            return $strategy->{$name}($arguments);
97
        } else {
98
            throw new Exception("This method doesn't exist");
99
        }
100
    }
101
}
102