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 |
||
24 | class MonologServiceFactory implements FactoryInterface |
||
|
|||
25 | { |
||
26 | |||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | * @throws ContainerException |
||
30 | * @throws RuntimeException |
||
31 | * @throws NotFoundException |
||
32 | * @throws ReflectionException |
||
33 | */ |
||
34 | 2 | public function createService(ServiceLocatorInterface $serviceLocator) |
|
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | * @throws NotFoundException |
||
44 | * @throws RuntimeException |
||
45 | * @throws ContainerException |
||
46 | * @throws ReflectionException |
||
47 | */ |
||
48 | 1 | public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
|
54 | |||
55 | /** |
||
56 | * @param ServiceLocatorInterface|ContainerInterface $container |
||
57 | * @param MonologOptions $options |
||
58 | * @return Logger |
||
59 | * @throws NotFoundException |
||
60 | * @throws RuntimeException |
||
61 | * @throws ContainerException |
||
62 | * @throws ReflectionException |
||
63 | */ |
||
64 | 4 | public function createLogger($container, MonologOptions $options) |
|
79 | |||
80 | /** |
||
81 | * @param ServiceLocatorInterface|ContainerInterface $container |
||
82 | * @param MonologOptions $options |
||
83 | * @param string|array $handler |
||
84 | * @return HandlerInterface |
||
85 | * @throws RuntimeException |
||
86 | * @throws NotFoundException |
||
87 | * @throws ContainerException |
||
88 | * @throws ReflectionException |
||
89 | * |
||
90 | */ |
||
91 | 15 | public function createHandler($container, MonologOptions $options, $handler) |
|
92 | { |
||
93 | 15 | if (is_string($handler) && $container->has($handler)) { |
|
94 | 2 | return $container->get($handler); |
|
95 | } |
||
96 | |||
97 | 14 | if (!isset($handler['name'])) { |
|
98 | 1 | throw new RuntimeException('Cannot create logger handler'); |
|
99 | } |
||
100 | |||
101 | 13 | $handlerClassName = $handler['name']; |
|
102 | |||
103 | 13 | if (!class_exists($handlerClassName)) { |
|
104 | 1 | throw new RuntimeException('Cannot create logger handler (' . $handlerClassName . ')'); |
|
105 | } |
||
106 | |||
107 | 12 | $arguments = array_key_exists('args', $handler) ? $handler['args'] : array(); |
|
108 | |||
109 | 12 | if (!is_array($arguments)) { |
|
110 | 1 | throw new RuntimeException('Arguments of handler(' . $handlerClassName . ') must be array'); |
|
111 | } |
||
112 | |||
113 | 11 | if (isset($arguments['handler'])) { |
|
114 | 1 | foreach ($options->getHandlers() as $key => $option) { |
|
115 | 1 | if ($arguments['handler'] == $key) { |
|
116 | 1 | $arguments['handler'] = $this->createHandler($container, $options, $option); |
|
117 | 1 | break; |
|
118 | } |
||
119 | } |
||
120 | } |
||
121 | |||
122 | try { |
||
123 | /** @var HandlerInterface $instance */ |
||
124 | 11 | $instance = $this->createInstanceFromArguments($handlerClassName, $arguments); |
|
125 | 1 | } catch (InvalidArgumentException $exception) { |
|
126 | 1 | throw new RuntimeException(sprintf( |
|
127 | 1 | 'Handler(%s) has an invalid argument configuration', |
|
128 | $handlerClassName |
||
129 | 1 | ), 0, $exception); |
|
130 | } |
||
131 | |||
132 | 10 | if (isset($handler['formatter'])) { |
|
133 | 1 | if ($instance instanceof FormattableHandlerInterface |
|
134 | 1 | || \Monolog\Logger::API === 1) { |
|
135 | 1 | $formatter = $this->createFormatter($container, $handler['formatter']); |
|
136 | 1 | $instance->setFormatter($formatter); |
|
137 | } else { |
||
138 | throw new RuntimeException(sprintf( |
||
139 | 'Handler(%s) doesn\'t implements %s to attach formatter.', |
||
140 | $handlerClassName, |
||
141 | FormattableHandlerInterface::class |
||
142 | )); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | 10 | return $instance; |
|
147 | } |
||
148 | |||
149 | /** |
||
150 | * @param ContainerInterface $container |
||
151 | * @param string|array $formatter |
||
152 | * @return FormatterInterface |
||
153 | * @throws NotFoundException |
||
154 | * @throws ContainerException |
||
155 | * @throws RuntimeException |
||
156 | * @throws ReflectionException |
||
157 | */ |
||
158 | 12 | public function createFormatter($container, $formatter) |
|
192 | |||
193 | /** |
||
194 | * @param ContainerInterface $container |
||
195 | * @param $processor |
||
196 | * @return Closure |
||
197 | * @throws NotFoundException |
||
198 | * @throws ContainerException |
||
199 | * @throws RuntimeException |
||
200 | * @throws ReflectionException |
||
201 | */ |
||
202 | 13 | public function createProcessor($container, $processor) |
|
261 | |||
262 | /** |
||
263 | * Handles the constructor arguments and if they're named, just sort them to fit constructor ordering. |
||
264 | * |
||
265 | * @param string $className |
||
266 | * @param array $arguments |
||
267 | * |
||
268 | * @return object |
||
269 | * @throws InvalidArgumentException If given arguments are not valid for provided className constructor. |
||
270 | * @throws ReflectionException |
||
271 | */ |
||
272 | 22 | private function createInstanceFromArguments($className, array $arguments) |
|
329 | } |
||
330 |
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.