| Conditions | 8 |
| Paths | 460 |
| Total Lines | 84 |
| Code Lines | 47 |
| Lines | 6 |
| Ratio | 7.14 % |
| Tests | 39 |
| CRAP Score | 8.8338 |
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 |
||
| 35 | 4 | public function setUp() { |
|
|
|
|||
| 36 | 4 | error_log('++++ EL BASE SET UP T1'); |
|
| 37 | // no need to index here as it's done when fixtures are loaded during setup method |
||
| 38 | 4 | $cache = SS_Cache::factory('elasticsearch'); |
|
| 39 | 4 | $cache->clean(Zend_Cache::CLEANING_MODE_ALL); |
|
| 40 | 4 | SS_Cache::set_cache_lifetime('elasticsearch', 3600, 1000); |
|
| 41 | 4 | error_log('++++ EL BASE SET UP T2'); |
|
| 42 | |||
| 43 | // this needs to be called in order to create the list of searchable |
||
| 44 | // classes and fields that are available. Simulates part of a build |
||
| 45 | 4 | $classes = array('SearchableTestPage','SiteTree','Page','FlickrPhotoTO','FlickrSetTO', |
|
| 46 | 4 | 'FlickrTagTO', 'FlickrAuthorTO'); |
|
| 47 | 4 | $this->requireDefaultRecordsFrom = $classes; |
|
| 48 | |||
| 49 | 4 | error_log('++++ EL BASE SET UP T3'); |
|
| 50 | |||
| 51 | // clear the index |
||
| 52 | 4 | $this->service = Injector::inst()->create('SilverStripe\Elastica\ElasticaService'); |
|
| 53 | 4 | $this->service->setTestMode(true); |
|
| 54 | |||
| 55 | 4 | error_log('++++ EL BASE SET UP T4'); |
|
| 56 | |||
| 57 | 4 | $elasticException = false; |
|
| 58 | |||
| 59 | try { |
||
| 60 | // A previous test may have deleted the index and then failed, so check for this |
||
| 61 | 4 | if (!$this->service->getIndex()->exists()) { |
|
| 62 | 1 | error_log('++++ EL BASE SET UP T4a'); |
|
| 63 | |||
| 64 | 1 | $this->service->getIndex()->create(); |
|
| 65 | 1 | } |
|
| 66 | 4 | error_log('++++ EL BASE SET UP T4b'); |
|
| 67 | 4 | $this->service->reset(); |
|
| 68 | 4 | error_log('++++ EL BASE SET UP T4c'); |
|
| 69 | // FIXME - use request getVar instead? |
||
| 70 | 4 | $_GET['progress'] = 20; |
|
| 71 | // load fixtures |
||
| 72 | |||
| 73 | 4 | $orig_fixture_file = static::$fixture_file; |
|
| 74 | 4 | error_log('++++ EL BASE SET UP T5'); |
|
| 75 | |||
| 76 | 4 | View Code Duplication | foreach (static::$ignoreFixtureFileFor as $testPattern) { |
| 77 | $pattern = '/'.$testPattern.'/'; |
||
| 78 | if (preg_match($pattern, $this->getName())) { |
||
| 79 | static::$fixture_file = null; |
||
| 80 | } |
||
| 81 | 4 | } |
|
| 82 | |||
| 83 | 4 | error_log('++++ EL BASE SET UP T6'); |
|
| 84 | 4 | } catch (Exception $e) { |
|
| 85 | error_log("**** EXCEPTION T1 ".$e->getMessage()); |
||
| 86 | $elasticException = true; |
||
| 87 | } |
||
| 88 | 4 | error_log('++++ EL BASE SET UP T7'); |
|
| 89 | |||
| 90 | |||
| 91 | // this needs to run otherwise nested injector errors show up |
||
| 92 | 4 | parent::setUp(); |
|
| 93 | |||
| 94 | 4 | if ($elasticException) { |
|
| 95 | $this->fail('T1 An error has occurred trying to contact Elasticsearch server'); |
||
| 96 | } |
||
| 97 | |||
| 98 | try { |
||
| 99 | 4 | static::$fixture_file = $orig_fixture_file; |
|
| 100 | |||
| 101 | 4 | $this->publishSiteTree(); |
|
| 102 | 4 | $this->service->reset(); |
|
| 103 | |||
| 104 | // index loaded fixtures |
||
| 105 | 4 | $task = new ReindexTask($this->service); |
|
| 106 | // null request is fine as no parameters used |
||
| 107 | |||
| 108 | 4 | $task->run(null); |
|
| 109 | 4 | } catch (Exception $e) { |
|
| 110 | error_log("**** EXCEPTION T2 ".$e->getMessage()); |
||
| 111 | $elasticException = true; |
||
| 112 | } |
||
| 113 | |||
| 114 | 4 | if ($elasticException) { |
|
| 115 | $this->fail('T2 An error has occurred trying to contact Elasticsearch server'); |
||
| 116 | } |
||
| 117 | |||
| 118 | 4 | } |
|
| 119 | |||
| 274 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: