Completed
Push — upgrade ( e29d4e...a26676 )
by Simonas
01:42
created

MappingPass   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 9.46 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 7
loc 74
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 24 3
A getNamespaces() 7 22 3

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
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchBundle\DependencyInjection\Compiler;
13
14
use ONGR\ElasticsearchBundle\DependencyInjection\Configuration;
15
use ONGR\ElasticsearchBundle\Service\IndexService;
16
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Definition;
19
use Symfony\Component\DependencyInjection\Reference;
20
use Symfony\Component\Finder\Finder;
21
22
/**
23
 * Compiles elastic search data.
24
 */
25
class MappingPass implements CompilerPassInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     * @throws \Exception
30
     */
31
    public function process(ContainerBuilder $container)
32
    {
33
        $analysis = $container->getParameter(Configuration::ONGR_ANALYSIS_CONFIG);
0 ignored issues
show
Unused Code introduced by
$analysis is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
34
        $additionalDirs = $container->getParameter(Configuration::ONGR_INCLUDE_DIR_CONFIG);
35
36
        $collector = $container->get('es.metadata_collector');
37
38
        $kernelProjectDir = $container->getParameter('kernel.project_dir');
39
40
        $namespaces = $this->getNamespaces($kernelProjectDir . '/src');
41
42
        foreach ($additionalDirs as $directory) {
43
            $namespaces = array_merge($namespaces, $this->getNamespaces($directory));
44
        }
45
46
        foreach ($namespaces as $namespace) {
47
            $indexMapping = $collector->getMapping($namespace);
0 ignored issues
show
Unused Code introduced by
$indexMapping is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
48
            $definition = new Definition(IndexService::class, [
0 ignored issues
show
Unused Code introduced by
$definition is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
49
50
            ]);
51
52
//            $container->autowire($namespace, IndexService::class);
53
        }
54
    }
55
56
    private function getNamespaces($directory) {
57
        $documentsDirectory = DIRECTORY_SEPARATOR . str_replace('\\', '/', $directory) . DIRECTORY_SEPARATOR;
58
59
        if (!is_dir($directory)) {
60
            return [];
61
        }
62
63
        $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory));
64
        $files = new \RegexIterator($iterator, '/^.+\.php$/i', \RecursiveRegexIterator::GET_MATCH);
65
66
        $documents = [];
67
68 View Code Duplication
        foreach ($files as $file => $v) {
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...
69
            $documents[] = str_replace(
70
                DIRECTORY_SEPARATOR,
71
                '\\',
72
                substr(strstr($file, $documentsDirectory), strlen($documentsDirectory), -4)
73
            );
74
        }
75
76
        return $documents;
77
    }
78
79
//
80
//        private function getFullNamespace($filename) {
81
//            $lines = file($filename);
82
//            $namespaceLine = array_shift(preg_grep('/^namespace /', $lines));
83
//            $match = array();
84
//            preg_match('/^namespace (.*);$/', $namespaceLine, $match);
85
//            $fullNamespace = array_pop($match);
86
//
87
//            return $fullNamespace;
88
//        }
89
//
90
//       private function getClassname($filename) {
91
//            $directoriesAndFilename = explode('/', $filename);
92
//            $filename = array_pop($directoriesAndFilename);
93
//            $nameAndExtension = explode('.', $filename);
94
//            $className = array_shift($nameAndExtension);
95
//
96
//            return $className;
97
//        }
98
}
99