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 |
||
| 12 | class ResolverSpec extends ObjectBehavior |
||
| 13 | { |
||
| 14 | function it_is_initializable() |
||
| 15 | { |
||
| 16 | $this->shouldHaveType(Resolver::class); |
||
| 17 | } |
||
| 18 | |||
| 19 | function its_a_resolver() |
||
| 20 | { |
||
| 21 | $this->shouldBeAnInstanceOf(ResolverInterface::class); |
||
| 22 | } |
||
| 23 | |||
| 24 | function it_resolves_simple_objects() |
||
| 25 | { |
||
| 26 | $data = new DefinitionData(SimpleObject::class); |
||
| 27 | $this->resolve($data)->shouldBeAnInstanceOf(SimpleObject::class); |
||
| 28 | } |
||
| 29 | |||
| 30 | function it_resolvers_objects_with_constructor_dependencies(ContainerInterface $container) |
||
| 31 | { |
||
| 32 | $object = new SimpleObject(); |
||
| 33 | $container->get('first-object')->willReturn($object); |
||
| 34 | $this->setContainer($container); |
||
| 35 | $data = new DefinitionData(ConstructorObject::class, ['@first-object', 'test']); |
||
| 36 | $this->resolve($data)->shouldBeAnInstanceOf(ConstructorObject::class); |
||
| 37 | } |
||
| 38 | |||
| 39 | function it_can_call_methods_with_arguments_on_resolved_objects(ContainerInterface $container, SimpleObject $object) |
||
| 40 | { |
||
| 41 | $container->get('first-object')->willReturn($object); |
||
| 42 | $this->setContainer($container); |
||
| 43 | $data = new DefinitionData(ConstructorObject::class, ['@first-object']); |
||
| 44 | $data->addCall(DefinitionData::METHOD, 'setOther', ['@first-object']); |
||
| 45 | $this->resolve($data)->shouldHavePropertyEquals('other', $object); |
||
| 46 | } |
||
| 47 | |||
| 48 | function it_can_assign_property_values_on_resolved_objects(ContainerInterface $container, SimpleObject $object) |
||
| 49 | { |
||
| 50 | $container->get('first-object')->willReturn($object); |
||
| 51 | $this->setContainer($container); |
||
| 52 | $data = new DefinitionData(ConstructorObject::class, ['@first-object']); |
||
| 53 | $data->addCall(DefinitionData::PROPERTY, 'other', '@first-object'); |
||
| 54 | $this->resolve($data)->shouldHavePropertyEquals('other', $object); |
||
| 55 | } |
||
| 56 | |||
| 57 | function it_creates_object_from_classes_without_constructor() |
||
| 58 | { |
||
| 59 | $data = new DefinitionData(WithoutConstructor::class); |
||
| 60 | $this->resolve($data)->shouldBeAnInstanceOf(WithoutConstructor::class); |
||
| 61 | |||
| 62 | } |
||
| 63 | |||
| 64 | public function getMatchers() |
||
| 65 | { |
||
| 66 | return [ |
||
| 67 | 'havePropertyEquals' => function(ConstructorObject $subject, $property, $expected) { |
||
| 68 | \PHPUnit_Framework_Assert::assertEquals($expected, $subject->{$property}); |
||
| 69 | return true; |
||
| 70 | } |
||
| 71 | ]; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 107 | } |