Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 11 | class ContainerBuilderSpec extends ObjectBehavior |
||
| 12 | { |
||
| 13 | function it_is_initializable() |
||
| 14 | { |
||
| 15 | $this->shouldHaveType(ContainerBuilder::class); |
||
| 16 | } |
||
| 17 | |||
| 18 | function its_container_aware() |
||
| 19 | { |
||
| 20 | $this->shouldBeAnInstanceOf(ContainerAwareInterface::class); |
||
| 21 | } |
||
| 22 | |||
| 23 | function it_creates_empty_containers() |
||
| 24 | { |
||
| 25 | $this->beConstructedWith(); |
||
| 26 | $this->getContainer()->shouldBeAnInstanceOf(ContainerInterface::class); |
||
| 27 | } |
||
| 28 | |||
| 29 | function it_hydrates_a_container_with_an_array_of_definitions(ContainerInterface $container) |
||
| 30 | { |
||
| 31 | $services = [ |
||
| 32 | 'foo' => 'bar', |
||
| 33 | 'baz' => '@foo' |
||
| 34 | ]; |
||
| 35 | $container->register(Argument::type('string'), Argument::any())->shouldBeCalledTimes(2); |
||
| 36 | $this->beConstructedWith($services); |
||
| 37 | $this->setContainer($container); |
||
| 38 | $this->getContainer()->shouldBeAnInstanceOf(ContainerInterface::class); |
||
| 39 | } |
||
| 40 | |||
| 41 | function it_hydrates_a_container_with_an_array_from_a_file(ContainerInterface $container) |
||
| 42 | { |
||
| 43 | $container->register(Argument::type('string'), Argument::any())->shouldBeCalledTimes(3); |
||
| 44 | $this->beConstructedWith(__DIR__ .'/services/services1.php'); |
||
| 45 | $this->setContainer($container); |
||
| 46 | $this->getContainer()->shouldBeAnInstanceOf(ContainerInterface::class); |
||
| 47 | } |
||
| 48 | |||
| 49 | function it_hydrates_a_container_with_all_arrays_form_the_files_within_a_directory(ContainerInterface $container) |
||
| 50 | { |
||
| 51 | $container->register(Argument::type('string'), Argument::any())->shouldBeCalledTimes(6); |
||
| 52 | $this->beConstructedWith(__DIR__ .'/services'); |
||
| 53 | $this->setContainer($container); |
||
| 54 | $this->getContainer()->shouldBeAnInstanceOf(ContainerInterface::class); |
||
| 55 | } |
||
| 56 | |||
| 57 | } |
||
| 58 |