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

ConvertCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 121
Duplicated Lines 33.88 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 96.23%

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 7
c 5
b 1
f 1
lcom 1
cbo 7
dl 41
loc 121
ccs 51
cts 53
cp 0.9623
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setCache() 0 6 1
B configure() 32 32 1
A execute() 0 22 2
A __construct() 0 7 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\BrowscapUpdater;
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
 * command to convert a downloaded Browscap ini file and write it to the cache
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 ConvertCommand extends Command
57
{
58
    /**
59
     * @var BrowscapCacheInterface
60
     */
61
    private $cache = null;
62
63
    /**
64
     * @var string
65
     */
66
    private $defaultIniFile;
67
68
    /**
69
     * @var string
70
     */
71
    private $defaultCacheFolder;
72
73
    /**
74
     * @param string $defaultCacheFolder
75
     * @param string $defaultIniFile
76
     */
77 2
    public function __construct($defaultCacheFolder, $defaultIniFile)
78
    {
79 2
        $this->defaultCacheFolder = $defaultCacheFolder;
80 2
        $this->defaultIniFile     = $defaultIniFile;
81
82 2
        parent::__construct();
83 2
    }
84
85
    /**
86
     * @param \BrowscapPHP\Cache\BrowscapCacheInterface $cache
87
     *
88
     * @return $this
89
     */
90 2
    public function setCache(BrowscapCacheInterface $cache)
91
    {
92 2
        $this->cache = $cache;
93
94 2
        return $this;
95
    }
96
97
    /**
98
     * Configures the current command.
99
     */
100 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...
101
    {
102 2
        $this
103 2
            ->setName('browscap:convert')
104 2
            ->setDescription('Converts an existing browscap.ini file to a cache.php file.')
105 2
            ->addArgument(
106 2
                'file',
107 2
                InputArgument::OPTIONAL,
108 2
                'Path to the browscap.ini file',
109 2
                $this->defaultIniFile
110 2
            )
111 2
            ->addOption(
112 2
                'no-backup',
113 2
                null,
114 2
                InputOption::VALUE_NONE,
115
                'Do not backup the previously existing file'
116 2
            )
117 2
            ->addOption(
118 2
                'debug',
119 2
                'd',
120 2
                InputOption::VALUE_NONE,
121
                'Should the debug mode entered?'
122 2
            )
123 2
            ->addOption(
124 2
                'cache',
125 2
                'c',
126 2
                InputOption::VALUE_OPTIONAL,
127 2
                'Where the cache files are located',
128 2
                $this->defaultCacheFolder
129 2
            )
130
        ;
131 2
    }
132
133
    /**
134
     * @param InputInterface  $input
135
     * @param OutputInterface $output
136
     *
137
     * @return int|null|void
138
     */
139 1
    protected function execute(InputInterface $input, OutputInterface $output)
140
    {
141 1
        $loggerHelper = new LoggerHelper();
142 1
        $logger       = $loggerHelper->create($input->getOption('debug'));
143
144 1
        $logger->info('initializing converting process');
145
146 1
        $browscap = new BrowscapUpdater();
147
148
        $browscap
149 1
            ->setLogger($logger)
150 1
            ->setCache($this->getCache($input))
151
        ;
152
153 1
        $logger->info('started converting local file');
154
155 1
        $file = ($input->getArgument('file') ? $input->getArgument('file') : ($this->defaultIniFile));
156
157 1
        $browscap->convertFile($file, $input->getOption('no-backup'));
0 ignored issues
show
Unused Code introduced by
The call to BrowscapUpdater::convertFile() has too many arguments starting with $input->getOption('no-backup').

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
158
159 1
        $logger->info('finished converting local file');
160 1
    }
161
162
    /**
163
     * @param InputInterface $input
164
     *
165
     * @return BrowscapCacheInterface
166
     */
167 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...
168
    {
169 1
        if (null === $this->cache) {
170
            $cacheAdapter = new File(array(File::DIR => $input->getOption('cache')));
171
            $this->cache  = new BrowscapCache($cacheAdapter);
172
        }
173
174 1
        return $this->cache;
175
    }
176
}
177