| Total Complexity | 89 | 
| Total Lines | 275 | 
| Duplicated Lines | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like ExceptionUtil 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.
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 ExceptionUtil, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 17 | class ExceptionUtil  | 
            ||
| 18 | { | 
            ||
| 19 | public const PERSISTENCE_EXCEPTION_MESSAGE = "An exception occurred in the " .  | 
            ||
| 20 | "persistence layer. Please check the server logs for a detailed message and the entire " .  | 
            ||
| 21 | "exception stack trace.";  | 
            ||
| 22 | |||
| 23 | public static function getExceptionStacktrace($obj): ?string  | 
            ||
| 31 | }  | 
            ||
| 32 | |||
| 33 | |||
| 34 | public static function createJobExceptionByteArray(?string $byteArray, ResourceTypeInterface $type): ?ByteArrayEntity  | 
            ||
| 35 |     { | 
            ||
| 36 |         return self::createExceptionByteArray("job.exceptionByteArray", $byteArray, $type); | 
            ||
| 37 | }  | 
            ||
| 38 | |||
| 39 | /**  | 
            ||
| 40 | * create ByteArrayEntity with specified name and payload and make sure it's  | 
            ||
| 41 | * persisted  | 
            ||
| 42 | *  | 
            ||
| 43 | * used in Jobs and ExternalTasks  | 
            ||
| 44 | *  | 
            ||
| 45 | * @param name - type\source of the exception  | 
            ||
| 46 | * @param byteArray - payload of the exception  | 
            ||
| 47 | * @param type - resource type of the exception  | 
            ||
| 48 | * @return persisted entity  | 
            ||
| 49 | */  | 
            ||
| 50 | public static function createExceptionByteArray(string $name, ?string $byteArray, ResourceTypeInterface $type): ?ByteArrayEntity  | 
            ||
| 51 |     { | 
            ||
| 52 | $result = null;  | 
            ||
| 53 | |||
| 54 |         if ($byteArray != null) { | 
            ||
| 55 | $result = new ByteArrayEntity($name, $byteArray, $type);  | 
            ||
| 56 | Context::getCommandContext()  | 
            ||
| 57 | ->getByteArrayManager()  | 
            ||
| 58 | ->insertByteArray($result);  | 
            ||
| 59 | }  | 
            ||
| 60 | |||
| 61 | return $result;  | 
            ||
| 62 | }  | 
            ||
| 63 | |||
| 64 | protected static function getPersistenceCauseException(/*ServerException|ORMException*/$persistenceException): \Throwable  | 
            ||
| 65 |     { | 
            ||
| 66 |         if (method_exists($persistenceException, "getCause")) { | 
            ||
| 67 | $cause = $persistenceException->getCause();  | 
            ||
| 68 |             if ($cause !== null) { | 
            ||
| 69 | return $cause;  | 
            ||
| 70 | }  | 
            ||
| 71 |         } elseif (method_exists($persistenceException, "getPrevious")) { | 
            ||
| 72 | $cause = $persistenceException->getPrevious();  | 
            ||
| 73 |             if ($cause !== null) { | 
            ||
| 74 | return $cause;  | 
            ||
| 75 | }  | 
            ||
| 76 | }  | 
            ||
| 77 | return $persistenceException;  | 
            ||
| 78 | }  | 
            ||
| 79 | |||
| 80 | public static function unwrapException(/*ProcessEngineException|ServerException|ORMException*/$exception): ?\Exception  | 
            ||
| 81 |     { | 
            ||
| 82 |         if ($exception instanceof ProcessEngineException) { | 
            ||
| 83 | $cause = null;  | 
            ||
| 84 |             if (method_exists($exception, "getCause")) { | 
            ||
| 85 | $cause = $exception->getCause();  | 
            ||
| 86 |             } elseif (method_exists($exception, "getPrevious")) { | 
            ||
| 87 | $cause = $exception->getPrevious();  | 
            ||
| 88 | }  | 
            ||
| 89 | |||
| 90 |             if ($cause instanceof ProcessEngineException) { | 
            ||
| 91 | $processEngineExceptionCause = null;  | 
            ||
| 92 |                 if (method_exists($cause, "getCause")) { | 
            ||
| 93 | $processEngineExceptionCause = $cause->getCause();  | 
            ||
| 94 |                 } elseif (method_exists($cause, "getPrevious")) { | 
            ||
| 95 | $processEngineExceptionCause = $cause->getPrevious();  | 
            ||
| 96 | }  | 
            ||
| 97 | |||
| 98 |                 if ($processEngineExceptionCause instanceof ServerException || $processEngineExceptionCause instanceof ORMException) { | 
            ||
| 99 | return self::unwrapException($processEngineExceptionCause);  | 
            ||
| 100 |                 } else { | 
            ||
| 101 | return null;  | 
            ||
| 102 | }  | 
            ||
| 103 |             } elseif ($cause instanceof ServerException || $cause instanceof ORMException) { | 
            ||
| 104 | return self::getPersistenceCauseException($cause);  | 
            ||
| 105 |             } else { | 
            ||
| 106 | return null;  | 
            ||
| 107 | }  | 
            ||
| 108 |         } elseif ($exception instanceof ServerException || $exception instanceof ORMException) { | 
            ||
| 109 | return self::getPersistenceCauseException($exception);  | 
            ||
| 110 |         } else { | 
            ||
| 111 | return null;  | 
            ||
| 112 | }  | 
            ||
| 113 | }  | 
            ||
