Conditions | 5 |
Paths | 12 |
Total Lines | 83 |
Code Lines | 51 |
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 |
||
44 | public function testRepositoryServiceWiring() |
||
45 | { |
||
46 | // needed for older versions of Doctrine |
||
47 | AnnotationRegistry::registerFile(__DIR__.'/../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'); |
||
48 | |||
49 | $container = new ContainerBuilder(new ParameterBag(array( |
||
50 | 'kernel.name' => 'app', |
||
51 | 'kernel.debug' => false, |
||
52 | 'kernel.bundles' => array('RepositoryServiceBundle' => RepositoryServiceBundle::class), |
||
53 | 'kernel.cache_dir' => sys_get_temp_dir(), |
||
54 | 'kernel.environment' => 'test', |
||
55 | 'kernel.root_dir' => __DIR__.'/../../../../', // src dir |
||
56 | ))); |
||
57 | $container->set('annotation_reader', new AnnotationReader()); |
||
58 | $extension = new DoctrineExtension(); |
||
59 | $container->registerExtension($extension); |
||
60 | $extension->load(array(array( |
||
61 | 'dbal' => array( |
||
62 | 'driver' => 'pdo_mysql', |
||
63 | 'charset' => 'UTF8', |
||
64 | ), |
||
65 | 'orm' => array( |
||
66 | 'mappings' => array('RepositoryServiceBundle' => array( |
||
67 | 'type' => 'annotation', |
||
68 | 'dir' => __DIR__.'/DependencyInjection/Fixtures/Bundles/RepositoryServiceBundle/Entity', |
||
69 | 'prefix' => 'Fixtures\Bundles\RepositoryServiceBundle\Entity', |
||
70 | )), |
||
71 | ), |
||
72 | )), $container); |
||
73 | |||
74 | $def = $container->register(TestCustomServiceRepoRepository::class, TestCustomServiceRepoRepository::class) |
||
75 | ->setPublic(false); |
||
76 | |||
77 | // Symfony 2.7 compat - can be moved above later |
||
78 | if (method_exists($container, 'setAutowired')) { |
||
79 | $def->setAutowired(true); |
||
80 | } |
||
81 | |||
82 | // Symfony 3.3 and higher: autowire definition so it receives the tags |
||
83 | if (class_exists(ServiceLocatorTagPass::class)) { |
||
84 | $def->setAutoconfigured(true); |
||
85 | } |
||
86 | |||
87 | $container->addCompilerPass(new ServiceRepositoryCompilerPass()); |
||
88 | $container->compile(); |
||
89 | |||
90 | $em = $container->get('doctrine.orm.default_entity_manager'); |
||
91 | |||
92 | // traditional custom class repository |
||
93 | $customClassRepo = $em->getRepository(TestCustomClassRepoEntity::class); |
||
94 | $this->assertInstanceOf(TestCustomClassRepoRepository::class, $customClassRepo); |
||
95 | // a smoke test, trying some methods |
||
96 | $this->assertSame(TestCustomClassRepoEntity::class, $customClassRepo->getClassName()); |
||
97 | $this->assertInstanceOf(QueryBuilder::class, $customClassRepo->createQueryBuilder('tc')); |
||
98 | |||
99 | // generic EntityRepository |
||
100 | $genericRepository = $em->getRepository(TestDefaultRepoEntity::class); |
||
101 | $this->assertInstanceOf(EntityRepository::class, $genericRepository); |
||
102 | $this->assertSame($genericRepository, $genericRepository = $em->getRepository(TestDefaultRepoEntity::class)); |
||
103 | // a smoke test, trying one of the methods |
||
104 | $this->assertSame(TestDefaultRepoEntity::class, $genericRepository->getClassName()); |
||
105 | |||
106 | // Symfony 3.2 and lower should work normally in traditional cases (tested above) |
||
107 | // the code below should *not* work (by design) |
||
108 | if (!class_exists(ServiceLocatorTagPass::class)) { |
||
109 | $message = '/Support for loading entities from the service container only works for Symfony 3\.3/'; |
||
110 | if (method_exists($this, 'expectException')) { |
||
111 | $this->expectException(\RuntimeException::class); |
||
112 | $this->expectExceptionMessageRegExp($message); |
||
113 | } else { |
||
114 | // PHPUnit 4 compat |
||
115 | $this->setExpectedException(\RuntimeException::class); |
||
|
|||
116 | $this->setExpectedExceptionMessage($message); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | // custom service repository |
||
121 | $customServiceRepo = $em->getRepository(TestCustomServiceRepoEntity::class); |
||
122 | $this->assertSame($customServiceRepo, $container->get(TestCustomServiceRepoRepository::class)); |
||
123 | // a smoke test, trying some methods |
||
124 | $this->assertSame(TestCustomServiceRepoEntity::class, $customServiceRepo->getClassName()); |
||
125 | $this->assertInstanceOf(QueryBuilder::class, $customServiceRepo->createQueryBuilder('tc')); |
||
126 | } |
||
127 | } |
||
128 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.