FindParametersService::findParameters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Mouf\Database\QueryWriter\Utils;
4
5
use Mouf\MoufInstanceDescriptor;
6
7
/**
8
 * This class is used to find conditions recursively inside a MoufInstanceDescriptor.
9
 */
10
class FindParametersService
11
{
12
    /**
13
     * Tries to find parameters recursively in the instances and retrieves the list of parameters found.
14
     *
15
     * @param MoufInstanceDescriptor $instanceDescriptor
16
     *
17
     * @return string[]
18
     */
19
    public static function findParameters(MoufInstanceDescriptor $instanceDescriptor)
20
    {
21
        return array_keys(self::recursiveFindParameters($instanceDescriptor)[1]);
22
    }
23
24
    /**
25
     * @param MoufInstanceDescriptor $instanceDescriptor
26
     * @param array<string>          $visitedInstances   The list of names of visited instances.
27
     */
28
    private static function recursiveFindParameters(MoufInstanceDescriptor $instanceDescriptor, array $visitedInstances = array(), array $foundParameters = array())
29
    {
30
        if (isset($visitedInstances[$instanceDescriptor->getIdentifierName()])) {
31
            return array($visitedInstances, $foundParameters);
32
        }
33
        $visitedInstances[$instanceDescriptor->getIdentifierName()] = true;
34
35
        if ($instanceDescriptor->getClassDescriptor()->getName() == 'SQLParser\\Node\\Parameter') {
36
            $foundParameters[$instanceDescriptor->getProperty('name')->getValue()] = true;
37
38
            return array($visitedInstances, $foundParameters);
39
        }
40
        if ($instanceDescriptor->getClassDescriptor()->getName() == 'Mouf\\Database\\QueryWriter\\Condition\\ParamAvailableCondition'
41
                || $instanceDescriptor->getClassDescriptor()->getName() == 'Mouf\\Database\\QueryWriter\\Condition\\ParamEqualsCondition'
42
                || $instanceDescriptor->getClassDescriptor()->getName() == 'Mouf\\Database\\QueryWriter\\Condition\\ParamNotAvailableCondition') {
43
            $foundParameters[$instanceDescriptor->getProperty('parameterName')->getValue()] = true;
44
45
            return array($visitedInstances, $foundParameters);
46
        }
47
48
        $classDescriptor = $instanceDescriptor->getClassDescriptor();
49
        /* @var $classDescriptor MoufReflectionClass */
50 View Code Duplication
        foreach ($classDescriptor->getInjectablePropertiesByConstructor() as $moufPropertyDescriptor) {
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...
51
            /* @var $moufPropertyDescriptor MoufPropertyDescriptor */
52
            $name = $moufPropertyDescriptor->getName();
53
54
            $value = $instanceDescriptor->getConstructorArgumentProperty($name)->getValue();
55
            if ($value instanceof MoufInstanceDescriptor) {
0 ignored issues
show
Bug introduced by
The class Mouf\MoufInstanceDescriptor does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
56
                list($visitedInstances, $foundParameters) = self::recursiveFindParameters($value, $visitedInstances, $foundParameters);
57
            } elseif (is_array($value)) {
58
                foreach ($value as $val) {
59
                    if ($val instanceof MoufInstanceDescriptor) {
0 ignored issues
show
Bug introduced by
The class Mouf\MoufInstanceDescriptor does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
60
                        list($visitedInstances, $foundParameters) = self::recursiveFindParameters($val, $visitedInstances, $foundParameters);
61
                    }
62
                }
63
            }
64
        }
65
66 View Code Duplication
        foreach ($classDescriptor->getInjectablePropertiesBySetter() as $moufPropertyDescriptor) {
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...
67
            /* @var $moufPropertyDescriptor MoufPropertyDescriptor */
68
            $name = $moufPropertyDescriptor->getName();
69
70
            $value = $instanceDescriptor->getSetterProperty($name)->getValue();
71
            if ($value instanceof MoufInstanceDescriptor) {
0 ignored issues
show
Bug introduced by
The class Mouf\MoufInstanceDescriptor does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
72
                list($visitedInstances, $foundParameters) = self::recursiveFindParameters($value, $visitedInstances, $foundParameters);
73
            } elseif (is_array($value)) {
74
                foreach ($value as $val) {
75
                    if ($val instanceof MoufInstanceDescriptor) {
0 ignored issues
show
Bug introduced by
The class Mouf\MoufInstanceDescriptor does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
76
                        list($visitedInstances, $foundParameters) = self::recursiveFindParameters($val, $visitedInstances, $foundParameters);
77
                    }
78
                }
79
            }
80
        }
81
82 View Code Duplication
        foreach ($classDescriptor->getInjectablePropertiesByPublicProperty() as $moufPropertyDescriptor) {
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...
83
            /* @var $moufPropertyDescriptor MoufPropertyDescriptor */
84
            $name = $moufPropertyDescriptor->getName();
85
86
            $value = $instanceDescriptor->getInjectablePropertiesByPublicProperty($name)->getValue();
87
            if ($value instanceof MoufInstanceDescriptor) {
0 ignored issues
show
Bug introduced by
The class Mouf\MoufInstanceDescriptor does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
88
                list($visitedInstances, $foundParameters) = self::recursiveFindParameters($value, $visitedInstances, $foundParameters);
89
            } elseif (is_array($value)) {
90
                foreach ($value as $val) {
91
                    if ($val instanceof MoufInstanceDescriptor) {
0 ignored issues
show
Bug introduced by
The class Mouf\MoufInstanceDescriptor does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
92
                        list($visitedInstances, $foundParameters) = self::recursiveFindParameters($val, $visitedInstances, $foundParameters);
93
                    }
94
                }
95
            }
96
        }
97
98
        return array($visitedInstances, $foundParameters);
99
    }
100
}
101