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

Browscap   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 198
Duplicated Lines 15.15 %

Coupling/Cohesion

Components 1
Dependencies 13

Test Coverage

Coverage 100%

Importance

Changes 24
Bugs 6 Features 7
Metric Value
wmc 18
c 24
b 6
f 7
lcom 1
cbo 13
dl 30
loc 198
ccs 54
cts 54
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setFormatter() 0 6 1
A getFormatter() 0 8 2
A getCache() 14 14 2
A setCache() 16 16 3
A setParser() 0 6 1
A getParser() 0 15 2
A setLogger() 0 6 1
B getBrowser() 0 24 4
A getLogger() 0 8 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    Browscap
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
 */
29
30
namespace BrowscapPHP;
31
32
use BrowscapPHP\Cache\BrowscapCache;
33
use BrowscapPHP\Cache\BrowscapCacheInterface;
34
use BrowscapPHP\Helper\Quoter;
35
use BrowscapPHP\Parser\ParserInterface;
36
use Psr\Log\LoggerInterface;
37
use Psr\Log\NullLogger;
38
use WurflCache\Adapter\AdapterInterface;
39
use WurflCache\Adapter\File;
40
41
/**
42
 * Browscap.ini parsing class with caching and update capabilities
43
 *
44
 * @category   Browscap-PHP
45
 * @package    Browscap
46
 * @author     Jonathan Stoppani <[email protected]>
47
 * @author     Vítor Brandão <[email protected]>
48
 * @author     Mikołaj Misiurewicz <[email protected]>
49
 * @author     Christoph Ziegenberg <[email protected]>
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 Browscap
57
{
58
    /**
59
     * Parser to use
60
     *
61
     * @var \BrowscapPHP\Parser\ParserInterface
62
     */
63
    private $parser = null;
64
65
    /**
66
     * Formatter to use
67
     *
68
     * @var \BrowscapPHP\Formatter\FormatterInterface
69
     */
70
    private $formatter = null;
71
72
    /**
73
     * The cache instance
74
     *
75
     * @var \BrowscapPHP\Cache\BrowscapCacheInterface
76
     */
77
    private $cache = null;
78
79
    /**
80
     * @var @var \Psr\Log\LoggerInterface
81
     */
82
    private $logger = null;
83
84
    /**
85
     * Set theformatter instance to use for the getBrowser() result
86
     *
87
     * @param \BrowscapPHP\Formatter\FormatterInterface $formatter
88
     *
89
     * @return \BrowscapPHP\Browscap
90
     */
91 5
    public function setFormatter(Formatter\FormatterInterface $formatter)
92
    {
93 5
        $this->formatter = $formatter;
94
95 5
        return $this;
96
    }
97
98
    /**
99
     * @return \BrowscapPHP\Formatter\FormatterInterface
100
     */
101 3
    public function getFormatter()
102
    {
103 3
        if (null === $this->formatter) {
104 2
            $this->setFormatter(new Formatter\PhpGetBrowser());
105
        }
106
107 3
        return $this->formatter;
108
    }
109
110
    /**
111
     * Gets a cache instance
112
     *
113
     * @return \BrowscapPHP\Cache\BrowscapCacheInterface
114
     */
115 8 View Code Duplication
    public function getCache()
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...
116
    {
117 8
        if (null === $this->cache) {
118 3
            $cacheDirectory = __DIR__.'/../resources/';
119
120 3
            $cacheAdapter = new File(
121 3
                array(File::DIR => $cacheDirectory)
122 3
            );
123
124 3
            $this->cache = new BrowscapCache($cacheAdapter);
125
        }
126
127 8
        return $this->cache;
128
    }
129
130
    /**
131
     * Sets a cache instance
132
     *
133
     * @param \BrowscapPHP\Cache\BrowscapCacheInterface|\WurflCache\Adapter\AdapterInterface $cache
134
     *
135
     * @throws \BrowscapPHP\Exception
136
     * @return \BrowscapPHP\Browscap
137
     */
