Complex classes like MonologServiceFactory often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MonologServiceFactory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class MonologServiceFactory implements FactoryInterface |
||
|
|||
19 | { |
||
20 | |||
21 | /** |
||
22 | * {@inheritdoc} |
||
23 | * @throws \Interop\Container\Exception\ContainerException |
||
24 | * @throws \RuntimeException |
||
25 | * @throws \Interop\Container\Exception\NotFoundException |
||
26 | */ |
||
27 | 2 | public function createService(ServiceLocatorInterface $serviceLocator) |
|
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | * @throws \Interop\Container\Exception\NotFoundException |
||
37 | * @throws \RuntimeException |
||
38 | */ |
||
39 | 1 | public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
|
40 | { |
||
41 | /** @var MonologOptions $options */ |
||
42 | 1 | $options = $container->get('EnliteMonologOptions'); |
|
43 | 1 | return $this->createLogger($container, $options); |
|
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param ServiceLocatorInterface|ContainerInterface $container |
||
48 | * @param MonologOptions $options |
||
49 | * @return Logger |
||
50 | * @throws \Interop\Container\Exception\NotFoundException |
||
51 | * @throws \RuntimeException |
||
52 | * @throws \Interop\Container\Exception\ContainerException |
||
53 | */ |
||
54 | 4 | public function createLogger($container, MonologOptions $options) |
|
69 | |||
70 | /** |
||
71 | * @param ServiceLocatorInterface|ContainerInterface $container |
||
72 | * @param MonologOptions $options |
||
73 | * @param string|array $handler |
||
74 | * @throws \RuntimeException |
||
75 | * @return HandlerInterface |
||
76 | * @throws \Interop\Container\Exception\NotFoundException |
||
77 | * @throws \Interop\Container\Exception\ContainerException |
||
78 | * |
||
79 | */ |
||
80 | 15 | public function createHandler($container, MonologOptions $options, $handler) |
|
81 | { |
||
82 | 15 | if (is_string($handler) && $container->has($handler)) { |
|
83 | 2 | return $container->get($handler); |
|
84 | } |
||
85 | |||
86 | |||
87 | 14 | if (!isset($handler['name'])) { |
|
88 | 1 | throw new RuntimeException('Cannot create logger handler'); |
|
89 | } |
||
90 | |||
91 | 13 | $handlerClassName = $handler['name']; |
|
92 | |||
93 | 13 | if (!class_exists($handlerClassName)) { |
|
94 | 1 | throw new RuntimeException('Cannot create logger handler (' . $handlerClassName . ')'); |
|
95 | } |
||
96 | |||
97 | 12 | $arguments = array_key_exists('args', $handler) ? $handler['args'] : array(); |
|
98 | |||
99 | 12 | if (!is_array($arguments)) { |
|
100 | 1 | throw new RuntimeException('Arguments of handler(' . $handlerClassName . ') must be array'); |
|
101 | } |
||
102 | |||
103 | 11 | if (isset($arguments['handler'])) { |
|
104 | 1 | foreach ($options->getHandlers() as $key => $option) { |
|
105 | 1 | if ($arguments['handler'] == $key) { |
|
106 | 1 | $arguments['handler'] = $this->createHandler($container, $options, $option); |
|
107 | 1 | break; |
|
108 | } |
||
109 | } |
||
110 | } |
||
111 | |||
112 | try { |
||
113 | /** @var HandlerInterface $instance */ |
||
114 | 11 | $instance = $this->createInstanceFromArguments($handlerClassName, $arguments); |
|
115 | 1 | } catch (\InvalidArgumentException $exception) { |
|
116 | 1 | throw new RuntimeException(sprintf( |
|
117 | 1 | 'Handler(%s) has an invalid argument configuration', |
|
118 | $handlerClassName |
||
119 | 1 | ), 0, $exception); |
|
120 | } |
||
121 | |||
122 | 10 | if (isset($handler['formatter'])) { |
|
123 | 1 | $formatter = $this->createFormatter($container, $handler['formatter']); |
|
124 | 1 | $instance->setFormatter($formatter); |
|
125 | } |
||
126 | |||
127 | 10 | return $instance; |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * @param ServiceLocatorInterface|ContainerInterface $container |
||
132 | * @param string|array $formatter |
||
133 | * @return FormatterInterface |
||
134 | * @throws \Interop\Container\Exception\NotFoundException |
||
135 | * @throws \Interop\Container\Exception\ContainerException |
||
136 | * @throws RuntimeException |
||
137 | */ |
||
138 | 12 | public function createFormatter($container, $formatter) |
|
139 | { |
||
140 | 12 | if (is_string($formatter) && $container->has($formatter)) { |
|
141 | 1 | return $container->get($formatter); |
|
142 | } |
||
143 | |||
144 | 11 | if (!isset($formatter['name'])) { |
|
145 | 1 | throw new RuntimeException('Cannot create logger formatter'); |
|
146 | } |
||
147 | |||
148 | 10 | $formatterClassName = $formatter['name']; |
|
149 | |||
150 | 10 | if (!class_exists($formatter['name'])) { |
|
151 | 1 | throw new RuntimeException('Cannot create logger formatter (' . $formatterClassName . ')'); |
|
152 | } |
||
153 | |||
154 | 9 | $arguments = array_key_exists('args', $formatter) ? $formatter['args'] : array(); |
|
155 | |||
156 | 9 | if (!is_array($arguments)) { |
|
157 | 1 | throw new RuntimeException('Arguments of formatter(' . $formatterClassName . ') must be array'); |
|
158 | } |
||
159 | |||
160 | try { |
||
161 | /** @var FormatterInterface $instance */ |
||
162 | 8 | $instance = $this->createInstanceFromArguments($formatterClassName, $arguments); |
|
163 | 3 | } catch (\InvalidArgumentException $exception) { |
|
164 | 3 | throw new RuntimeException(sprintf( |
|
165 | 3 | 'Formatter(%s) has an invalid argument configuration', |
|
166 | $formatterClassName |
||
167 | 3 | ), 0, $exception); |
|
168 | } |
||
169 | |||
170 | 5 | return $instance; |
|
171 | } |
||
172 | |||
173 | /** |
||
174 | * @param ServiceLocatorInterface|ContainerInterface $container |
||
175 | * @param $processor |
||
176 | * @return Closure |
||
177 | * @throws \Interop\Container\Exception\NotFoundException |
||
178 | * @throws \Interop\Container\Exception\ContainerException |
||
179 | * @throws RuntimeException |
||
180 | */ |
||
181 | 5 | public function createProcessor($container, $processor) |
|
207 | |||
208 | /** |
||
209 | * Handles the constructor arguments and if they're named, just sort them to fit constructor ordering. |
||
210 | * |
||
211 | * @param string $className |
||
212 | * @param array $arguments |
||
213 | * |
||
214 | * @return object |
||
215 | * @throws \InvalidArgumentException If given arguments are not valid for provided className constructor. |
||
216 | */ |
||
217 | 18 | private function createInstanceFromArguments($className, array $arguments) |
|
274 | } |
||
275 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.