Conditions | 7 |
Paths | 16 |
Total Lines | 66 |
Code Lines | 44 |
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 |
||
104 | public function guardTransition(GuardEvent $event, $eventName) |
||
105 | { |
||
106 | if (!array_key_exists($eventName, $this->supportedEventsConfig)) { |
||
107 | throw new UnsupportedGuardEventException(sprintf("Cannot find registered guard event by name '%s'", $eventName)); |
||
108 | } |
||
109 | |||
110 | list($workflowName, $expression) = $this->supportedEventsConfig[$eventName]; |
||
111 | $subject = $event->getSubject(); |
||
112 | $workflowContext = new WorkflowContext( |
||
113 | $this->workflowRegistry->get($subject, $workflowName), |
||
114 | $subject, |
||
115 | $this->subjectManipulator->getSubjectId($subject) |
||
116 | ); |
||
117 | $loggerContext = $workflowContext->getLoggerContext(); |
||
118 | |||
119 | $expressionFailure = false; |
||
120 | $errorMessage = null; |
||
121 | |||
122 | try { |
||
123 | $expressionResult = $this->language->evaluate($expression, ['event' => $event]); |
||
124 | } catch (Exception $e) { |
||
125 | $errorMessage = sprintf( |
||
126 | "Guard expression '%s' for guard event '%s' cannot be evaluated. Details: '%s'", |
||
127 | $expression, |
||
128 | $eventName, |
||
129 | $e->getMessage() |
||
130 | ); |
||
131 | $expressionFailure = true; |
||
132 | } catch (Throwable $e) { |
||
133 | $errorMessage = sprintf( |
||
134 | "Guard expression '%s' for guard event '%s' cannot be evaluated. Details: '%s'", |
||
135 | $expression, |
||
136 | $eventName, |
||
137 | $e->getMessage() |
||
138 | ); |
||
139 | $expressionFailure = true; |
||
140 | } |
||
141 | |||
142 | if ($expressionFailure) { |
||
143 | $this->logger->error($errorMessage, $loggerContext); |
||
144 | |||
145 | // simply skipping processing here without blocking transition |
||
146 | return; |
||
147 | } |
||
148 | |||
149 | if (!is_bool($expressionResult)) { |
||
150 | $this->logger->debug( |
||
151 | sprintf("Guard expression '%s' for guard event '%s' evaluated with non-boolean result". |
||
152 | " and will be converted to boolean", $expression, $eventName), |
||
153 | $loggerContext |
||
154 | ); |
||
155 | } |
||
156 | |||
157 | $event->setBlocked($expressionResult); |
||
158 | |||
159 | if ($expressionResult) { |
||
160 | $this->logger->debug( |
||
161 | sprintf("Transition '%s' is blocked by guard expression '%s' for guard event '%s'", |
||
162 | $event->getTransition()->getName(), |
||
163 | $expression, |
||
164 | $eventName |
||
165 | ), |
||
166 | $loggerContext |
||
167 | ); |
||
168 | } |
||
169 | } |
||
170 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.