Conditions | 1 |
Paths | 1 |
Total Lines | 112 |
Code Lines | 67 |
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 |
||
27 | { |
||
28 | $this->container->setParameter( |
||
29 | 'kernel.bundles', |
||
30 | array() |
||
31 | ); |
||
32 | $this->load(array('document_tree' => array())); |
||
33 | |||
34 | $this->assertContainerBuilderHasParameter( |
||
35 | 'sonata_admin_doctrine_phpcr.tree_block.configuration', |
||
36 | array( |
||
37 | 'routing_defaults' => array(), |
||
38 | 'repository_name' => null, |
||
39 | 'sortable_by' => 'position', |
||
40 | 'move' => true, |
||
41 | 'reorder' => true, |
||
42 | ) |
||
43 | ); |
||
44 | } |
||
45 | |||
46 | public function testPrependDefaultRepositoryName() |
||
47 | { |
||
48 | $this->container->setParameter('kernel.bundles', array('CmfResourceBundle')); |
||
49 | $this->container->getExtensionConfig('sonata_doctrine_phpcr_admin'); |
||
50 | $this->container->prependExtensionConfig('sonata_doctrine_phpcr_admin', array('document_tree' => array())); |
||
51 | $this->container->prependExtensionConfig('cmf_resource', array('default_repository' => 'default')); |
||
52 | |||
53 | $this->container->getExtension('sonata_doctrine_phpcr_admin')->prepend($this->container); |
||
|
|||
54 | |||
55 | $config = $this->container->getExtensionConfig('sonata_doctrine_phpcr_admin'); |
||
56 | $expectedConfig = array( |
||
57 | 'routing_defaults' => array(), |
||
58 | 'repository_name' => 'default', |
||
59 | 'sortable_by' => 'position', |
||
60 | 'enabled' => true, |
||
61 | ); |
||
62 | |||
63 | $this->assertEquals($expectedConfig, $config[0]['document_tree']); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
||
68 | */ |
||
69 | public function testPrependDefaultRepositoryNameThrowsOnNonExistingResourceBundle() |
||
70 | { |
||
71 | $this->container->setParameter('kernel.bundles', array()); |
||
72 | $this->container->getExtensionConfig('sonata_doctrine_phpcr_admin'); |
||
73 | $this->container->prependExtensionConfig('sonata_doctrine_phpcr_admin', array('document_tree' => array())); |
||
74 | $this->container->prependExtensionConfig('cmf_resource', array('default_repository' => 'default')); |
||
75 | |||
76 | $this->container->getExtension('sonata_doctrine_phpcr_admin')->prepend($this->container); |
||
77 | } |
||
78 | |||
79 | public function testPrependDefaultRepositoryNameKeepsCustomNames() |
||
80 | { |
||
81 | $this->container->setParameter('kernel.bundles', array('CmfResourceBundle')); |
||
82 | $this->container->getExtensionConfig('sonata_doctrine_phpcr_admin'); |
||
83 | $this->container->prependExtensionConfig( |
||
84 | 'sonata_doctrine_phpcr_admin', |
||
85 | array('document_tree' => array('repository_name' => 'custom')) |
||
86 | ); |
||
87 | $this->container->prependExtensionConfig('cmf_resource', array('default_repository' => 'default')); |
||
88 | |||
89 | $this->container->getExtension('sonata_doctrine_phpcr_admin')->prepend($this->container); |
||
90 | |||
91 | $config = $this->container->getExtensionConfig('sonata_doctrine_phpcr_admin'); |
||
92 | $expectedConfig = array('repository_name' => 'custom'); |
||
93 | |||
94 | $this->assertEquals($expectedConfig, $config[0]['document_tree']); |
||
95 | } |
||
96 | } |
||
97 |
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: