Completed
Push — master ( 06e640...bcbb74 )
by Tim
01:53 queued 10s
created

ExtendedJsonParser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 92
Duplicated Lines 27.17 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 25
loc 92
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A parse() 20 20 2
A processExtensionLibraries() 5 19 5

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
/**
4
 * TechDivision\Import\Configuration\Jms\Parsers\JsonParser
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2020 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-configuration-jms
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Configuration\Jms\Parsers;
22
23
use TechDivision\Import\Configuration\Jms\Utils\ArrayUtilInterface;
24
use TechDivision\Import\Loaders\LoaderInterface;
25
26
/**
27
 * The JSON configuration parser implementation.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2020 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import-configuration-jms
33
 * @link      http://www.techdivision.com
34
 */
35
class ExtendedJsonParser extends JsonParser
36
{
37
38
    /**
39
     * The key for the configuration snippet that contains the extension libary configuration.
40
     *
41
     * @var string
42
     */
43
    const EXTENSION_LIBRARIES = 'extension-libraries';
44
45
    /**
46
     * The absolute path to the vendor directory
47
     *
48
     * @var string
49
     */
50
    protected $vendorDir;
51
52
    /**
53
     * Initializes the parser with the array utility instance.
54
     *
55
     * @param \TechDivision\Import\Configuration\Jms\Utils\ArrayUtilInterface $arrayUtil       The utility instance
56
     * @param \TechDivision\Import\Loaders\LoaderInterface                    $vendorDirLoader The loader for the absolute vendor directory
57
     */
58
    public function __construct(ArrayUtilInterface $arrayUtil, LoaderInterface $vendorDirLoader)
59
    {
60
61
        // pass the array utility to the parent constructor
62
        parent::__construct($arrayUtil);
63
64
        // initialize the absolute path to the vendor directory
65
        $this->vendorDir = $vendorDirLoader->load();
0 ignored issues
show
Documentation Bug introduced by
It seems like $vendorDirLoader->load() of type object<ArrayAccess> is incompatible with the declared type string of property $vendorDir.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
66
    }
67
68
    /**
69
     * Parsing the configuration and merge it recursively.
70
     *
71
     * @param string $installationDir         The assumed Magento installation directory
72
     * @param string $defaultConfigurationDir The default configuration directory
73
     * @param array  $directories             An array with diretories to parse
74
     *
75
     * @return string The parsed configuration as string
76
     */
77 View Code Duplication
    public function parse(string $installationDir, string $defaultConfigurationDir, array $directories) : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
78
    {
79
80
        // initialize the array that'll contain the configuration structure
81
        $main = array();
82
83
        // iterate over the found directories to parse them for configuration files
84
        foreach ($directories as $directory) {
85
            $this->process($main, $directory);
86
        }
87
88
        // process the configuration of the extension libaries, if available
89
        $this->processExtensionLibraries($main, $defaultConfigurationDir);
90
91
        // process the additional vendor directories, if available
92
        $this->processAdditionalVendorDirs($main, $installationDir, $defaultConfigurationDir);
93
94
        // return the JSON encoded configuration
95
        return json_encode($main, JSON_PRETTY_PRINT);
96
    }
97
98
    /**
99
     * Process the configuration files found in the extension libary directories
100
     * and merge/replace its content in the also passed main configuration file.
101
     *
102
     * @param array  $main                    The main configuration to merge/replace the extension libary files to
103
     * @param string $defaultConfigurationDir The default configuration directory
104
     *
105
     * @return void
106
     */
107
    protected function processExtensionLibraries(array &$main, string $defaultConfigurationDir) : void
108
    {
109
110
        // query whether or not extension libary directories has been registered in the configuration
111
        if (isset($main[ExtendedJsonParser::EXTENSION_LIBRARIES]) && is_array($main[ExtendedJsonParser::EXTENSION_LIBRARIES])) {
112
            // iterate over the extension library directory configurations
113
            foreach ($main[ExtendedJsonParser::EXTENSION_LIBRARIES] as $extensionLibrary) {
114
                // prepare the absolute path to the extension library
115
                $libDir = sprintf('%s/%s/%s', $this->vendorDir, $extensionLibrary, $defaultConfigurationDir);
116
117
                // create the canonicalized absolute pathname and try to load the configuration
118 View Code Duplication
                if (is_dir($libraryDir = realpath($libDir))) {
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...
119
                    $this->process($main, $libraryDir);
120
                } else {
121
                    throw new \Exception(sprintf('Can\'t find find extension library directory "%s"', $libDir));
122
                }
123
            }
124
        }
125
    }
126
}
127