| Conditions | 1 |
| Paths | 1 |
| Total Lines | 63 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 25 | public function testConstruct() |
||
| 26 | { |
||
| 27 | $datalayer = new ParameterBag(array('foo' => 'bar')); |
||
| 28 | $dispatcher = new EventDispatcher(); |
||
| 29 | |||
| 30 | $tc_vars = 'toto_var'; |
||
| 31 | $extension = new TagcommanderExtension($datalayer, $dispatcher, $tc_vars); |
||
| 32 | $this->assertEquals('tagcommander_extension', $extension->getName()); |
||
| 33 | |||
| 34 | |||
| 35 | $tc_container_config = array( |
||
| 36 | 'name' => 'ab-test', |
||
| 37 | 'script' => 'my-ab-test-container.js', |
||
| 38 | 'version' => 1, |
||
| 39 | 'alternative' => 'none', |
||
| 40 | ); |
||
| 41 | $extension->addContainer($tc_container_config); |
||
| 42 | |||
| 43 | $tc_event_config = array( |
||
| 44 | 'name' => 'generic', |
||
| 45 | 'function' => 'tc_event_1', |
||
| 46 | ); |
||
| 47 | $extension->addEvent($tc_event_config); |
||
| 48 | |||
| 49 | $this->assertEquals('tagcommander_extension', $extension->getName()); |
||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | $functions = array_reduce( |
||
| 54 | $extension->getFunctions(), |
||
| 55 | function($functions, $twig_function) { |
||
| 56 | $functions[] = $twig_function->getName(); |
||
| 57 | return $functions; |
||
| 58 | } |
||
| 59 | ); |
||
| 60 | $this->assertTrue(in_array('tc_vars', $functions)); |
||
| 61 | $this->assertTrue(in_array('tc_container', $functions)); |
||
| 62 | $this->assertTrue(in_array('tc_event', $functions)); |
||
| 63 | |||
| 64 | |||
| 65 | |||
| 66 | /* test tc_vars */ |
||
| 67 | $doc = new \DOMDocument(); |
||
| 68 | $doc->loadHTML( |
||
| 69 | $extension->tcVars(array('lorem'=>'ipsum')) |
||
| 70 | ); |
||
| 71 | $str = trim($doc->getElementsByTagName('script')->item(0)->nodeValue, ' ;'); |
||
| 72 | list(, $values) = sscanf($str, 'var %s = %s'); |
||
| 73 | $values = json_decode($values); |
||
| 74 | |||
| 75 | $this->assertEquals('ipsum', $values->lorem); |
||
| 76 | |||
| 77 | |||
| 78 | $container = '<script type="text/javascript" src="my-ab-test-container.js?1" async></script>'; |
||
| 79 | $container .= sprintf( |
||
| 80 | '<noscript><iframe src="none" width="1" height="1" rel="noindex,nofollow" sandbox="%s"></iframe></noscript>', |
||
|
|
|||
| 81 | 'allow-same-origin allow-scripts' |
||
| 82 | ); |
||
| 83 | |||
| 84 | /* test tc_event */ |
||
| 85 | $this->assertEquals("tc_event_1('click', {\"foo\":\"bar\"});", $extension->tcEvent('click')); |
||
| 86 | $this->assertEquals($container, $extension->tcContainer('ab-test')); |
||
| 87 | } |
||
| 88 | } |
||
| 89 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.