138 6 View Code Duplication
    public function setCache($cache)
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...
139
    {
140 6
        if ($cache instanceof BrowscapCacheInterface) {
141 4
            $this->cache = $cache;
142 6
        } elseif ($cache instanceof AdapterInterface) {
143 1
            $this->cache = new BrowscapCache($cache);
144 1
        } else {
145 1
            throw new Exception(
146
                'the cache has to be an instance of \BrowscapPHP\Cache\BrowscapCacheInterface or '
147 1
                .'an instanceof of \WurflCache\Adapter\AdapterInterface',
148
                Exception::CACHE_INCOMPATIBLE
149 1
            );
150
        }
151
152 5
        return $this;
153
    }
154
155
    /**
156
     * Sets the parser instance to use
157
     *
158
     * @param \BrowscapPHP\Parser\ParserInterface $parser
159
     *
160
     * @return \BrowscapPHP\Browscap
161
     */
162 3
    public function setParser(ParserInterface $parser)
163
    {
164 3
        $this->parser = $parser;
165
166 3
        return $this;
167
    }
168
169
    /**
170
     * returns an instance of the used parser class
171
     *
172
     * @return \BrowscapPHP\Parser\ParserInterface
173
     */
174 5
    public function getParser()
175
    {
176 5
        if (null === $this->parser) {
177 2
            $cache  = $this->getCache();
178 2
            $logger = $this->getLogger();
179 2
            $quoter = new Quoter();
180
181 2
            $patternHelper = new Parser\Helper\GetPattern($cache, $logger);
182 2
            $dataHelper    = new Parser\Helper\GetData($cache, $logger, $quoter);
183
184 2
            $this->parser = new Parser\Ini($patternHelper, $dataHelper, $this->getFormatter());
185
        }
186
187 5
        return $this->parser;
188
    }
189
190
    /**
191
     * Sets a logger instance
192
     *
193
     * @param \Psr\Log\LoggerInterface $logger
194
     *
195
     * @return \BrowscapPHP\Browscap
196
     */
197 1
    public function setLogger(LoggerInterface $logger)
198
    {
199 1
        $this->logger = $logger;
200
201 1
        return $this;
202
    }
203
204
    /**
205
     * returns a logger instance
206
     *
207
     * @return \Psr\Log\LoggerInterface
208
     */
209 3
    public function getLogger()
210
    {
211 3
        if (null === $this->logger) {
212 2
            $this->logger = new NullLogger();
213
        }
214
215 3
        return $this->logger;
216
    }
217
218
    /**
219
     * parses the given user agent to get the information about the browser
220
     *
221
     * if no user agent is given, it uses {@see \BrowscapPHP\Helper\Support} to get it
222
     *
223
     * @param string $userAgent the user agent string
224
     *
225
     * @throws \BrowscapPHP\Exception
226
     * @return \stdClass              the object containing the browsers details. Array if
227
     *                                $return_array is set to true.
228
     */
229 5
    public function getBrowser($userAgent = null)
230
    {
231 5
        if (null === $this->getCache()->getVersion()) {
232
            // there is no active/warm cache available
233 1
            throw new Exception('there is no active cache available, please run the update command');
234
        }
235
236
        // Automatically detect the useragent
237 4
        if (!isset($userAgent)) {
238 2
            $support   = new Helper\Support($_SERVER);
239 2
            $userAgent = $support->getUserAgent();
240
        }
241
242
        // try to get browser data
243 4
        $formatter = $this->getParser()->getBrowser($userAgent);
244
245
        // if return is still NULL, updates are disabled... in this
246
        // case we return an empty formatter instance
247 4
        if ($formatter === null) {
248 2
            return $this->getFormatter()->getData();
249
        }
250
251 2
        return $formatter->getData();
252
    }
253
}
254