| 114 | |||
| 115 | public static function checkValueTooLongException($exception): bool  | 
            ||
| 116 |     { | 
            ||
| 117 |         if ($exception instanceof ProcessEngineException) { | 
            ||
| 118 | $sqlException = self::unwrapException($exception);  | 
            ||
| 119 |             if ($sqlException === null) { | 
            ||
| 120 | return false;  | 
            ||
| 121 |             } else { | 
            ||
| 122 | return self::checkValueTooLongException($sqlException);  | 
            ||
| 123 | }  | 
            ||
| 124 |         } elseif ($exception instanceof ServerException || $exception instanceof ORMException) { | 
            ||
| 125 | $message = $exception->getMessage();  | 
            ||
| 126 | return strpos($message, "too long") !== false ||  | 
            ||
| 127 | strpos($message, "too large") !== false ||  | 
            ||
| 128 | strpos($message, "TOO LARGE") !== false ||  | 
            ||
| 129 | strpos($message, "ORA-01461") !== false ||  | 
            ||
| 130 | strpos($message, "ORA-01401") !== false ||  | 
            ||
| 131 | strpos($message, "data would be truncated") !== false ||  | 
            ||
| 132 | strpos($message, "SQLCODE=-302, SQLSTATE=22001");  | 
            ||
| 133 |         } else { | 
            ||
| 134 | return false;  | 
            ||
| 135 | }  | 
            ||
| 136 | }  | 
            ||
| 137 | |||
| 138 | public static function checkConstraintViolationException($exception): bool  | 
            ||
| 156 | }  | 
            ||
| 157 | |||
| 158 | public static function checkForeignKeyConstraintViolation(/*ProcessEngineException|ServerException|ORMException*/$exception, bool $skipPostgres): bool  | 
            ||
| 159 |     { | 
            ||
| 160 |         if ($exception instanceof ProcessEngineException) { | 
            ||
| 161 | $sqlException = self::unwrapException($exception);  | 
            ||
| 162 |             if ($sqlException === null) { | 
            ||
| 163 | return false;  | 
            ||
| 164 |             } else { | 
            ||
| 165 | return self::checkForeignKeyConstraintViolation($sqlException);  | 
            ||
| 166 | }  | 
            ||
| 167 | }  | 
            ||
| 168 | |||
| 169 |         if ($exception instanceof ForeignKeyConstraintViolationException) { | 
            ||
| 170 | return true;  | 
            ||
| 171 | }  | 
            ||
| 172 | |||
| 173 | $message = strtolower($exception->getMessage());  | 
            ||
| 174 | $sqlState = null;  | 
            ||
| 175 |         if (method_exists($exception, 'getSQLState')) { | 
            ||
| 176 | $sqlState = $exception->getSQLState();  | 
            ||
| 177 |         } else { | 
            ||
| 178 | //SQLSTATE  | 
            ||
| 179 |             preg_match_all('/SQLSTATE\[(\d*)\]/', $message, $matches); | 
            ||
| 180 |             if (!empty($matches[0])) { | 
            ||
| 181 | $sqlState = $matches[0];  | 
            ||
| 182 | }  | 
            ||
| 183 | }  | 
            ||
| 184 | $errorCode = $exception->getCode();  | 
            ||
| 185 | |||
| 186 |         if ($sqlState == '23503' && $errorCode == 0) { | 
            ||
| 187 | return !$skipPostgres;  | 
            ||
| 188 |         } else { | 
            ||
| 189 | // SqlServer  | 
            ||
| 190 | return strpos($message, "foreign key constraint") !== false ||  | 
            ||
| 191 | $sqlState == "23000" && $errorCode == 547 ||  | 
            ||
| 192 | // MySql & MariaDB & PostgreSQL  | 
            ||
| 193 | $sqlState == "23000" && $errorCode == 1452 ||  | 
            ||
| 194 | // Oracle & H2  | 
            ||
| 195 | strpos($message, "integrity constraint") !== false ||  | 
            ||
| 196 | // Oracle  | 
            ||
| 197 | $sqlState == "23000" && $errorCode == 2291 ||  | 
            ||
| 198 | // H2  | 
            ||
| 199 | //$sqlState == "23506" && $errorCode == 23506 ||  | 
            ||
| 200 | // DB2  | 
            ||
| 201 | strpos($message, "sqlstate=23503") !== false && strpos($message, "sqlcode=-530") !== false ||  | 
            ||
| 202 | // DB2 zOS  | 
            ||
| 203 | $sqlState == "23503" && $errorCode == -530;  | 
            ||
| 204 | }  | 
            ||
| 205 | }  | 
            ||
