Completed
Pull Request — master (#221)
by Thomas
28:24 queued 26:59
created

Pattern   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 85
ccs 18
cts 21
cp 0.8571
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B getHashForPattern() 0 29 5
A getHashForParts() 0 4 1
A getPatternLength() 0 4 1
1
<?php
2
/**
3
 * This file is part of the browscap-php package.
4
 *
5
 * Copyright (c) 1998-2017, Browser Capabilities Project
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
declare(strict_types = 1);
12
namespace BrowscapPHP\Parser\Helper;
13
14
/**
15
 * includes general functions for the work with patterns
16
 */
17
final class Pattern
18
{
19
    private function __construct()
20
    {
21
    }
22
23
    /**
24
     * Gets a hash or an array of hashes from the first characters of a pattern/user agent, that can
25
     * be used for a fast comparison, by comparing only the hashes, without having to match the
26
     * complete pattern against the user agent.
27
     *
28
     * With the variants options, all variants from the maximum number of pattern characters to one
29
     * character will be returned. This is required in some cases, the a placeholder is used very
30
     * early in the pattern.
31
     *
32
     * Example:
33
     *
34
     * Pattern: "Mozilla/* (Nintendo 3DS; *) Version/*"
35
     * User agent: "Mozilla/5.0 (Nintendo 3DS; U; ; en) Version/1.7567.US"
36
     *
37
     * In this case the has for the pattern is created for "Mozilla/" while the pattern
38
     * for the hash for user agent is created for "Mozilla/5.0". The variants option
39
     * results in an array with hashes for "Mozilla/5.0", "Mozilla/5.", "Mozilla/5",
40
     * "Mozilla/" ... "M", so that the pattern hash is included.
41
     *
42
     * @param string $pattern
43
     * @param bool   $variants
44
     *
45
     * @return string[]
46
     */
47 2
    public static function getHashForPattern(string $pattern, bool $variants = false) : array
48
    {
49 2
        $regex = '/^([^\.\*\?\s\r\n\\\\]+).*$/';
50 2
        $pattern = substr($pattern, 0, 32);
51 2
        $matches = [];
52
53 2
        if (! preg_match($regex, $pattern, $matches) || ! isset($matches[1])) {
54
            return [md5('')];
55
        }
56
57 2
        $string = $matches[1];
58
59 2
        if (true === $variants) {
60 1
            $patternStarts = [];
61
62 1
            for ($i = strlen($string); $i >= 1; --$i) {
63 1
                $string = substr($string, 0, $i);
64 1
                $patternStarts[] = md5($string);
65
            }
66
67
            // Add empty pattern start to include patterns that start with "*",
68
            // e.g. "*FAST Enterprise Crawler*"
69 1
            $patternStarts[] = md5('');
70
71 1
            return $patternStarts;
72
        }
73
74 1
        return [md5($string)];
75
    }
76
77
    /**
78
     * returns a hash for one pattern
79
     *
80
     * @param $pattern
81
     *
82
     * @return string
83
     */
84 1
    public static function getHashForParts(string $pattern) : string
85
    {
86 1
        return md5($pattern);
87
    }
88
89
    /**
90
     * Gets the minimum length of the patern (used in the getPatterns() method to
91
     * check against the user agent length)
92
     *
93
     * @param string $pattern
94
     *
95
     * @return int
96
     */
97 1
    public static function getPatternLength(string $pattern) : int
98
    {
99 1
        return strlen(str_replace('*', '', $pattern));
100
    }
101
}
102