Passed
Push — master ( d2ebdc...958928 )
by Bogdan
05:05
created

RegExpExtractor::extract()   C

Complexity

Conditions 11
Paths 24

Size

Total Lines 48
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 0
cts 23
cp 0
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 23
nc 24
nop 4
crap 132

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
/*
4
 * This file is part of the pinepain/js-sandbox PHP library.
5
 *
6
 * Copyright (c) 2016-2017 Bogdan Padalko <[email protected]>
7
 *
8
 * Licensed under the MIT license: http://opensource.org/licenses/MIT
9
 *
10
 * For the full copyright and license information, please view the
11
 * LICENSE file that was distributed with this source or visit
12
 * http://opensource.org/licenses/MIT
13
 */
14
15
16
namespace Pinepain\JsSandbox\Extractors\PlainExtractors;
17
18
19
use Pinepain\JsSandbox\Extractors\Definition\PlainExtractorDefinitionInterface;
20
use Pinepain\JsSandbox\Extractors\ExtractorException;
21
use Pinepain\JsSandbox\Extractors\ExtractorInterface;
22
use V8\Context;
23
use V8\RegExpObject;
24
use V8\Value;
25
26
27
class RegExpExtractor implements PlainExtractorInterface
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function extract(Context $context, Value $value, PlainExtractorDefinitionInterface $definition, ExtractorInterface $extractor)
33
    {
34
        if ($value instanceof RegExpObject) {
35
36
            $regex = $value->getSource()->value();
37
38
            if ($definition->getNext() && 'string' == $definition->getNext()->getName()) {
39
                return $regex;
40
            }
41
42
            $flags  = $value->getFlags();
43
            $regexp = '/' . preg_quote($regex, '/') . '/';
44
45
            if (!$flags) {
46
                return $regexp;
47
            }
48
49
            if ($flags & RegExpObject::FLAG_GLOBAL) {
50
                // global flag is not supported in PHP
51
                throw new ExtractorException('Global flag is not supported');
52
            }
53
54
            if ($flags & RegExpObject::FLAG_IGNORE_CASE) {
55
                $regexp .= 'i';
56
            }
57
58
            if ($flags & RegExpObject::FLAG_MULTILINE) {
59
                $regexp .= 'm';
60
            }
61
62
            if ($flags & RegExpObject::FLAG_STICKY) {
63
                // sticky flag is not supported in PHP
64
                throw new ExtractorException('Sticky flag is not supported');
65
            }
66
67
            if ($flags & RegExpObject::FLAG_UNICODE) {
68
                $regexp .= 'u';
69
            }
70
71
            if ($flags & RegExpObject::FLAG_DOTALL) {
72
                $regexp .= 's';
73
            }
74
75
            return $regexp;
76
        }
77
78
        throw new ExtractorException('Value must be of the type regexp, ' . $value->typeOf()->value() . ' given');
79
    }
80
}
81