| 206 | |||
| 207 | public static function checkVariableIntegrityViolation($exception): bool  | 
            ||
| 208 |     { | 
            ||
| 209 |         if ($exception instanceof ProcessEngineException) { | 
            ||
| 210 | $sqlException = self::unwrapException($exception);  | 
            ||
| 211 |             if ($sqlException === null) { | 
            ||
| 212 | return false;  | 
            ||
| 213 |             } else { | 
            ||
| 214 | return self::checkVariableIntegrityViolation($sqlException);  | 
            ||
| 215 | }  | 
            ||
| 216 | }  | 
            ||
| 217 | |||
| 218 | $message = strtolower($exception->getMessage());  | 
            ||
| 219 | $sqlState = null;  | 
            ||
| 220 |         if (method_exists($exception, 'getSQLState')) { | 
            ||
| 221 | $sqlState = $exception->getSQLState();  | 
            ||
| 222 |         } else { | 
            ||
| 223 | //SQLSTATE  | 
            ||
| 224 |             preg_match_all('/SQLSTATE\[(\d*)\]/', $message, $matches); | 
            ||
| 225 |             if (!empty($matches[0])) { | 
            ||
| 226 | $sqlState = $matches[0];  | 
            ||
| 227 | }  | 
            ||
| 228 | }  | 
            ||
| 229 | $errorCode = $exception->getCode();  | 
            ||
| 230 | |||
| 231 | // MySQL & MariaDB  | 
            ||
| 232 | return (strpos($message, "act_uniq_variable") !== false && $sqlState == "23000" && $errorCode == 1062)  | 
            ||
| 233 | // PostgreSQL  | 
            ||
| 234 | || (strpos($message, "act_uniq_variable") !== false && $sqlState == "23505" && $errorCode == 0)  | 
            ||
| 235 | // SqlServer  | 
            ||
| 236 | || (strpos($message, "act_uniq_variable") !== false && $sqlState == "23000" && $errorCode == 2601)  | 
            ||
| 237 | // Oracle  | 
            ||
| 238 | || (strpos($message, "act_uniq_variable") !== false && $sqlState == "23000" && $errorCode == 1);  | 
            ||
| 239 | }  | 
            ||
| 240 | |||
| 241 | public static function checkDeadlockException($sqlException): bool  | 
            ||
| 260 | }  | 
            ||
| 261 | |||
| 262 | public static function findBatchExecutorException($exception)  | 
            ||
| 265 | }  | 
            ||
| 266 | |||
| 267 | /**  | 
            ||
| 268 | * Pass logic, which directly calls MyBatis API. In case a MyBatis exception is thrown, it is  | 
            ||
| 269 |      * wrapped into a {@link ProcessEngineException} and never propagated directly to an Engine API | 
            ||
| 270 | * call. In some cases, the top-level exception and its message are shown as a response body in  | 
            ||
| 271 | * the REST API. Wrapping all MyBatis API calls in our codebase makes sure that the top-level  | 
            ||
| 272 |      * exception is always a {@link ProcessEngineException} with a generic message. Like this, SQL | 
            ||
| 273 | * details are never disclosed to potential attackers.  | 
            ||
| 274 | *  | 
            ||
| 275 | * @param supplier which calls MyBatis API  | 
            ||
| 276 | * @param <T> is the type of the return value  | 
            ||
| 277 | * @return the value returned by the supplier  | 
            ||
| 278 | * @throws ProcessEngineException which wraps the actual exception  | 
            ||
| 279 | */  | 
            ||
| 280 | public static function doWithExceptionWrapper(callable $supplier)  | 
            ||
| 286 | }  | 
            ||
| 287 | }  | 
            ||
| 288 | |||
| 289 | public static function wrapPersistenceException(\Exception $ex): ProcessEngineException  | 
            ||
| 290 |     { | 
            ||
| 291 | return new ProcessEngineException(self::PERSISTENCE_EXCEPTION_MESSAGE, $ex);  | 
            ||
| 292 | }  | 
            ||
| 293 | }  | 
            ||
| 294 |