Completed
Pull Request — 4.0 (#55)
by Huberty
03:08
created

TdbmController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 157
Duplicated Lines 14.65 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 13
Bugs 0 Features 2
Metric Value
wmc 14
c 13
b 0
f 2
lcom 1
cbo 7
dl 23
loc 157
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B defaultAction() 17 44 5
A generate() 0 9 1
B generateDaos() 6 59 8

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
namespace Mouf\Database\TDBM\Controllers;
4
5
use Mouf\Composer\ClassNameMapper;
6
use Mouf\Controllers\AbstractMoufInstanceController;
7
use Mouf\Database\TDBM\TDBMService;
8
use Mouf\Database\TDBM\Utils\TDBMDaoGenerator;
9
use Mouf\MoufManager;
10
use Mouf\InstanceProxy;
11
12
/**
13
 * The controller to generate automatically the Beans, Daos, etc...
14
 * Sweet!
15
 *
16
 * @Component
17
 */
18
class TdbmController extends AbstractMoufInstanceController
19
{
20
    /**
21
     * @var HtmlBlock
22
     */
23
    public $content;
24
25
    protected $daoNamespace;
26
    protected $beanNamespace;
27
    protected $daoFactoryName;
28
    protected $daoFactoryInstanceName;
29
    protected $autoloadDetected;
30
    protected $storeInUtc;
31
32
    /**
33
     * Admin page used to display the DAO generation form.
34
     *
35
     * @Action
36
     * //@Admin
37
     */
38
    public function defaultAction($name, $selfedit = 'false')
39
    {
40
        $this->initController($name, $selfedit);
41
42
        // Fill variables
43
        if ($this->moufManager->getVariable('tdbmDefaultSourceDirectory_'.$name) != null) {
44
            $this->daoNamespace = $this->moufManager->getVariable('tdbmDefaultDaoNamespace_'.$name);
45
            $this->beanNamespace = $this->moufManager->getVariable('tdbmDefaultBeanNamespace_'.$name);
46
            $this->daoFactoryName = $this->moufManager->getVariable('tdbmDefaultDaoFactoryName_'.$name);
47
            $this->daoFactoryInstanceName = $this->moufManager->getVariable('tdbmDefaultDaoFactoryInstanceName_'.$name);
48
            $this->storeInUtc = $this->moufManager->getVariable('tdbmDefaultStoreInUtc_'.$name);
49
            $this->useCustomComposer = $this->moufManager->getVariable('tdbmDefaultUseCustomComposer_'.$name);
50
            $this->composerFile = $this->moufManager->getVariable('tdbmDefaultComposerFile_'.$name);
51
        } else {
52
            $this->daoNamespace = $this->moufManager->getVariable('tdbmDefaultDaoNamespace');
53
            $this->beanNamespace = $this->moufManager->getVariable('tdbmDefaultBeanNamespace');
54
            $this->daoFactoryName = $this->moufManager->getVariable('tdbmDefaultDaoFactoryName');
55
            $this->daoFactoryInstanceName = $this->moufManager->getVariable('tdbmDefaultDaoFactoryInstanceName');
56
            $this->storeInUtc = $this->moufManager->getVariable('tdbmDefaultStoreInUtc');
57
            $this->useCustomComposer = $this->moufManager->getVariable('tdbmDefaultUseCustomComposer');
58
            $this->composerFile = $this->moufManager->getVariable('tdbmDefaultComposerFile');
59
        }
60
61 View Code Duplication
        if ($this->daoNamespace == null && $this->beanNamespace == null) {
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...
62
            $classNameMapper = ClassNameMapper::createFromComposerFile(__DIR__.'/../../../../../../../../composer.json');
63
64
            $autoloadNamespaces = $classNameMapper->getManagedNamespaces();
65
            if ($autoloadNamespaces) {
66
                $this->autoloadDetected = true;
67
                $rootNamespace = $autoloadNamespaces[0];
68
                $this->daoNamespace = $rootNamespace.'Dao';
69
                $this->beanNamespace = $rootNamespace.'Dao\\Bean';
70
            } else {
71
                $this->autoloadDetected = false;
72
                $this->daoNamespace = 'YourApplication\\Dao';
73
                $this->beanNamespace = 'YourApplication\\Dao\\Bean';
74
            }
75
        } else {
76
            $this->autoloadDetected = true;
77
        }
78
79
        $this->content->addFile(dirname(__FILE__).'/../../../../views/tdbmGenerate.php', $this);
80
        $this->template->toHtml();
81
    }
82
83
    /**
84
     * This action generates the DAOs and Beans for the TDBM service passed in parameter.
85
     *
86
     * @Action
87
     *
88
     * @param string $name
89
     * @param bool   $selfedit
90
     */
91
    public function generate($name, $daonamespace, $beannamespace, $daofactoryclassname, $daofactoryinstancename, $storeInUtc = 0, $selfedit = 'false', $useCustomComposer = false, $composerFile = '')
92
    {
93
        $this->initController($name, $selfedit);
94
95
        self::generateDaos($this->moufManager, $name, $daonamespace, $beannamespace, $daofactoryclassname, $daofactoryinstancename, $selfedit, $storeInUtc, $useCustomComposer, $composerFile);
96
97
        // TODO: better: we should redirect to a screen that list the number of DAOs generated, etc...
98
        header('Location: '.ROOT_URL.'ajaxinstance/?name='.urlencode($name).'&selfedit='.$selfedit);
99
    }
100
101
    /**
102
     * This function generates the DAOs and Beans for the TDBM service passed in parameter.
103
     *
104
     * @param MoufManager $moufManager
105
     * @param string      $name
106
     * @param string      $daonamespace
107
     * @param string      $beannamespace
108
     * @param string      $daofactoryclassname
109
     * @param string      $daofactoryinstancename
110
     * @param string      $selfedit
111
     * @param bool        $storeInUtc
112
     *
113
     * @throws \Mouf\MoufException
114
     */
115
    public static function generateDaos(MoufManager $moufManager, $name, $daonamespace, $beannamespace, $daofactoryclassname, $daofactoryinstancename, $selfedit = 'false', $storeInUtc = null, $useCustomComposer = null, $composerFile = null)
116
    {
117
        $moufManager->setVariable('tdbmDefaultDaoNamespace_'.$name, $daonamespace);
118
        $moufManager->setVariable('tdbmDefaultBeanNamespace_'.$name, $beannamespace);
119
        $moufManager->setVariable('tdbmDefaultDaoFactoryName_'.$name, $daofactoryclassname);
120
        $moufManager->setVariable('tdbmDefaultDaoFactoryInstanceName_'.$name, $daofactoryinstancename);
121
        $moufManager->setVariable('tdbmDefaultStoreInUtc_'.$name, $storeInUtc);
122
        $moufManager->setVariable('tdbmDefaultUseCustomComposer_'.$name, $useCustomComposer);
123
        $moufManager->setVariable('tdbmDefaultComposerFile_'.$name, $composerFile);
124
125
        // In case of instance renaming, let's use the last used settings
126
        $moufManager->setVariable('tdbmDefaultDaoNamespace', $daonamespace);
127
        $moufManager->setVariable('tdbmDefaultBeanNamespace', $beannamespace);
128
        $moufManager->setVariable('tdbmDefaultDaoFactoryName', $daofactoryclassname);
129
        $moufManager->setVariable('tdbmDefaultDaoFactoryInstanceName', $daofactoryinstancename);
130
        $moufManager->setVariable('tdbmDefaultStoreInUtc', $storeInUtc);
131
        $moufManager->setVariable('tdbmDefaultUseCustomComposer', $useCustomComposer);
132
        $moufManager->setVariable('tdbmDefaultComposerFile', $composerFile);
133
134
        // Remove first and last slash in namespace.
135
        if (strpos($daonamespace, '\\') === 0) {
136
            $daonamespace = substr($daonamespace, 1);
137
        }
138 View Code Duplication
        if (strpos($daonamespace, '\\') === strlen($daonamespace) - 1) {
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...
139
            $daonamespace = substr($daonamespace, 0, strlen($daonamespace) - 1);
140
        }
141
        if (strpos($beannamespace, '\\') === 0) {
142
            $beannamespace = substr($beannamespace, 1);
143
        }
144 View Code Duplication
        if (strpos($beannamespace, '\\') === strlen($beannamespace) - 1) {
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...
145
            $beannamespace = substr($beannamespace, 0, strlen($beannamespace) - 1);
146
        }
147
148
        $tdbmService = new InstanceProxy($name);
149
        /* @var $tdbmService TDBMService */
150
        $tables = $tdbmService->generateAllDaosAndBeans($daofactoryclassname, $daonamespace, $beannamespace, $storeInUtc, ($useCustomComposer ? $composerFile : null));
0 ignored issues
show
Bug introduced by
It seems like $storeInUtc defined by parameter $storeInUtc on line 115 can also be of type null; however, Mouf\Database\TDBM\TDBMS...nerateAllDaosAndBeans() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
151
152
        $moufManager->declareComponent($daofactoryinstancename, $daonamespace.'\\'.$daofactoryclassname, false, MoufManager::DECLARE_ON_EXIST_KEEP_INCOMING_LINKS);
153
154
        $tableToBeanMap = [];
155
156
        //$tdbmServiceDescriptor = $moufManager->getInstanceDescriptor('tdbmService');
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
157
158
        foreach ($tables as $table) {
159
            $daoName = TDBMDaoGenerator::getDaoNameFromTableName($table);
160
161
            $instanceName = TDBMDaoGenerator::toVariableName($daoName);
162
            if (!$moufManager->instanceExists($instanceName)) {
163
                $moufManager->declareComponent($instanceName, $daonamespace.'\\'.$daoName);
164
            }
165
            $moufManager->setParameterViaConstructor($instanceName, 0, $name, 'object');
166
            $moufManager->bindComponentViaSetter($daofactoryinstancename, 'set'.$daoName, $instanceName);
167
168
            $tableToBeanMap[$table] = $beannamespace.'\\'.TDBMDaoGenerator::getBeanNameFromTableName($table);
169
        }
170
        $tdbmServiceDescriptor = $moufManager->getInstanceDescriptor($name);
171
        $tdbmServiceDescriptor->getSetterProperty('setTableToBeanMap')->setValue($tableToBeanMap);
172
        $moufManager->rewriteMouf();
173
    }
174
}
175