ParserCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 26.03 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 55.56%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 19
loc 73
ccs 15
cts 27
cp 0.5556
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 19 19 1
A execute() 0 32 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
declare(strict_types = 1);
3
4
namespace BrowscapPHP\Command;
5
6
use BrowscapPHP\Browscap;
7
use BrowscapPHP\Exception;
8
use BrowscapPHP\Helper\LoggerHelper;
9
use Doctrine\Common\Cache\FilesystemCache;
10
use ExceptionalJSON\EncodeErrorException;
11
use Roave\DoctrineSimpleCache\SimpleCacheAdapter;
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
/**
19
 * commands to parse a given useragent
20
 */
21
class ParserCommand extends Command
22
{
23
    /**
24
     * @var string
25
     */
26
    private $defaultCacheFolder;
27
28
    public function __construct(string $defaultCacheFolder)
29
    {
30
        $this->defaultCacheFolder = $defaultCacheFolder;
31
32
        parent::__construct();
33 1
    }
34
35 View Code Duplication
    protected function configure() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36 1
    {
37 1
        $this
38 1
            ->setName('browscap:parse')
39 1
            ->setDescription('Parses a user agent string and dumps the results.')
40 1
            ->addArgument(
41 1
                'user-agent',
42 1
                InputArgument::REQUIRED,
43
                'User agent string to analyze',
44 1
                null
45 1
            )
46 1
            ->addOption(
47 1
                'cache',
48 1
                'c',
49 1
                InputOption::VALUE_OPTIONAL,
50
                'Where the cache files are located',
51 1
                $this->defaultCacheFolder
52
            );
53
    }
54
55
    /**
56
     * @param \Symfony\Component\Console\Input\InputInterface   $input
57
     * @param \Symfony\Component\Console\Output\OutputInterface $output
58
     *
59
     * @return int
60
     */
61
    protected function execute(InputInterface $input, OutputInterface $output) : int
62
    {
63
        $logger = LoggerHelper::createDefaultLogger($output);
64
65
        /** @var string $cacheOption */
66
        $cacheOption = $input->getOption('cache');
67
        $fileCache = new FilesystemCache($cacheOption);
68
        $cache = new SimpleCacheAdapter($fileCache);
69
70
        $browscap = new Browscap($cache, $logger);
71
72
        /** @var string $uaArgument */
73
        $uaArgument = $input->getArgument('user-agent');
74
75
        try {
76
            $result = $browscap->getBrowser($uaArgument);
77
        } catch (Exception $e) {
78
            $logger->debug($e);
79
80
            return 11;
81
        }
82
83
        try {
84
            $output->writeln(\ExceptionalJSON\encode($result, JSON_PRETTY_PRINT));
85
        } catch (EncodeErrorException $e) {
0 ignored issues
show
Bug introduced by
The class ExceptionalJSON\EncodeErrorException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
86
            $logger->error($e);
87
88
            return 11;
89
        }
90
91
        return 0;
92
    }
93
}
94