ParserFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 20.51 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 3
Bugs 1 Features 2
Metric Value
wmc 6
c 3
b 1
f 2
lcom 0
cbo 4
dl 8
loc 39
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B create() 8 23 6

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
namespace PhpParser;
4
5
class ParserFactory {
6
    const PREFER_PHP7 = 1;
7
    const PREFER_PHP5 = 2;
8
    const ONLY_PHP7 = 3;
9
    const ONLY_PHP5 = 4;
10
11
    /**
12
     * Creates a Parser instance, according to the provided kind.
13
     *
14
     * @param int        $kind  One of ::PREFER_PHP7, ::PREFER_PHP5, ::ONLY_PHP7 or ::ONLY_PHP5
15
     * @param Lexer|null $lexer Lexer to use. Defaults to emulative lexer when not specified
16
     * @param array      $parserOptions Parser options. See ParserAbstract::__construct() argument
17
     *
18
     * @return Parser The parser instance
19
     */
20
    public function create($kind, Lexer $lexer = null, array $parserOptions = array()) {
21
        if (null === $lexer) {
22
            $lexer = new Lexer\Emulative();
23
        }
24
        switch ($kind) {
25 View Code Duplication
            case self::PREFER_PHP7:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
26
                return new Parser\Multiple([
27
                    new Parser\Php7($lexer, $parserOptions), new Parser\Php5($lexer, $parserOptions)
28
                ]);
29 View Code Duplication
            case self::PREFER_PHP5:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
30
                return new Parser\Multiple([
31
                    new Parser\Php5($lexer, $parserOptions), new Parser\Php7($lexer, $parserOptions)
32
                ]);
33
            case self::ONLY_PHP7:
34
                return new Parser\Php7($lexer, $parserOptions);
35
            case self::ONLY_PHP5:
36
                return new Parser\Php5($lexer, $parserOptions);
37
            default:
38
                throw new \LogicException(
39
                    'Kind must be one of ::PREFER_PHP7, ::PREFER_PHP5, ::ONLY_PHP7 or ::ONLY_PHP5'
40
                );
41
        }
42
    }
43
}
44