| Conditions | 2 |
| Paths | 2 |
| Total Lines | 54 |
| Lines | 9 |
| Ratio | 16.67 % |
| 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 |
||
| 59 | public function testRendererControllerReferenceWithCompoundMatcher() |
||
| 60 | { |
||
| 61 | $reference = new ControllerReference('FooBundle:bar:baz'); |
||
| 62 | $compoundMatcher = new SiteAccess\Matcher\Compound\LogicalAnd([]); |
||
| 63 | $subMatchers = [ |
||
| 64 | 'Map\URI' => new SiteAccess\Matcher\Map\URI([]), |
||
| 65 | 'Map\Host' => new SiteAccess\Matcher\Map\Host([]), |
||
| 66 | ]; |
||
| 67 | $compoundMatcher->setSubMatchers($subMatchers); |
||
| 68 | $siteAccess = new SiteAccess( |
||
| 69 | 'test', |
||
| 70 | 'test', |
||
| 71 | $compoundMatcher |
||
| 72 | ); |
||
| 73 | $request = new Request(); |
||
| 74 | $request->attributes->set('siteaccess', $siteAccess); |
||
| 75 | $request->attributes->set('semanticPathinfo', '/foo/bar'); |
||
| 76 | $request->attributes->set('viewParametersString', '/(foo)/bar'); |
||
| 77 | $options = ['foo' => 'bar']; |
||
| 78 | $expectedReturn = '/_fragment?foo=bar'; |
||
| 79 | $this->innerRenderer |
||
| 80 | ->expects($this->once()) |
||
| 81 | ->method('render') |
||
| 82 | ->with($reference, $request, $options) |
||
| 83 | ->will($this->returnValue($expectedReturn)); |
||
| 84 | |||
| 85 | $renderer = new InlineFragmentRenderer($this->innerRenderer); |
||
| 86 | $this->assertSame($expectedReturn, $renderer->render($reference, $request, $options)); |
||
| 87 | $this->assertTrue(isset($reference->attributes['serialized_siteaccess'])); |
||
| 88 | $serializedSiteAccess = json_encode($siteAccess); |
||
| 89 | $this->assertSame($serializedSiteAccess, $reference->attributes['serialized_siteaccess']); |
||
| 90 | $this->assertTrue(isset($reference->attributes['serialized_siteaccess_matcher'])); |
||
| 91 | $this->assertSame( |
||
| 92 | $this->getSerializer()->serialize( |
||
| 93 | $siteAccess->matcher, |
||
| 94 | 'json' |
||
| 95 | ), |
||
| 96 | $reference->attributes['serialized_siteaccess_matcher'] |
||
| 97 | ); |
||
| 98 | $this->assertTrue(isset($reference->attributes['serialized_siteaccess_sub_matchers'])); |
||
| 99 | View Code Duplication | foreach ($siteAccess->matcher->getSubMatchers() as $subMatcher) { |
|
| 100 | $this->assertSame( |
||
| 101 | $this->getSerializer()->serialize( |
||
| 102 | $subMatcher, |
||
| 103 | 'json' |
||
| 104 | ), |
||
| 105 | $reference->attributes['serialized_siteaccess_sub_matchers'][get_class($subMatcher)] |
||
| 106 | ); |
||
| 107 | } |
||
| 108 | $this->assertTrue(isset($reference->attributes['semanticPathinfo'])); |
||
| 109 | $this->assertSame('/foo/bar', $reference->attributes['semanticPathinfo']); |
||
| 110 | $this->assertTrue(isset($reference->attributes['viewParametersString'])); |
||
| 111 | $this->assertSame('/(foo)/bar', $reference->attributes['viewParametersString']); |
||
| 112 | } |
||
| 113 | } |
||
| 114 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: