ParserFactory::initOptions()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
ccs 5
cts 7
cp 0.7143
crap 3.2098
1
<?php
2
/**
3
 * This file is part of PHP Mess Detector.
4
 *
5
 * Copyright (c) Manuel Pichler <[email protected]>.
6
 * All rights reserved.
7
 *
8
 * Licensed under BSD License
9
 * For full copyright and license information, please see the LICENSE file.
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @author Manuel Pichler <[email protected]>
13
 * @copyright Manuel Pichler. All rights reserved.
14
 * @license https://opensource.org/licenses/bsd-license.php BSD License
15
 * @link http://phpmd.org/
16
 */
17
18
namespace PHPMD;
19
20
use PDepend\Application;
21
use PDepend\Engine;
22
use PDepend\Input\ExcludePathFilter;
23
use PDepend\Input\ExtensionFilter;
24
25
/**
26
 * Simple factory that is used to return a ready to use PDepend instance.
27
 */
28
class ParserFactory
29
{
30
    /**
31
     * Mapping between phpmd option names and those used by pdepend.
32
     *
33
     * @var array
34
     */
35
    private $phpmd2pdepend = array(
36
        'coverage'  =>  'coverage-report'
37
    );
38
39
    /**
40
     * Creates the used {@link \PHPMD\Parser} analyzer instance.
41
     *
42
     * @param \PHPMD\PHPMD $phpmd
43
     * @return \PHPMD\Parser
44
     */
45 12
    public function create(PHPMD $phpmd)
46
    {
47 12
        $pdepend = $this->createInstance();
48 12
        $pdepend = $this->init($pdepend, $phpmd);
49
50 12
        return new Parser($pdepend);
51
    }
52
53
    /**
54
     * Creates a clean php depend instance with some base settings.
55
     *
56
     * @return \PDepend\Engine
57
     */
58 12
    private function createInstance()
59
    {
60 12
        $application = new Application();
61
62 12
        if (file_exists(getcwd() . '/pdepend.xml')) {
63
            $application->setConfigurationFile(getcwd() . '/pdepend.xml');
64 12
        } elseif (file_exists(getcwd() . '/pdepend.xml.dist')) {
65 2
            $application->setConfigurationFile(getcwd() . '/pdepend.xml.dist');
66
        }
67
68 12
        return $application->getEngine();
69
    }
70
71
    /**
72
     * Configures the given PDepend\Engine instance based on some user settings.
73
     *
74
     * @param \PDepend\Engine $pdepend
75
     * @param \PHPMD\PHPMD $phpmd
76
     * @return \PDepend\Engine
77
     */
78 12
    private function init(Engine $pdepend, PHPMD $phpmd)
79
    {
80 12
        $this->initOptions($pdepend, $phpmd);
81 12
        $this->initInput($pdepend, $phpmd);
82 12
        $this->initIgnores($pdepend, $phpmd);
83 12
        $this->initExtensions($pdepend, $phpmd);
84
85 12
        return $pdepend;
86
    }
87
88
    /**
89
     * Configures the input source.
90
     *
91
     * @param \PDepend\Engine $pdepend
92
     * @param \PHPMD\PHPMD $phpmd
93
     * @return void
94
     */
95 12
    private function initInput(Engine $pdepend, PHPMD $phpmd)
96
    {
97 12
        foreach (explode(',', $phpmd->getInput()) as $path) {
98 12
            if (is_dir(trim($path))) {
99 3
                $pdepend->addDirectory(trim($path));
100
            } else {
101 12
                $pdepend->addFile(trim($path));
102
            }
103
        }
104 12
    }
105
106
    /**
107
     * Initializes the ignored files and path's.
108
     *
109
     * @param \PDepend\Engine $pdepend
110
     * @param \PHPMD\PHPMD $phpmd
111
     * @return void
112
     */
113 12
    private function initIgnores(Engine $pdepend, PHPMD $phpmd)
114
    {
115 12
        if (count($phpmd->getIgnorePattern()) > 0) {
116 12
            $pdepend->addFileFilter(
117 12
                new ExcludePathFilter($phpmd->getIgnorePattern())
118
            );
119
        }
120 12
    }
121
122
    /**
123
     * Initializes the accepted php source file extensions.
124
     *
125
     * @param \PDepend\Engine $pdepend
126
     * @param \PHPMD\PHPMD $phpmd
127
     * @return void
128
     */
129 12
    private function initExtensions(Engine $pdepend, PHPMD $phpmd)
130
    {
131 12
        if (count($phpmd->getFileExtensions()) > 0) {
132 12
            $pdepend->addFileFilter(
133 12
                new ExtensionFilter($phpmd->getFileExtensions())
134
            );
135
        }
136 12
    }
137
138
    /**
139
     * Initializes additional options for pdepend.
140
     *
141
     * @param \PDepend\Engine $pdepend
142
     * @param \PHPMD\PHPMD $phpmd
143
     * @return void
144
     */
145 12
    private function initOptions(Engine $pdepend, PHPMD $phpmd)
146
    {
147 12
        $options = array();
148 12
        foreach (array_filter($phpmd->getOptions()) as $name => $value) {
149
            if (isset($this->phpmd2pdepend[$name])) {
150
                $options[$this->phpmd2pdepend[$name]] = $value;
151
            }
152
        }
153 12
        $pdepend->setOptions($options);
154 12
    }
155
}
156