| Conditions | 8 |
| Paths | 400 |
| Total Lines | 80 |
| Code Lines | 43 |
| Lines | 6 |
| Ratio | 7.5 % |
| Tests | 0 |
| CRAP Score | 72 |
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 |
||
| 28 | public function setUp() { |
||
|
|
|||
| 29 | |||
| 30 | error_log("*************** TEST: ".$this->getName()); |
||
| 31 | |||
| 32 | $cache = SS_Cache::factory('elasticsearch'); |
||
| 33 | $cache->clean(Zend_Cache::CLEANING_MODE_ALL); |
||
| 34 | SS_Cache::set_cache_lifetime('elasticsearch', 3600, 1000); |
||
| 35 | |||
| 36 | // this needs to be called in order to create the list of searchable |
||
| 37 | // classes and fields that are available. Simulates part of a build |
||
| 38 | $classes = array('SearchableTestPage','SiteTree','Page','FlickrPhotoTO','FlickrSetTO', |
||
| 39 | 'FlickrTagTO', 'FlickrAuthorTO'); |
||
| 40 | $this->requireDefaultRecordsFrom = $classes; |
||
| 41 | |||
| 42 | // add Searchable extension where appropriate |
||
| 43 | FlickrSetTO::add_extension('SilverStripe\Elastica\Searchable'); |
||
| 44 | FlickrPhotoTO::add_extension('SilverStripe\Elastica\Searchable'); |
||
| 45 | FlickrTagTO::add_extension('SilverStripe\Elastica\Searchable'); |
||
| 46 | FlickrAuthorTO::add_extension('SilverStripe\Elastica\Searchable'); |
||
| 47 | SearchableTestPage::add_extension('SilverStripe\Elastica\Searchable'); |
||
| 48 | |||
| 49 | $elasticaException = false; |
||
| 50 | |||
| 51 | try { |
||
| 52 | // clear the index |
||
| 53 | $this->service = Injector::inst()->create('SilverStripe\Elastica\ElasticaService'); |
||
| 54 | $this->service->setTestMode(true); |
||
| 55 | |||
| 56 | // A previous test may have deleted the index and then failed, so check for this |
||
| 57 | if (!$this->service->getIndex()->exists()) { |
||
| 58 | $this->service->getIndex()->create(); |
||
| 59 | } |
||
| 60 | $this->service->reset(); |
||
| 61 | |||
| 62 | // FIXME - use request getVar instead? |
||
| 63 | $_GET['progress'] = 20; |
||
| 64 | |||
| 65 | // load fixtures |
||
| 66 | $orig_fixture_file = static::$fixture_file; |
||
| 67 | |||
| 68 | View Code Duplication | foreach (static::$ignoreFixtureFileFor as $testPattern) { |
|
| 69 | $pattern = '/'.$testPattern.'/'; |
||
| 70 | if (preg_match($pattern, $this->getName())) { |
||
| 71 | static::$fixture_file = null; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } catch (Exception $e) { |
||
| 75 | error_log('**** T1 EXCEPTION ' . $e->getMessage()); |
||
| 76 | $elasticaException = true; |
||
| 77 | } |
||
| 78 | |||
| 79 | // this has to be executed otherwise nesting exceptions occur |
||
| 80 | parent::setUp(); |
||
| 81 | |||
| 82 | if ($elasticaException) { |
||
| 83 | $this->fail('T1 Exception with Elasticsearch'); |
||
| 84 | } |
||
| 85 | |||
| 86 | |||
| 87 | try { |
||
| 88 | static::$fixture_file = $orig_fixture_file; |
||
| 89 | |||
| 90 | $this->publishSiteTree(); |
||
| 91 | |||
| 92 | $this->service->reset(); |
||
| 93 | |||
| 94 | // index loaded fixtures |
||
| 95 | $task = new ReindexTask($this->service); |
||
| 96 | // null request is fine as no parameters used |
||
| 97 | |||
| 98 | $task->run(null); |
||
| 99 | } catch (Exception $e) { |
||
| 100 | error_log('**** T2 EXCEPTION ' . $e->getMessage()); |
||
| 101 | $elasticaException = true; |
||
| 102 | } |
||
| 103 | |||
| 104 | if ($elasticaException) { |
||
| 105 | $this->fail('T2 Exception with Elasticsearch'); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 201 |
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: