| Conditions | 16 |
| Paths | 48 |
| Total Lines | 62 |
| Code Lines | 34 |
| 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 |
||
| 40 | public function setUpOnce($class) |
||
| 41 | { |
||
| 42 | // May be altered by another class |
||
| 43 | $isAltered = false; |
||
| 44 | $this->extensionsToReapply = []; |
||
| 45 | $this->extensionsToRemove = []; |
||
| 46 | |||
| 47 | /** @var string|SapphireTest $class */ |
||
| 48 | /** @var string|DataObject $dataClass */ |
||
| 49 | // Remove any illegal extensions that are present |
||
| 50 | foreach ($class::getIllegalExtensions() as $dataClass => $extensions) { |
||
| 51 | if (!class_exists($dataClass)) { |
||
| 52 | continue; |
||
| 53 | } |
||
| 54 | if ($extensions === '*') { |
||
| 55 | $extensions = $dataClass::get_extensions(); |
||
| 56 | } |
||
| 57 | foreach ($extensions as $extension) { |
||
| 58 | if (!class_exists($extension) || !$dataClass::has_extension($extension)) { |
||
| 59 | continue; |
||
| 60 | } |
||
| 61 | if (!isset($this->extensionsToReapply[$dataClass])) { |
||
| 62 | $this->extensionsToReapply[$dataClass] = []; |
||
| 63 | } |
||
| 64 | $this->extensionsToReapply[$dataClass][] = $extension; |
||
| 65 | $dataClass::remove_extension($extension); |
||
| 66 | $isAltered = true; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | // Add any required extensions that aren't present |
||
| 71 | foreach ($class::getRequiredExtensions() as $dataClass => $extensions) { |
||
| 72 | if (!class_exists($dataClass)) { |
||
| 73 | throw new LogicException("Test {$class} requires dataClass {$dataClass} which doesn't exist"); |
||
| 74 | } |
||
| 75 | foreach ($extensions as $extension) { |
||
| 76 | $extension = Extension::get_classname_without_arguments($extension); |
||
| 77 | if (!class_exists($extension)) { |
||
| 78 | throw new LogicException("Test {$class} requires extension {$extension} which doesn't exist"); |
||
| 79 | } |
||
| 80 | if (!$dataClass::has_extension($extension)) { |
||
| 81 | if (!isset($this->extensionsToRemove[$dataClass])) { |
||
| 82 | $this->extensionsToRemove[$dataClass] = []; |
||
| 83 | } |
||
| 84 | $this->extensionsToRemove[$dataClass][] = $extension; |
||
| 85 | $dataClass::add_extension($extension); |
||
| 86 | $isAltered = true; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | // clear singletons, they're caching old extension info |
||
| 92 | // which is used in DatabaseAdmin->doBuild() |
||
| 93 | Injector::inst()->unregisterObjects(DataObject::class); |
||
| 94 | |||
| 95 | // If we have altered the schema, but SapphireTest::setUpBeforeClass() would not otherwise |
||
| 96 | // reset the schema (if there were extra objects) then force a reset |
||
| 97 | if ($isAltered && empty($class::getExtraDataObjects())) { |
||
| 98 | DataObject::reset(); |
||
| 99 | $class::resetDBSchema(true, true); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 125 |