| Conditions | 13 |
| Paths | 5 |
| Total Lines | 112 |
| Code Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php declare(strict_types=1); |
||
| 76 | public function init() |
||
| 77 | { |
||
| 78 | $containerPath = __DIR__ . '/../../../../../www/cache'; |
||
| 79 | $containerName = 'ContainerCore'; |
||
| 80 | $containerNamespace = 'samsonphp\core\loader'; |
||
| 81 | /** @var Module $module */ |
||
| 82 | $modules = $this->moduleManager->getRegisteredModules(); |
||
| 83 | |||
| 84 | $localModulesPath = '../src'; |
||
| 85 | ResourceMap::get('cache'); |
||
| 86 | $resourceMap = ResourceMap::get($localModulesPath); |
||
| 87 | $localModules = $resourceMap->modules; |
||
| 88 | |||
| 89 | if (false || !file_exists($containerPath . '/' . $containerName . '.php')) { |
||
|
|
|||
| 90 | |||
| 91 | $builder = new DefinitionBuilder(new ParameterBuilder()); |
||
| 92 | $xmlResolver = new XmlResolver(); |
||
| 93 | $xmlResolver->resolveFile($builder, __DIR__ . '/../../../../../app/config/config.xml'); |
||
| 94 | |||
| 95 | new Service(''); |
||
| 96 | new InjectService(''); |
||
| 97 | new InjectClass(''); |
||
| 98 | new InjectParameter(''); |
||
| 99 | |||
| 100 | foreach ($modules as $module) { |
||
| 101 | if ($module->className && !$builder->hasDefinition($module->className)) { |
||
| 102 | // Fix samson.php files |
||
| 103 | if (!class_exists($module->className)) { |
||
| 104 | require_once($module->pathName); |
||
| 105 | } |
||
| 106 | /** @var ClassDefinition $classDefinition */ |
||
| 107 | $classDefinition = $builder->addDefinition($module->className); |
||
| 108 | if ($id = $this->getModuleId($module->pathName)) { |
||
| 109 | $classDefinition->setServiceName($id); |
||
| 110 | } else { |
||
| 111 | // Generate identifier from module class |
||
| 112 | $classDefinition->setServiceName( |
||
| 113 | strtolower(ltrim(str_replace(__NS_SEPARATOR__, '_', $module->className), '_')) |
||
| 114 | ); |
||
| 115 | } |
||
| 116 | $classDefinition->addScope(new ModuleScope())->setIsSingleton(true); |
||
| 117 | $this->defineConstructor($classDefinition, $module->path); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | $classDefinition = $builder->addDefinition(VirtualModule::class); |
||
| 122 | $classDefinition->addScope(new ModuleScope())->setServiceName('local')->setIsSingleton(true); |
||
| 123 | $this->defineConstructor($classDefinition, getcwd()); |
||
| 124 | |||
| 125 | foreach ($localModules as $moduleFile) { |
||
| 126 | if (!$builder->hasDefinition($moduleFile[0])) { |
||
| 127 | |||
| 128 | /** @var ClassDefinition $classDefinition */ |
||
| 129 | $classDefinition = $builder->addDefinition($moduleFile[0]); |
||
| 130 | $classDefinition->addScope(new ModuleScope()); |
||
| 131 | $classDefinition->setIsSingleton(true); |
||
| 132 | if ($id = $this->getModuleId($moduleFile[1])) { |
||
| 133 | $classDefinition->setServiceName($id); |
||
| 134 | } else { |
||
| 135 | throw new \Exception('Can not get id of local module'); |
||
| 136 | } |
||
| 137 | |||
| 138 | $modulePath = explode('/', str_replace(realpath($localModulesPath), '', $moduleFile[1])); |
||
| 139 | $this->defineConstructor($classDefinition, $localModulesPath . '/' . $modulePath[1]); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * Add implementors |
||
| 146 | */ |
||
| 147 | foreach ($this->moduleManager->implements as $interfaceName => $class) { |
||
| 148 | $builder->defineImplementors($interfaceName, new ClassReference($class)); |
||
| 149 | } |
||
| 150 | |||
| 151 | // Init compiler |
||
| 152 | $reader = new AnnotationReader(); |
||
| 153 | $compiler = new DefinitionCompiler( |
||
| 154 | new DefinitionGenerator(new ClassGenerator()), |
||
| 155 | (new DefinitionAnalyzer()) |
||
| 156 | ->addClassAnalyzer(new AnnotationClassAnalyzer($reader)) |
||
| 157 | ->addClassAnalyzer(new ReflectionClassAnalyzer()) |
||
| 158 | ->addMethodAnalyzer(new AnnotationMethodAnalyzer($reader)) |
||
| 159 | ->addMethodAnalyzer(new ReflectionMethodAnalyzer()) |
||
| 160 | ->addPropertyAnalyzer(new AnnotationPropertyAnalyzer($reader)) |
||
| 161 | ->addPropertyAnalyzer(new ReflectionPropertyAnalyzer()) |
||
| 162 | ->addParameterAnalyzer(new ReflectionParameterAnalyzer()) |
||
| 163 | ); |
||
| 164 | |||
| 165 | $container = $compiler->compile($builder, $containerName, $containerNamespace, $containerPath); |
||
| 166 | |||
| 167 | } else { |
||
| 168 | |||
| 169 | $containerClassName = $containerNamespace. '\\' . $containerName; |
||
| 170 | require_once($containerPath . '/' . $containerName . '.php'); |
||
| 171 | $container = new $containerClassName(); |
||
| 172 | } |
||
| 173 | |||
| 174 | $GLOBALS['__core'] = $container->get('core'); |
||
| 175 | |||
| 176 | $this->prepareModules($modules, $container); |
||
| 177 | |||
| 178 | /** @var array $module */ |
||
| 179 | foreach ($localModules as $module) { |
||
| 180 | $instance = $container->get($module[0]); |
||
| 181 | $instance->parent = $this->getClassParentModule($container, get_parent_class($instance)); |
||
| 182 | } |
||
| 183 | |||
| 184 | // $container->get('core')->active($container->get('local')); |
||
| 185 | |||
| 186 | return $container; |
||
| 187 | } |
||
| 188 | |||
| 304 |