Conditions | 1 |
Total Lines | 85 |
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); |
||
72 | private function getContainerForNamespace(string $namespace): ContainerBuilder |
||
73 | { |
||
74 | if (isset($this->generatedContainerClass[$namespace])) { |
||
75 | return $this->generatedContainerClass[$namespace]; |
||
76 | } |
||
77 | |||
78 | $containerBuilder = new ContainerBuilder(); |
||
79 | $pathToFiles = $this->copiedWorkDir . '/src/'; |
||
80 | $fileLocator = new FileLocator($pathToFiles); |
||
81 | |||
82 | $loader = new class($containerBuilder, $fileLocator, $namespace, $pathToFiles) extends FileLoader |
||
83 | { |
||
84 | /** |
||
85 | * @var string |
||
86 | */ |
||
87 | private $namespace; |
||
88 | /** |
||
89 | * @var string |
||
90 | */ |
||
91 | private $pathToFiles; |
||
92 | |||
93 | public function __construct( |
||
94 | ContainerBuilder $container, |
||
95 | FileLocatorInterface $locator, |
||
96 | string $namespace, |
||
97 | string $pathToFiles |
||
98 | ) { |
||
99 | parent::__construct($container, $locator); |
||
100 | $this->namespace = $namespace . '\\'; |
||
101 | $this->pathToFiles = str_replace('//', '/', $pathToFiles . '/*'); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Loads a resource. |
||
106 | * |
||
107 | * @param mixed $resource The resource |
||
108 | * @param string|null $type The resource type or null if unknown |
||
109 | * |
||
110 | * @throws \Exception If something went wrong |
||
111 | * @SupressWarnings(PHPMD.Superglobals) |
||
112 | */ |
||
113 | public function load($resource, $type = null): void |
||
114 | { |
||
115 | $definition = new Definition(); |
||
116 | |||
117 | $definition->setAutowired(true)->setAutoconfigured(true)->setPublic(true); |
||
118 | $this->registerClasses($definition, $this->namespace, $this->pathToFiles); |
||
119 | $dsmContainer = new Container(); |
||
120 | $dsmContainer->addConfiguration($this->container, $this->buildServerConfig()); |
||
121 | $this->container->compile(); |
||
122 | } |
||
123 | |||
124 | private function buildServerConfig() |
||
125 | { |
||
126 | SimpleEnv::setEnv(Config::getProjectRootDirectory() . '/.env'); |
||
127 | $testConfig = $_SERVER; |
||
128 | $testConfig[ConfigInterface::PARAM_DB_NAME] .= '_test'; |
||
129 | $testConfig[ConfigInterface::PARAM_DEVMODE] = true; |
||
130 | return $testConfig; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Returns whether this class supports the given resource. |
||
135 | * |
||
136 | * @param mixed $resource A resource |
||
137 | * @param string|null $type The resource type or null if unknown |
||
138 | * |
||
139 | * @return bool True if this class supports the given resource, false otherwise |
||
140 | */ |
||
141 | public function supports($resource, $type = null): bool |
||
142 | { |
||
143 | return true; |
||
144 | } |
||
145 | |||
146 | public function getContainer(): ContainerBuilder |
||
147 | { |
||
148 | return $this->container; |
||
149 | } |
||
150 | }; |
||
151 | |||
152 | $loader->load('required by the interface'); |
||
153 | |||
154 | $this->generatedContainerClass[$namespace] = $loader->getContainer(); |
||
155 | |||
156 | return $this->generatedContainerClass[$namespace]; |
||
157 | } |
||
159 |