Complex classes like ElasticEntityManager 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 ElasticEntityManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class ElasticEntityManager implements EntityManagerInterface { |
||
29 | |||
30 | /** @var ElasticRepositoryManager */ |
||
31 | protected $repositoryManager; |
||
32 | |||
33 | /** @var Configuration */ |
||
34 | protected $config; |
||
35 | |||
36 | /** @var EventManager */ |
||
37 | protected $eventManager; |
||
38 | |||
39 | /** @var ElasticUnitOfWork */ |
||
40 | protected $unitOfWork; |
||
41 | |||
42 | /** @var Expr */ |
||
43 | protected $expressionBuilder; |
||
44 | |||
45 | /** @var ElasticConnection */ |
||
46 | protected $conn; |
||
47 | |||
48 | /** @var ElasticAnnotationDriver */ |
||
49 | private $annotationDriver; |
||
50 | |||
51 | public function __construct(ElasticConnection $connection, EventManager $eventManager = null) { |
||
59 | |||
60 | private function registerEventsListeners() { |
||
81 | |||
82 | /** |
||
83 | * @return ElasticUnitOfWork |
||
84 | */ |
||
85 | public function getUnitOfWork() { |
||
88 | |||
89 | public function getRepository($className) { |
||
92 | |||
93 | public function getReference($entityName, $id) { |
||
104 | |||
105 | public function find($entityName, $id, $lockMode = null, $lockVersion = null) { |
||
108 | |||
109 | public function getCache() { |
||
112 | |||
113 | /** |
||
114 | * @return ElasticConnection |
||
115 | */ |
||
116 | public function getConnection() { |
||
119 | |||
120 | public function getExpressionBuilder() { |
||
127 | |||
128 | public function beginTransaction() { |
||
131 | |||
132 | public function transactional($func) { |
||
135 | |||
136 | public function commit() { |
||
139 | |||
140 | public function rollback() { |
||
143 | |||
144 | public function createQuery($dql = '') { |
||
153 | |||
154 | public function createNamedQuery($name) { |
||
157 | |||
158 | public function createNativeQuery($sql, ResultSetMapping $rsm) { |
||
161 | |||
162 | public function createNamedNativeQuery($name) { |
||
165 | |||
166 | public function createQueryBuilder() { |
||
169 | |||
170 | public function getPartialReference($entityName, $identifier) { |
||
173 | |||
174 | public function close() { |
||
177 | |||
178 | public function copy($entity, $deep = false) { |
||
181 | |||
182 | public function lock($entity, $lockMode, $lockVersion = null) { |
||
185 | |||
186 | public function getEventManager() { |
||
193 | |||
194 | public function getConfiguration() { |
||
197 | |||
198 | public function isOpen() { |
||
201 | |||
202 | public function getHydrator($hydrationMode) { |
||
205 | |||
206 | public function newHydrator($hydrationMode = null) { |
||
209 | |||
210 | public function getProxyFactory() { |
||
213 | |||
214 | public function getFilters() { |
||
217 | |||
218 | public function isFiltersStateClean() { |
||
221 | |||
222 | public function hasFilters() { |
||
225 | |||
226 | public function persist($object) { |
||
229 | |||
230 | public function remove($object) { |
||
233 | |||
234 | public function merge($object) { |
||
237 | |||
238 | public function clear($objectName = null) { |
||
241 | |||
242 | public function detach($object) { |
||
245 | |||
246 | public function refresh($object) { |
||
249 | |||
250 | public function flush($entity = null) { |
||
253 | |||
254 | public function getMetadataFactory() { |
||
257 | |||
258 | public function initializeObject($obj) { |
||
261 | |||
262 | public function contains($object) { |
||
265 | |||
266 | private function getAnnotationDriver(AnnotationReader $annotationReader = null) { |
||
273 | |||
274 | /** |
||
275 | * @param string $className |
||
276 | * @return ClassMetadata |
||
277 | */ |
||
278 | public function getClassMetadata($className) { |
||
287 | } |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.