Conditions | 1 |
Paths | 1 |
Total Lines | 88 |
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 |
||
51 | protected function setUp() |
||
52 | { |
||
53 | $user = new User(); |
||
54 | $user->setId(self::$USER_ID); |
||
55 | $user->setUsername(self::$USER_NAME); |
||
56 | $this->user = $user; |
||
57 | |||
58 | $newEntity = $this->getMockBuilder(LockableEntity::class)->getMock(); |
||
59 | $newEntity->method('getId')->willReturn(null); |
||
60 | $newEntity->method('getEntityClass')->willReturn(self::$TEST_CLASS); |
||
61 | $newEntity->method('getEntityId')->willReturn(self::$TEST_NEW_ENTITY_ID); |
||
62 | $newEntity->method('getUpdated')->willReturn(new \DateTime()); |
||
63 | |||
64 | $entity = $this->getMockBuilder(LockableEntity::class)->getMock(); |
||
65 | $entity->method('getId')->willReturn(self::$TEST_ID); |
||
66 | $entity->method('getEntityClass')->willReturn(self::$TEST_CLASS); |
||
67 | $entity->method('getEntityId')->willReturn(self::$TEST_ENTITY_ID); |
||
68 | $entity->method('getUpdated')->willReturn(new \DateTime()); |
||
69 | |||
70 | $outDatedEntity = $this->getMockBuilder(LockableEntity::class)->getMock(); |
||
71 | $outDatedEntity->method('getId')->willReturn(self::$ALTERNATIVE_TEST_ID); |
||
72 | $outDatedEntity->method('getEntityClass')->willReturn(self::$TEST_CLASS); |
||
73 | $outDatedEntity->method('getEntityId')->willReturn(self::$ALTERNATIVE_TEST_ENTITY_ID); |
||
74 | $outDatedEntity->method('getUpdated')->willReturn(new \DateTime('-1 days')); |
||
75 | |||
76 | $newEntityVersionLock = new EntityVersionLock(); |
||
77 | $newEntityVersionLock->setOwner(self::$ALTERNATIVE_USER); |
||
78 | $newEntityVersionLock->setLockableEntity($newEntity); |
||
|
|||
79 | $newEntityVersionLock->setCreatedAt(new \DateTime()); |
||
80 | |||
81 | $entityVersionLock = new EntityVersionLock(); |
||
82 | $entityVersionLock->setOwner(self::$ALTERNATIVE_USER); |
||
83 | $entityVersionLock->setLockableEntity($entity); |
||
84 | $entityVersionLock->setCreatedAt(new \DateTime()); |
||
85 | |||
86 | $expiredEntityVersionLock = new EntityVersionLock(); |
||
87 | $expiredEntityVersionLock->setOwner($user->getUsername()); |
||
88 | $expiredEntityVersionLock->setLockableEntity($outDatedEntity); |
||
89 | $expiredEntityVersionLock->setCreatedAt(new \DateTime('-1 days')); |
||
90 | |||
91 | $locksMap = [ |
||
92 | [$newEntity, self::$THRESHOLD, $this->user, [$newEntityVersionLock]], |
||
93 | [$entity, self::$THRESHOLD, $this->user, [$entityVersionLock]], |
||
94 | [$outDatedEntity, self::$THRESHOLD, $this->user, []], |
||
95 | ]; |
||
96 | $mockLockRepository = $this->getMockBuilder('Kunstmaan\AdminListBundle\Repository\EntityVersionLockRepository') |
||
97 | ->disableOriginalConstructor() |
||
98 | ->getMock(); |
||
99 | $mockLockRepository |
||
100 | ->expects($this->any()) |
||
101 | ->method('findOneBy') |
||
102 | ->will($this->returnValue($entityVersionLock)); |
||
103 | $mockLockRepository |
||
104 | ->expects($this->any()) |
||
105 | ->method('getExpiredLocks') |
||
106 | ->will($this->returnValue([$expiredEntityVersionLock])); |
||
107 | $mockLockRepository |
||
108 | ->expects($this->any()) |
||
109 | ->method('getLocksForLockableEntity') |
||
110 | ->will($this->returnValueMap($locksMap)); |
||
111 | |||
112 | $lockableMap = [ |
||
113 | [self::$TEST_NEW_ENTITY_ID, self::$TEST_CLASS, $newEntity], |
||
114 | [self::$TEST_ENTITY_ID, self::$TEST_CLASS, $entity], |
||
115 | [self::$ALTERNATIVE_TEST_ENTITY_ID, self::$TEST_CLASS, $outDatedEntity], |
||
116 | ]; |
||
117 | $mockLockableRepository = $this->getMockBuilder('Kunstmaan\AdminListBundle\Repository\LockableEntityRepository') |
||
118 | ->disableOriginalConstructor() |
||
119 | ->getMock(); |
||
120 | $mockLockableRepository |
||
121 | ->expects($this->any()) |
||
122 | ->method('getOrCreate') |
||
123 | ->will($this->returnValueMap($lockableMap)); |
||
124 | |||
125 | $repositoryMap = [ |
||
126 | ['KunstmaanAdminListBundle:EntityVersionLock', $mockLockRepository], |
||
127 | ['KunstmaanAdminListBundle:LockableEntity', $mockLockableRepository], |
||
128 | ]; |
||
129 | $mockObjectManager = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager') |
||
130 | ->disableOriginalConstructor() |
||
131 | ->getMock(); |
||
132 | $mockObjectManager |
||
133 | ->expects($this->any()) |
||
134 | ->method('getRepository') |
||
135 | ->will($this->returnValueMap($repositoryMap)); |
||
136 | |||
137 | $this->object = new EntityVersionLockService($mockObjectManager, self::$THRESHOLD, true); |
||
138 | } |
||
139 | |||
190 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: