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

CerStrategy::getNoCertificado()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
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\Strategies;
13
14
class CerStrategy
15
{
16
    /**
17
     * File to decode.
18
     *
19
     * @var string
20
     */
21
    protected $file;
22
23
    /**
24
     * Chunk length.
25
     *
26
     * @var integer
27
     */
28
    protected $chunklen = 64;
29
30
    /**
31
     * Create a new cer strategy instance.
32
     *
33
     * @param $file
34
     */
35
    public function __construct($file)
36
    {
37
        $this->file = file_get_contents($file);
38
    }
39
40
    /**
41
     * Convert .cer to .pem
42
     *
43
     * @return string
44
     */
45
    public function convertToPem() : string
46
    {
47
        $prefix = "-----BEGIN CERTIFICATE-----\n";
48
        $suffix = "-----END CERTIFICATE-----\n";
49
50
        $pem = base64_encode($this->file);
51
        $pem = chunk_split($pem, $this->chunklen, "\n") ;
52
        $pem = $prefix.$pem.$suffix;
53
54
        return $pem;
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function getNoCertificado()
61
    {
62
        $data = $this->parseCertificate();
63
        $data = str_split($data['serialNumberHex'], 2);
64
65
        for ($i = 0; $i < sizeof($data); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
66
            $serialNumber .= substr($data[$i], 1);
0 ignored issues
show
Bug introduced by
The variable $serialNumber 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...
67
        }
68
69
        return $serialNumber;
70
    }
71
72
    /**
73
     * @return array
74
     */
75
    protected function parseCertificate()
76
    {
77
        return openssl_x509_parse($this->convertToPem());
78
    }
79
}
80