Conditions | 8 |
Paths | 9 |
Total Lines | 53 |
Code Lines | 27 |
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 |
||
44 | public function get(string $id): mixed |
||
45 | { |
||
46 | // Custom hydrators should already be registered |
||
47 | if ($this->has($id)) { |
||
48 | return parent::get($id); |
||
49 | } |
||
50 | |||
51 | $entity = $this->typeManager->build(Entity::class, $id); |
||
52 | $config = $entity->getMetadata(); |
||
53 | $hydrator = new DoctrineObject($this->entityManager, $config['byValue']); |
||
54 | |||
55 | // Create field strategy and assign to hydrator |
||
56 | if ($hydrator instanceof StrategyEnabledInterface) { |
||
57 | foreach ($config['fields'] as $fieldName => $fieldMetadata) { |
||
58 | assert( |
||
59 | in_array(StrategyInterface::class, class_implements($fieldMetadata['strategy'])), |
||
60 | 'Strategy must implement ' . StrategyInterface::class, |
||
61 | ); |
||
62 | |||
63 | $hydrator->addStrategy($fieldName, $this->get($fieldMetadata['strategy'])); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | // Create filters and assign to hydrator |
||
68 | if ($hydrator instanceof Filter\FilterEnabledInterface) { |
||
69 | foreach ($config['filters'] as $name => $filterConfig) { |
||
70 | // Default filters to AND |
||
71 | $condition = $filterConfig['condition'] ?? Filter\FilterComposite::CONDITION_AND; |
||
72 | $filterClass = $filterConfig['filter']; |
||
73 | assert( |
||
74 | in_array(Filter\FilterInterface::class, class_implements($filterClass)), |
||
75 | 'Filter must implement ' . StrategyInterface::class, |
||
76 | ); |
||
77 | |||
78 | $hydrator->addFilter($name, $this->get($filterClass), $condition); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | // Create naming strategy and assign to hydrator |
||
83 | if ($hydrator instanceof NamingStrategyEnabledInterface && $config['namingStrategy']) { |
||
84 | $namingStrategyClass = $config['namingStrategy']; |
||
85 | |||
86 | assert( |
||
87 | in_array(NamingStrategyInterface::class, class_implements($namingStrategyClass)), |
||
88 | 'Naming Strategy must implement ' . NamingStrategyInterface::class, |
||
89 | ); |
||
90 | |||
91 | $hydrator->setNamingStrategy($this->get($namingStrategyClass)); |
||
92 | } |
||
93 | |||
94 | $this->set($id, $hydrator); |
||
95 | |||
96 | return $hydrator; |
||
97 | } |
||
99 |