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 ( ae149c...aa4006 )
by Orlando
01:13
created

Certificate::getFileExtensionName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 Exception;
15
use Kinedu\CfdiCertificate\IO;
16
use Kinedu\CfdiCertificate\Strategies\CerStrategy;
17
use Kinedu\CfdiCertificate\Strategies\KeyStrategy;
18
19
class Certificate
20
{
21
    /**
22
     * File to decode.
23
     *
24
     * @var string
25
     */
26
    protected $file;
27
28
    /**
29
     * Password to the decode the file.
30
     *
31
     * @var string
32
     */
33
    protected $password;
34
35
    /** @var string */
36
    protected $strategy;
37
38
    /**
39
     * Create a new certificate instance.
40
     *
41
     * @param string $file
42
     * @param string $password
43
     * @param string $strategy
44
     */
45
    public function __construct(string $file, string $password = null, string $strategy = null)
46
    {
47
        $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...
48
        $this->password = $password;
49
        $this->strategy = $strategy;
50
    }
51
52
    /**
53
     * @return CerStrategy|KeyStrategy|null
54
     */
55
    protected function getStrategy()
56
    {
57
        switch ($this->getFileExtensionName()) {
58
            case 'cer':
59
                $strategy = new CerStrategy(
60
                    $this->file->getOriginalRoute()
0 ignored issues
show
Bug introduced by
The method getOriginalRoute 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...
61
                );
62
            break;
63
64
            case 'key':
65
                $strategy = new KeyStrategy(
66
                    $this->file->getOriginalRoute(),
0 ignored issues
show
Bug introduced by
The method getOriginalRoute 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...
67
                    $this->password
68
                );
69
            break;
70
        }
71
72
        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...
73
    }
74
75
    public function decode(): string
76
    {
77
        $strategy = $this->getStrategy();
78
        $pem = $strategy->convertToPem();
79
80
        return $pem;
81
    }
82
83
    /**
84
     * @param string $directory
85
     * @param string $filename
86
     *
87
     * @return integer|bool
88
     */
89
    public function save(string $directory, string $filename = null)
90
    {
91
        $filename  = $filename ?? $this->file->getFileName();
0 ignored issues
show
Bug introduced by
The method getFileName 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...
92
        $extension = $this->getFileExtensionName();
93
94
        $directory = rtrim($directory, '/').'/';
95
        $directory = "{$directory}{$filename}.{$extension}.pem";
96
97
        return file_put_contents($directory, $this->decode());
98
    }
99
100
    /**
101
     * @param string $name
102
     * @param array $arguments
103
     */
104
    public function __call(string $name, array $arguments)
105
    {
106
        $strategy = $this->getStrategy();
107
108
        if (method_exists($strategy, $name)) {
109
            return $strategy->{$name}($arguments);
110
        } else {
111
            throw new Exception("This method doesn't exist");
112
        }
113
    }
114
115
    protected function getFileExtensionName()
116
    {
117
        return $this->strategy ?? $this->file->getFileExtensionName();
0 ignored issues
show
Bug introduced by
The method getFileExtensionName 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...
118
    }
119
}
120