Completed
Push — issue/270 ( cf7824 )
by Alex
01:38
created

CheckCommand::execute()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 60
ccs 0
cts 44
cp 0
rs 8.2505
c 0
b 0
f 0
cc 6
nc 32
nop 2
crap 42

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the feed-io package.
4
 *
5
 * (c) Alexandre Debril <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FeedIo\Command;
12
13
use FeedIo\Command\Check\CheckerAbstract;
14
use FeedIo\Command\Check\CountChecker;
15
use FeedIo\Command\Check\HistoryChecker;
16
use FeedIo\Factory;
17
use FeedIo\Feed\ItemInterface;
18
use FeedIo\FeedInterface;
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
21
use Symfony\Component\Console\Input\InputArgument;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Input\InputOption;
24
use Symfony\Component\Console\Output\OutputInterface;
25
26
class CheckCommand extends Command
27
{
28
    protected function configure()
29
    {
30
        $this->setName('check')
31
            ->setDescription('checks if a feed gets correctly updated')
32
            ->addArgument(
33
                'url',
34
                InputArgument::REQUIRED,
35
                'Please provide the feed\' URL'
36
            )
37
        ;
38
    }
39
40
    protected function execute(InputInterface $input, OutputInterface $output)
41
    {
42
        $this->configureOutput($output);
43
        $url = $input->getArgument('url');
44
45
        $feedIo = Factory::create()->getFeedIo();
46
        $feed = $feedIo->read($url)->getFeed();
0 ignored issues
show
Bug introduced by
It seems like $url defined by $input->getArgument('url') on line 43 can also be of type array<integer,string> or null; however, FeedIo\FeedIo::read() 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...
47
48
        $output->writeln("<info>first access to {$feed->getTitle()}</info>");
49
50
        $failures = 0;
51
52
        $count = count($feed);
53
        if( $count == 0) {
54
            $failures++;
55
            $output->writeln("<error>empty feed</error>");
56
        }
57
58
        $output->writeln("<info>found {$count} items</info>");
59
60
        $lastModifiedDates = [];
61
        /** @var \FeedIo\Feed\ItemInterface $item */
62
        foreach ($feed as $i => $item) {
63
            $lastModifiedDates[] = $item->getLastModified();
64
        }
65
66
        sort($lastModifiedDates);
67
68
        $first = current($lastModifiedDates);
69
70
        /** @var \DateTime $last */
71
        $last = end($lastModifiedDates);
72
73
        $normalDateFlow = true;
74
        if ($last > $first) {
75
            $output->writeln("<info>first item was published on {$first->format(\DateTime::ATOM)}</info>");
76
            $output->writeln("<info>last item was published on {$last->format(\DateTime::ATOM)}</info>");
77
        } else {
78
            $output->writeln("<warn>All items have the same date</warn>");
79
            $normalDateFlow = false;
80
        }
81
82
        if ($normalDateFlow) {
83
            $pick = intval($count / 2);
84
            $lastModified = $lastModifiedDates[$pick];
85
        } else {
86
            $lastModified = $last->sub(new \DateInterval('-1 days'));
87
        }
88
89
        $secondHit = $feedIo->readSince($url, $lastModified)->getFeed();
0 ignored issues
show
Bug introduced by
It seems like $url defined by $input->getArgument('url') on line 43 can also be of type array<integer,string> or null; however, FeedIo\FeedIo::readSince() 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...
90
91
        $count = count($secondHit);
92
        if( $count == 0) {
93
            $failures++;
94
            $output->writeln("<error>empty feed</error>");
95
        }
96
97
        $output->writeln("<info>found {$count} items</info>");
98
        return $failures;
99
    }
100
101
    private function configureOutput(OutputInterface $output): void
102
    {
103
        $outputStyle = new OutputFormatterStyle('black', 'magenta', ['bold']);
104
        $output->getFormatter()->setStyle('warn', $outputStyle);
105
    }
106
107
}
108