Completed
Push — master ( dca6dc...178b4a )
by Richard
06:29
created

App::create_dic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 57
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 1

Importance

Changes 12
Bugs 0 Features 1
Metric Value
c 12
b 0
f 1
dl 0
loc 57
ccs 28
cts 28
cp 1
rs 9.6818
cc 1
eloc 34
nc 1
nop 2
crap 1

How to fix   Long Method   

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
2
/******************************************************************************
3
 * An implementation of dicto (scg.unibe.ch/dicto) in and for PHP.
4
 * 
5
 * Copyright (c) 2016 Richard Klees <[email protected]>
6
 *
7
 * This software is licensed under The MIT License. You should have received 
8
 * a copy of the license along with the code.
9
 */
10
11
namespace Lechimp\Dicto\App;
12
13
use Lechimp\Dicto\App\RuleLoader;
14
use Lechimp\Dicto\Rules\Ruleset;
15
use Symfony\Component\Yaml\Yaml;
16
use Pimple\Container;
17
use PhpParser\ParserFactory;
18
19
/**
20
 * The App to be run from a script.
21
 */
22
class App {
23
    /**
24
     * @var RuleLoader
25
     */
26
    protected $rule_loader;
27
28 3
    public function __construct() {
29 3
        ini_set('xdebug.max_nesting_level', 200);
30 3
        $this->rule_loader = new RuleLoader();
31 3
    }
32
33
    /**
34
     * Run the app.
35
     *
36
     * @param   array   $params     from commandline
37
     * @return  null
38
     */
39
    public function run(array $params) {
40
        if (count($params) < 2) {
41
            throw new \RuntimeException(
42
                "Expected path to rule-file as first parameter.");
43
        }
44
45
        $configs = array();
46
        list($ruleset, $configs[]) = $this->load_rules_file($params[1]);
47
48
        // drop programm name and rule file path
49
        array_shift($params);
50
        array_shift($params);
51
52
        $this->load_extra_configs($params, $configs);
53
54
        $dic = $this->create_dic($ruleset, $configs);
55
56
        $dic["engine"]->run();
57
    }
58
59
    /**
60
     * Load rules and initial config from a *.php-file.
61
     *
62
     * @param   string  $path
63
     * @return  array   ($ruleset, $config)
64
     */
65 3
    protected function load_rules_file($path) {
66 3
        if (!file_exists($path)) {
67
            throw new \RuntimeException("Unknown rule-file '$path'");
68
        }
69 3
        list($ruleset, $config) = $this->rule_loader->load_rules_from($path);
70 3
        assert('is_array($config)');
71 3
        assert('$ruleset instanceof \\Lechimp\\Dicto\\Rules\\RuleSet');
72 3
        return array($ruleset, $config);
73
    }
74
75
    /**
76
     * Load extra configs from yaml files.
77
     *
78
     * @param   array   $config_file_paths
79
     * @param   array   &$configs_array
80
     * @return  null
81
     */
82
    protected function load_extra_configs(array $config_file_paths, array &$configs_array) {
83
        foreach ($config_file_paths as $config_file) {
84
            if (!file_exists($config_file)) {
85
                throw new \RuntimeException("Unknown config-file '$config_file'");
86
            }
87
            $configs_array[] = Yaml::parse(file_get_contents($config_file));
88
        }
89
    }
90
91
    /**
92
     * Create and initialize the DI-container.
93
     *
94
     * @param   RuleSet     $ruleset
95
     * @param   array       &$configs
96
     * @return  Container
97
     */
98 2
    protected function create_dic(RuleSet $ruleset, array &$configs) {
99 2
        $container = new Container();
100
101
        $container["config"] = function ($c) use (&$configs) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102 1
            return new Config($configs);
103
        };
104
105 2
        $container["ruleset"] = $ruleset;
106
107
        $container["engine"] = function($c) {
108
            return new Engine
109 1
                ( $c["log"]
110 1
                , $c["config"]
111 1
                , $c["database_factory"]
112 1
                , $c["indexer_factory"]
113 1
                , $c["analyzer_factory"]
114 1
                );
115
        };
116
117
        $container["indexer_factory"] = function($c) {
118
            return new \Lechimp\Dicto\Indexer\IndexerFactory
119 2
                ( $c["log"]
120 2
                , $c["php_parser"]
121 2
                , array
122 2
                    ( new \Lechimp\Dicto\Rules\ContainText()
123 2
                    , new \Lechimp\Dicto\Rules\DependOn()
124 2
                    , new \Lechimp\Dicto\Rules\Invoke()
125 2
                    )
126 2
                );
127
        };
128
129
        $container["php_parser"] = function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
130 2
            return (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
131
        };
132
133
        $container["database_factory"] = function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
134 1
            return new DBFactory();
135
        };
136
137
        $container["analyzer_factory"] = function($c) {
138
            return new \Lechimp\Dicto\Analysis\AnalyzerFactory
139 1
                ( $c["log"]
140 1
                , $c["report_generator"]
141 1
                , $c["ruleset"]
142 1
                );
143
        };
144
145
        $container["log"] = function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
146 2
            return new CLILogger();
147
        };
148
149 1
        $container["report_generator"] = function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
150 1
            return new CLIReportGenerator();
151
        };
152
153 2
        return $container;
154
    }
155
}
156