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.

ResolveCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spatie\CertificateChain\Console;
4
5
use Exception;
6
use GuzzleHttp\Client;
7
use Spatie\CertificateChain\Certificate;
8
use Spatie\CertificateChain\CertificateChain;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Spatie\CertificateChain\Exceptions\CouldNotRunCommand;
14
use Symfony\Component\Console\Question\ConfirmationQuestion;
15
16
class ResolveCommand extends Command
17
{
18
    protected $httpClient;
19
20
    /**
21
     * Configure the command options.
22
     */
23
    protected function configure()
24
    {
25
        $this->setName('resolve')
26
            ->setDescription('Download all intermediate certificates in the trust chain.')
27
            ->addArgument('certificate', InputArgument::REQUIRED)
28
            ->addArgument('outputFile', InputArgument::OPTIONAL);
29
    }
30
31
    public function __construct()
32
    {
33
        parent::__construct();
34
35
        $this->httpClient = new Client();
36
    }
37
38
    /**
39
     * Execute the command.
40
     *
41
     * @param InputInterface  $input
42
     * @param OutputInterface $output
43
     *
44
     * @return int|null|void
45
     *
46
     * @throws Exception
47
     */
48
    protected function execute(InputInterface $input, OutputInterface $output)
49
    {
50
        $output->writeln('<info>Start resolving trust chain...</info>');
51
52
        $certificateFile = $input->getArgument('certificate');
53
54
        if (! file_exists($certificateFile)) {
55
            throw CouldNotRunCommand::inputFileDoesNotExist($certificateFile);
0 ignored issues
show
Bug introduced by
It seems like $certificateFile defined by $input->getArgument('certificate') on line 52 can also be of type array<integer,string> or null; however, Spatie\CertificateChain\...inputFileDoesNotExist() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
56
        }
57
58
        $outputFile = $input->getArgument('outputFile') ?: 'certificate-including-trust-chain.crt';
59
60
        if (file_exists($outputFile)) {
61
            if (! $this->confirmOverwrite($input, $output, $outputFile)) {
0 ignored issues
show
Bug introduced by
It seems like $outputFile defined by $input->getArgument('out...luding-trust-chain.crt' on line 58 can also be of type array<integer,string>; however, Spatie\CertificateChain\...and::confirmOverwrite() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
62
                $output->writeln('<info>Cancelling...</info>');
63
64
                return true;
65
            }
66
        }
67
68
        $certificate = Certificate::loadFromFile($certificateFile);
0 ignored issues
show
Bug introduced by
It seems like $certificateFile defined by $input->getArgument('certificate') on line 52 can also be of type array<integer,string> or null; however, Spatie\CertificateChain\...ificate::loadFromFile() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
69
70
        $certificateChain = CertificateChain::fetchForCertificate($certificate);
71
72
        file_put_contents($outputFile, $certificateChain);
73
74
        $output->writeln('<info>Saved trust chain in '.$outputFile.'</info>');
75
        $output->writeln('<info>All done!</info>');
76
    }
77
78
    protected function guardAgainstInvalidInput(string $certificateFile)
79
    {
80
        if (! file_exists($certificateFile)) {
81
            throw CouldNotRunCommand::inputFileDoesNotExist($certificateFile);
82
        }
83
    }
84
85
    protected function confirmOverwrite(InputInterface $input, OutputInterface $output, string $outputFile): bool
86
    {
87
        $output->writeln('');
88
89
        $helper = $this->getHelper('question');
90
        $question = new ConfirmationQuestion('<comment>Outputfile '.$outputFile.' already exists. Do you want to overwrite it? (y/n) </comment>', false);
91
92
        if (! $helper->ask($input, $output, $question)) {
93
            return false;
94
        }
95
96
        return true;
97
    }
98
}
99