Completed
Pull Request — 4.2 (#140)
by David
26:56
created

MoufDiListener   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 8
dl 0
loc 55
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B onGenerate() 0 26 4
A getTdbmInstanceName() 0 20 4
1
<?php
2
3
4
namespace Mouf\Database\TDBM\Utils;
5
6
use Mouf\Database\TDBM\ConfigurationInterface;
7
use Mouf\Database\TDBM\MoufConfiguration;
8
use Mouf\Database\TDBM\TDBMService;
9
use Mouf\MoufManager;
10
11
class MoufDiListener implements GeneratorListenerInterface
12
{
13
14
    /**
15
     * @param ConfigurationInterface $configuration
16
     * @param BeanDescriptorInterface[] $beanDescriptors
17
     */
18
    public function onGenerate(ConfigurationInterface $configuration, array $beanDescriptors): void
19
    {
20
        // Let's generate the needed instance in Mouf.
21
        $moufManager = MoufManager::getMoufManager();
22
23
        if ($configuration instanceof MoufConfiguration) {
24
            $daoFactoryInstanceName = $configuration->getDaoFactoryInstanceName();
25
            $daoFactoryClassName = $configuration->getDaoNamespace().'\\Generated\\'.$configuration->getNamingStrategy()->getDaoFactoryClassName();
26
            $moufManager->declareComponent($daoFactoryInstanceName, $daoFactoryClassName, false, MoufManager::DECLARE_ON_EXIST_KEEP_INCOMING_LINKS);
27
        }
28
29
        $tdbmServiceInstanceName = $this->getTdbmInstanceName($configuration);
30
31
        foreach ($beanDescriptors as $beanDescriptor) {
32
            $daoName = $beanDescriptor->getDaoClassName();
33
34
            $instanceName = TDBMDaoGenerator::toVariableName($daoName);
35
            if (!$moufManager->instanceExists($instanceName)) {
36
                $moufManager->declareComponent($instanceName, $configuration->getDaoNamespace().'\\'.$daoName);
37
            }
38
            $moufManager->setParameterViaConstructor($instanceName, 0, $tdbmServiceInstanceName, 'object');
39
            $moufManager->bindComponentViaSetter($daoFactoryInstanceName, 'set'.$daoName, $instanceName);
0 ignored issues
show
Bug introduced by
The variable $daoFactoryInstanceName does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
40
        }
41
42
        $moufManager->rewriteMouf();
43
    }
44
45
    private function getTdbmInstanceName(ConfigurationInterface $configuration) : string
46
    {
47
        $moufManager = MoufManager::getMoufManager();
48
49
        $configurationInstanceName = $moufManager->findInstanceName($configuration);
50
        if (!$configurationInstanceName) {
51
            throw new \TDBMException('Could not find TDBM instance for configuration object.');
52
        }
53
54
        // Let's find the configuration
55
        $tdbmServicesNames = $moufManager->findInstances(TDBMService::class);
56
57
        foreach ($tdbmServicesNames as $name) {
58
            if ($moufManager->getInstanceDescriptor($name)->getConstructorArgumentProperty('configuration')->getValue()->getName() === $configurationInstanceName) {
59
                return $name;
60
            }
61
        }
62
63
        throw new \TDBMException('Could not find TDBMService instance.');
64
    }
65
}
66