Completed
Pull Request — master (#155)
by Thomas
12:04
created

ParserCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 107
Duplicated Lines 32.71 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 93.33%

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 7
c 5
b 1
f 1
lcom 2
cbo 7
dl 35
loc 107
ccs 42
cts 45
cp 0.9333
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setCache() 0 6 1
B configure() 26 26 1
A execute() 0 21 2
A __construct() 0 6 1
A getCache() 9 9 2

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
/**
3
 * Copyright (c) 1998-2015 Browser Capabilities Project
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * copy of this software and associated documentation files (the "Software"),
7
 * to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included
13
 * in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 *
23
 * @category   Browscap-PHP
24
 * @package    Command
25
 * @copyright  1998-2015 Browser Capabilities Project
26
 * @license    http://www.opensource.org/licenses/MIT MIT License
27
 * @link       https://github.com/browscap/browscap-php/
28
 * @since      added with version 3.0
29
 */
30
31
namespace BrowscapPHP\Command;
32
33
use BrowscapPHP\Browscap;
34
use BrowscapPHP\Cache\BrowscapCache;
35
use BrowscapPHP\Cache\BrowscapCacheInterface;
36
use BrowscapPHP\Helper\LoggerHelper;
37
use Symfony\Component\Console\Command\Command;
38
use Symfony\Component\Console\Input\InputArgument;
39
use Symfony\Component\Console\Input\InputInterface;
40
use Symfony\Component\Console\Input\InputOption;
41
use Symfony\Component\Console\Output\OutputInterface;
42
use WurflCache\Adapter\File;
43
44
/**
45
 * commands to parse a given useragent
46
 *
47
 * @category   Browscap-PHP
48
 * @package    Command
49
 * @author     Dave Olsen, http://dmolsen.com
50
 * @author     Thomas Müller <[email protected]>
51
 * @copyright  Copyright (c) 1998-2015 Browser Capabilities Project
52
 * @version    3.0
53
 * @license    http://www.opensource.org/licenses/MIT MIT License
54
 * @link       https://github.com/browscap/browscap-php/
55
 */
56
class ParserCommand extends Command
57
{
58
    /**
59
     * @var BrowscapCacheInterface
60
     */
61
    private $cache = null;
62
63
    /**
64
     * @var string
65
     */
66
    private $defaultCacheFolder;
67
68
    /**
69
     * @param string $defaultCacheFolder
70
     */
71 1
    public function __construct($defaultCacheFolder)
72
    {
73 1
        $this->defaultCacheFolder = $defaultCacheFolder;
74
75 1
        parent::__construct();
76 1
    }
77
78
    /**
79
     * @param \BrowscapPHP\Cache\BrowscapCacheInterface $cache
80
     *
81
     * @return $this
82
     */
83 2
    public function setCache(BrowscapCacheInterface $cache)
84
    {
85 2
        $this->cache = $cache;
86
87 2
        return $this;
88
    }
89
90
    /**
91
     * Configures the current command.
92
     */
93 2 View Code Duplication
    protected function configure()
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...
94
    {
95 2
        $this
96 2
            ->setName('browscap:parse')
97 2
            ->setDescription('Parses a user agent string and dumps the results.')
98 2
            ->addArgument(
99 2
                'user-agent',
100 2
                InputArgument::REQUIRED,
101 2
                'User agent string to analyze',
102
                null
103 2
            )
104 2
            ->addOption(
105 2
                'debug',
106 2
                'd',
107 2
                InputOption::VALUE_NONE,
108
                'Should the debug mode entered?'
109 2
            )
110 2
            ->addOption(
111 2
                'cache',
112 2
                'c',
113 2
                InputOption::VALUE_OPTIONAL,
114 2
                'Where the cache files are located',
115 2
                $this->defaultCacheFolder
116 2
            )
117
        ;
118 2
    }
119
120
    /**
121
     * @param InputInterface  $input
122
     * @param OutputInterface $output
123
     *
124
     * @return int|null|void
125
     */
126 1
    protected function execute(InputInterface $input, OutputInterface $output)
127
    {
128 1
        $loggerHelper = new LoggerHelper();
129 1
        $logger       = $loggerHelper->create($input->getOption('debug'));
130
131 1
        $browscap = new Browscap();
132
133
        $browscap
134 1
            ->setLogger($logger)
135 1
            ->setCache($this->getCache($input))
136
        ;
137
138 1
        $result = $browscap->getBrowser($input->getArgument('user-agent'));
139
140 1
        if (!defined('JSON_PRETTY_PRINT')) {
141
            // not defined in PHP 5.3
142
            define('JSON_PRETTY_PRINT', 128);
143
        }
144
145 1
        $output->writeln(json_encode($result, JSON_PRETTY_PRINT));
146 1
    }
147
148
    /**
149
     * @param InputInterface $input
150
     *
151
     * @return BrowscapCacheInterface
152
     */
153 1 View Code Duplication
    private function getCache(InputInterface $input)
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...
154
    {
155 1
        if (null === $this->cache) {
156
            $cacheAdapter = new File(array(File::DIR => $input->getOption('cache')));
157
            $this->cache  = new BrowscapCache($cacheAdapter);
158
        }
159
160 1
        return $this->cache;
161
    }
162
}
163