Completed
Pull Request — master (#52)
by
unknown
03:07
created

CreateLinkCommand::addOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spatie\CalendarLinks\Console;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputOption;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Spatie\CalendarLinks\Link;
10
use DateTime;
11
use Symfony\Component\Console\Style\SymfonyStyle;
12
13
class CreateLinkCommand extends Command
14
{
15
    /**
16
     * @var string
17
     */
18
    private $commandName = 'create';
19
20
    /**
21
     * @var string
22
     */
23
    private $commandDescription = 'Generate a link to create an event on specific service';
24
25
    protected function configure(): void
26
    {
27
        $helpContent = '
28
    Example of a default request:
29
30
        <fg=black;bg=yellow>calendar_links create --to="2018-02-01 18:00"</>
31
32
    Example of a request with start and end date:
33
34
        <fg=black;bg=yellow>calendar_links create --from="2019-02-01 18:00" --to="2019-02-01 22:00"</></>
35
36
    Example of a request for multiple links:
37
38
        <fg=black;bg=yellow>calendar_links create --from="2019-02-01 18:00" --to="2019-02-01 22:00" -s google -s yahoo</>
39
40
    Example of a raw request:
41
42
        <fg=black;bg=yellow>calendar_links create --from="2019-02-01 18:00" --to="2019-02-01 22:00" -r true</>
43
        ';
44
45
        $this
46
            ->setName($this->commandName)
47
            ->setDescription($this->commandDescription)
48
            ->setHelp($helpContent)
49
            ->addOptions();
50
    }
51
52
    protected function execute(InputInterface $input, OutputInterface $output): void
53
    {
54
        $io = new SymfonyStyle($input, $output);
55
        $raw = $input->getOption('raw');
56
        $services = $input->getOption('services');
57
        $from = DateTime::createFromFormat('Y-m-d H:i', $input->getOption('from'));
58
        $to = DateTime::createFromFormat('Y-m-d H:i', $input->getOption('to'));
59
60
        $link = Link::create('Sebastian\'s birthday', $from, $to)
0 ignored issues
show
Security Bug introduced by
It seems like $from defined by \DateTime::createFromFor...put->getOption('from')) on line 57 can also be of type false; however, Spatie\CalendarLinks\Link::create() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
Security Bug introduced by
It seems like $to defined by \DateTime::createFromFor...input->getOption('to')) on line 58 can also be of type false; however, Spatie\CalendarLinks\Link::create() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
61
            ->description('Cookies & cocktails!')
62
            ->address('Samberstraat 69D, 2060 Antwerpen');
63
64
        $contentOutput = function ($service) use ($raw, $link) {
65
            $link = $link->$service();
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $link, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
66
67
            if ($raw) {
68
                return [$link, ''];
69
            }
70
71
            return [
72
                "<fg=black;bg=cyan>$service link</>",
73
                $link,
74
                ''
75
            ];
76
        };
77
78
        if (!empty($services)) {
79
            foreach ($services as $service) {
0 ignored issues
show
Bug introduced by
The expression $services of type string|array<integer,string>|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
80
                $output->writeln($contentOutput($service));
81
            }
82
83
            if (!$raw) {
84
                $io->success('');
85
            }
86
87
            return;
88
        }
89
90
        $service = $io->choice('Select the service to which the link will be generated', ['google', 'yahoo', 'webOutlook', 'ics'], 'google');
91
92
        $output->writeln($contentOutput($service));
93
94
        $io->success('');
95
    }
96
97
    private function addOptions(): self
98
    {
99
        $this
100
            ->addOption('from', 'f', InputOption::VALUE_OPTIONAL, 'The event start date', date('Y-m-d H:i'))
101
            ->addOption('to', 't', InputOption::VALUE_REQUIRED, 'The event end date')
102
            ->addOption(
103
                'services',
104
                's',
105
                InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
106
                'The service to which the link will be generated'
107
            )
108
            ->addOption(
109
                'raw',
110
                'r',
111
                InputOption::VALUE_OPTIONAL,
112
                'Remove any formattation from output content (true|false)',
113
                false
114
            );
115
116
        return $this;
117
    }
118
}
119