| Total Complexity | 49 | 
| Total Lines | 312 | 
| Duplicated Lines | 0 % | 
| Changes | 3 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like CodeGeneratorEventDispatcherTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CodeGeneratorEventDispatcherTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 12 | class CodeGeneratorEventDispatcherTest extends TestCase | ||
| 13 | { | ||
| 14 | /** | ||
| 15 | * @var CodeGeneratorEventDispatcher | ||
| 16 | */ | ||
| 17 | private $dispatcher; | ||
| 18 | private $method1; | ||
| 19 | private $method2; | ||
| 20 | private $method3; | ||
| 21 | private $method4; | ||
| 22 | private $method5; | ||
| 23 | private $beanDescriptor; | ||
| 24 | private $configuration; | ||
| 25 | private $class; | ||
| 26 | private $file; | ||
| 27 | private $pivotTableMethodsDescriptor; | ||
| 28 | private $beanPropertyDescriptor; | ||
| 29 | private $nullDispatcher; | ||
| 30 | |||
| 31 | public function setUp(): void | ||
| 32 |     { | ||
| 33 | $this->dispatcher = new CodeGeneratorEventDispatcher([new BaseCodeGeneratorListener()]); | ||
| 34 |         $this->nullDispatcher = new CodeGeneratorEventDispatcher([new class implements CodeGeneratorListenerInterface { | ||
| 35 | public function onBaseBeanGenerated(FileGenerator $fileGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration): ?FileGenerator | ||
| 36 |             { | ||
| 37 | return null; | ||
| 38 | } | ||
| 39 | |||
| 40 | public function onBaseBeanConstructorGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator | ||
| 41 |             { | ||
| 42 | return null; | ||
| 43 | } | ||
| 44 | |||
| 45 | public function onBaseBeanPropertyGenerated(?MethodGenerator $getter, ?MethodGenerator $setter, AbstractBeanPropertyDescriptor $propertyDescriptor, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): array | ||
| 46 |             { | ||
| 47 | return [null, null]; | ||
| 48 | } | ||
| 49 | |||
| 50 | public function onBaseBeanOneToManyGenerated(MethodGenerator $getter, DirectForeignKeyMethodDescriptor $directForeignKeyMethodDescriptor, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator | ||
| 51 |             { | ||
| 52 | return null; | ||
| 53 | } | ||
| 54 | |||
| 55 | public function onBaseBeanManyToManyGenerated(?MethodGenerator $getter, ?MethodGenerator $adder, ?MethodGenerator $remover, ?MethodGenerator $hasser, ?MethodGenerator $setter, PivotTableMethodsDescriptor $pivotTableMethodsDescriptor, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): array | ||
| 56 |             { | ||
| 57 | return [null, null, null, null, null]; | ||
| 58 | } | ||
| 59 | |||
| 60 | public function onBaseBeanJsonSerializeGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator | ||
| 61 |             { | ||
| 62 | return null; | ||
| 63 | } | ||
| 64 | |||
| 65 | public function onBaseBeanCloneGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator | ||
| 68 | } | ||
| 69 | |||
| 70 | public function onBaseDaoGenerated(FileGenerator $fileGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration): ?FileGenerator | ||
| 71 |             { | ||
| 72 | return null; | ||
| 73 | } | ||
| 74 | |||
| 75 | public function onBaseDaoConstructorGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator | ||
| 76 |             { | ||
| 77 | return null; | ||
| 78 | } | ||
| 79 | |||
| 80 | public function onBaseDaoSaveGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator | ||
| 81 |             { | ||
| 82 | return null; | ||
| 83 | } | ||
| 84 | |||
| 85 | public function onBaseDaoFindAllGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator | ||
| 86 |             { | ||
| 87 | return null; | ||
| 88 | } | ||
| 89 | |||
| 90 | public function onBaseDaoGetByIdGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator | ||
| 91 |             { | ||
| 92 | return null; | ||
| 93 | } | ||
| 94 | |||
| 95 | public function onBaseDaoDeleteGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator | ||
| 96 |             { | ||
| 97 | return null; | ||
| 98 | } | ||
| 99 | |||
| 100 | public function onBaseDaoFindGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator | ||
| 101 |             { | ||
| 102 | return null; | ||
| 103 | } | ||
| 104 | |||
| 105 | public function onBaseDaoFindFromSqlGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator | ||
| 108 | } | ||
| 109 | |||
| 110 | public function onBaseDaoFindFromRawSqlGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator | ||
| 111 |             { | ||
| 112 | return null; | ||
| 113 | } | ||
| 114 | |||
| 115 | public function onBaseDaoFindOneGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator | ||
| 116 |             { | ||
| 117 | return null; | ||
| 118 | } | ||
| 119 | |||
| 120 | public function onBaseDaoFindOneFromSqlGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator | ||
| 121 |             { | ||
| 122 | return null; | ||
| 123 | } | ||
| 124 | |||
| 125 | public function onBaseDaoSetDefaultSortGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator | ||
| 126 |             { | ||
| 127 | return null; | ||
| 128 | } | ||
| 129 | |||
| 130 | public function onBaseDaoFindByIndexGenerated(MethodGenerator $methodGenerator, Index $index, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator | ||
| 133 | } | ||
| 134 | |||
| 135 | /** | ||
| 136 | * @param BeanDescriptor[] $beanDescriptors | ||
| 137 | */ | ||
| 138 | public function onDaoFactoryGenerated(FileGenerator $fileGenerator, array $beanDescriptors, ConfigurationInterface $configuration): ?FileGenerator | ||
| 139 |             { | ||
| 140 | return null; | ||
| 141 | } | ||
| 142 | |||
| 143 | /** | ||
| 144 | * @param BeanDescriptor[] $beanDescriptors | ||
| 145 | */ | ||
| 146 | public function onDaoFactoryConstructorGenerated(MethodGenerator $methodGenerator, array $beanDescriptors, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator | ||
| 147 |             { | ||
| 148 | return null; | ||
| 149 | } | ||
| 150 | |||
| 151 | /** | ||
| 152 | * @param BeanDescriptor[] $beanDescriptor | ||
| 153 | */ | ||
| 154 | public function onDaoFactoryGetterGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator | ||
| 155 |             { | ||
| 156 | return null; | ||
| 157 | } | ||
| 158 | |||
| 159 | /** | ||
| 160 | * @param BeanDescriptor[] $beanDescriptor | ||
| 161 | */ | ||
| 162 | public function onDaoFactorySetterGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator | ||
| 163 |             { | ||
| 164 | return null; | ||
| 165 | } | ||
| 166 | }]); | ||
| 167 | $this->method1 = new MethodGenerator(); | ||
| 168 | $this->method2 = new MethodGenerator(); | ||
| 169 | $this->method3 = new MethodGenerator(); | ||
| 170 | $this->method4 = new MethodGenerator(); | ||
| 171 | $this->method5 = new MethodGenerator(); | ||
| 172 | $this->beanDescriptor = $this->getMockBuilder(BeanDescriptor::class)->disableOriginalConstructor()->getMock(); | ||
| 173 | $this->configuration = $this->getMockBuilder(ConfigurationInterface::class)->disableOriginalConstructor()->getMock(); | ||
| 174 | $this->pivotTableMethodsDescriptor = $this->getMockBuilder(PivotTableMethodsDescriptor::class)->disableOriginalConstructor()->getMock(); | ||
| 175 | $this->beanPropertyDescriptor = $this->getMockBuilder(ScalarBeanPropertyDescriptor::class)->disableOriginalConstructor()->getMock(); | ||
| 176 | $this->class = new ClassGenerator(); | ||
| 177 | $this->file = new FileGenerator(); | ||
| 178 | } | ||
| 179 | |||
| 180 | public function testOnBaseDaoFindAllGenerated(): void | ||
| 181 |     { | ||
| 182 | $this->assertSame($this->method1, $this->dispatcher->onBaseDaoFindAllGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 183 | $this->assertSame(null, $this->nullDispatcher->onBaseDaoFindAllGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 184 | } | ||
| 185 | |||
| 186 | public function testOnBaseBeanJsonSerializeGenerated(): void | ||
| 187 |     { | ||
| 188 | $this->assertSame($this->method1, $this->dispatcher->onBaseBeanJsonSerializeGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 189 | $this->assertSame(null, $this->nullDispatcher->onBaseBeanJsonSerializeGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 190 | } | ||
| 191 | |||
| 192 | public function testOnBaseDaoConstructorGenerated(): void | ||
| 193 |     { | ||
| 194 | $this->assertSame($this->method1, $this->dispatcher->onBaseDaoConstructorGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 195 | $this->assertSame(null, $this->nullDispatcher->onBaseDaoConstructorGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 196 | } | ||
| 197 | |||
| 198 | public function testOnBaseDaoFindOneFromSqlGenerated(): void | ||
| 199 |     { | ||
| 200 | $this->assertSame($this->method1, $this->dispatcher->onBaseDaoFindOneFromSqlGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 201 | $this->assertSame(null, $this->nullDispatcher->onBaseDaoFindOneFromSqlGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 202 | } | ||
| 203 | |||
| 204 | public function testOnBaseDaoGenerated(): void | ||
| 205 |     { | ||
| 206 | $this->assertSame($this->file, $this->dispatcher->onBaseDaoGenerated($this->file, $this->beanDescriptor, $this->configuration)); | ||
| 207 | $this->assertSame(null, $this->nullDispatcher->onBaseDaoGenerated($this->file, $this->beanDescriptor, $this->configuration)); | ||
| 208 | } | ||
| 209 | |||
| 210 | public function testOnBaseDaoSetDefaultSortGenerated(): void | ||
| 211 |     { | ||
| 212 | $this->assertSame($this->method1, $this->dispatcher->onBaseDaoSetDefaultSortGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 213 | $this->assertSame(null, $this->nullDispatcher->onBaseDaoSetDefaultSortGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 214 | } | ||
| 215 | |||
| 216 | public function testOnBaseDaoSaveGenerated(): void | ||
| 217 |     { | ||
| 218 | $this->assertSame($this->method1, $this->dispatcher->onBaseDaoSaveGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 219 | $this->assertSame(null, $this->nullDispatcher->onBaseDaoSaveGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 220 | } | ||
| 221 | |||
| 222 | public function testOnBaseDaoGetByIdGenerated(): void | ||
| 223 |     { | ||
| 224 | $this->assertSame($this->method1, $this->dispatcher->onBaseDaoGetByIdGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 225 | $this->assertSame(null, $this->nullDispatcher->onBaseDaoGetByIdGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 226 | } | ||
| 227 | |||
| 228 | public function testOnBaseBeanCloneGenerated(): void | ||
| 232 | } | ||
| 233 | |||
| 234 | public function testOnBaseDaoFindOneGenerated(): void | ||
| 235 |     { | ||
| 236 | $this->assertSame($this->method1, $this->dispatcher->onBaseDaoFindOneGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 237 | $this->assertSame(null, $this->nullDispatcher->onBaseDaoFindOneGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 238 | } | ||
| 239 | |||
| 240 | public function testOnBaseBeanPropertyGenerated(): void | ||
| 244 | } | ||
| 245 | |||
| 246 | public function testOnBaseDaoFindGenerated(): void | ||
| 247 |     { | ||
| 248 | $this->assertSame($this->method1, $this->dispatcher->onBaseDaoFindGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 249 | $this->assertSame(null, $this->nullDispatcher->onBaseDaoFindGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 250 | } | ||
| 251 | |||
| 252 | public function testOnBaseBeanOneToManyGenerated(): void | ||
| 253 |     { | ||
| 254 | $directForeignKeyMethodDescriptor = $this->getMockBuilder(DirectForeignKeyMethodDescriptor::class)->disableOriginalConstructor()->getMock(); | ||
| 255 | $this->assertSame($this->method1, $this->dispatcher->onBaseBeanOneToManyGenerated($this->method1, $directForeignKeyMethodDescriptor, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 256 | $this->assertSame(null, $this->nullDispatcher->onBaseBeanOneToManyGenerated($this->method1, $directForeignKeyMethodDescriptor, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 257 | } | ||
| 258 | |||
| 259 | public function testOnBaseDaoFindFromSqlGenerated(): void | ||
| 260 |     { | ||
| 261 | $this->assertSame($this->method1, $this->dispatcher->onBaseDaoFindFromSqlGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 262 | $this->assertSame(null, $this->nullDispatcher->onBaseDaoFindFromSqlGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 263 | } | ||
| 264 | |||
| 265 | public function testOnBaseBeanConstructorGenerated(): void | ||
| 266 |     { | ||
| 267 | $this->assertSame($this->method1, $this->dispatcher->onBaseBeanConstructorGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 268 | $this->assertSame(null, $this->nullDispatcher->onBaseBeanConstructorGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 269 | } | ||
| 270 | |||
| 271 | public function testOnBaseDaoFindFromRawSqlGenerated(): void | ||
| 275 | } | ||
| 276 | |||
| 277 | public function testOnBaseDaoDeleteGenerated(): void | ||
| 278 |     { | ||
| 279 | $this->assertSame($this->method1, $this->dispatcher->onBaseDaoDeleteGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 280 | $this->assertSame(null, $this->nullDispatcher->onBaseDaoDeleteGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 281 | } | ||
| 282 | |||
| 283 | public function testOnBaseBeanGenerated(): void | ||
| 284 |     { | ||
| 285 | $this->assertSame($this->file, $this->dispatcher->onBaseBeanGenerated($this->file, $this->beanDescriptor, $this->configuration)); | ||
| 286 | $this->assertSame(null, $this->nullDispatcher->onBaseBeanGenerated($this->file, $this->beanDescriptor, $this->configuration)); | ||
| 287 | } | ||
| 288 | |||
| 289 | public function testOnBaseBeanManyToManyGenerated(): void | ||
| 290 |     { | ||
| 291 | $this->assertSame([$this->method1, $this->method2, $this->method3, $this->method4, $this->method5], $this->dispatcher->onBaseBeanManyToManyGenerated($this->method1, $this->method2, $this->method3, $this->method4, $this->method5, $this->pivotTableMethodsDescriptor, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 292 | $this->assertSame([null, null, null, null, null], $this->nullDispatcher->onBaseBeanManyToManyGenerated(null, null, null, null, null, $this->pivotTableMethodsDescriptor, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 293 | } | ||
| 294 | |||
| 295 | public function testOnBaseDaoFindByIndexGenerated(): void | ||
| 296 |     { | ||
| 297 | $index = $this->getMockBuilder(Index::class)->disableOriginalConstructor()->getMock(); | ||
| 298 | $this->assertSame($this->method1, $this->dispatcher->onBaseDaoFindByIndexGenerated($this->method1, $index, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 299 | $this->assertSame(null, $this->nullDispatcher->onBaseDaoFindByIndexGenerated($this->method1, $index, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 300 | } | ||
| 301 | |||
| 302 | public function testOnDaoFactoryGenerated(): void | ||
| 303 |     { | ||
| 304 | $this->assertSame($this->file, $this->dispatcher->onDaoFactoryGenerated($this->file, [], $this->configuration)); | ||
| 305 | $this->assertSame(null, $this->nullDispatcher->onDaoFactoryGenerated($this->file, [], $this->configuration)); | ||
| 306 | } | ||
| 307 | |||
| 308 | public function testOnDaoFactoryConstructorGenerated(): void | ||
| 309 |     { | ||
| 310 | $this->assertSame($this->method1, $this->dispatcher->onDaoFactoryConstructorGenerated($this->method1, [], $this->configuration, $this->class)); | ||
| 311 | $this->assertSame(null, $this->nullDispatcher->onDaoFactoryConstructorGenerated($this->method1, [], $this->configuration, $this->class)); | ||
| 312 | } | ||
| 313 | |||
| 314 | public function testOnDaoFactoryGetterGenerated(): void | ||
| 315 |     { | ||
| 316 | $this->assertSame($this->method1, $this->dispatcher->onDaoFactoryGetterGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 317 | $this->assertSame(null, $this->nullDispatcher->onDaoFactoryGetterGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); | ||
| 318 | } | ||
| 319 | |||
| 320 | public function testOnDaoFactorySetterGenerated(): void | ||
| 324 | } | ||
| 325 | } | ||
| 326 |