| Conditions | 1 |
| Paths | 1 |
| Total Lines | 87 |
| 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 |
||
| 88 | public function testGetFixedQueryBuilder($class, $alias, $id, $expectedId, $value, $identifierType, $distinct): void |
||
| 89 | { |
||
| 90 | $meta = $this->createMock(ClassMetadata::class); |
||
| 91 | $meta->expects($this->any()) |
||
| 92 | ->method('getIdentifierFieldNames') |
||
| 93 | ->willReturn([$id]); |
||
| 94 | $meta->expects($this->any()) |
||
| 95 | ->method('getTypeOfField') |
||
| 96 | ->willReturn($identifierType); |
||
| 97 | |||
| 98 | $mf = $this->createMock(ClassMetadataFactory::class); |
||
| 99 | $mf->expects($this->any()) |
||
| 100 | ->method('getMetadataFor') |
||
| 101 | ->with($this->equalTo($class)) |
||
| 102 | ->willReturn($meta); |
||
| 103 | |||
| 104 | $platform = $this->createMock(PostgreSqlPlatform::class); |
||
| 105 | |||
| 106 | $conn = $this->createMock(Connection::class); |
||
| 107 | $conn->expects($this->any()) |
||
| 108 | ->method('getDatabasePlatform') |
||
| 109 | ->willReturn($platform); |
||
| 110 | |||
| 111 | $em = $this->createMock(EntityManager::class); |
||
| 112 | $em->expects($this->any()) |
||
| 113 | ->method('getMetadataFactory') |
||
| 114 | ->willReturn($mf); |
||
| 115 | $em->expects($this->any()) |
||
| 116 | ->method('getConnection') |
||
| 117 | ->willReturn($conn); |
||
| 118 | |||
| 119 | $q = $this->getMockBuilder(AbstractQuery::class) |
||
| 120 | ->disableOriginalConstructor() |
||
| 121 | ->setMethods(['setHint', 'execute']) |
||
| 122 | ->getMockForAbstractClass(); |
||
| 123 | $q->expects($this->once()) |
||
| 124 | ->method('setHint') |
||
| 125 | ->willReturn($q); |
||
| 126 | $q->expects($this->any()) |
||
| 127 | ->method('execute') |
||
| 128 | ->willReturn([[$id => $value]]); |
||
| 129 | |||
| 130 | $qb = $this->getMockBuilder(QueryBuilder::class) |
||
| 131 | ->setConstructorArgs([$em]) |
||
| 132 | ->getMock(); |
||
| 133 | $qb->expects($this->once()) |
||
| 134 | ->method('distinct') |
||
| 135 | ->with($this->equalTo($distinct)); |
||
| 136 | $qb->expects($this->any()) |
||
| 137 | ->method('getEntityManager') |
||
| 138 | ->willReturn($em); |
||
| 139 | $qb->expects($this->any()) |
||
| 140 | ->method('getQuery') |
||
| 141 | ->willReturn($q); |
||
| 142 | $qb->expects($this->once()) |
||
| 143 | ->method('setParameter') |
||
| 144 | ->with($this->equalTo($expectedId), $this->equalTo([$value])); |
||
| 145 | $qb->expects($this->any()) |
||
| 146 | ->method('getDQLPart') |
||
| 147 | ->willReturnCallback(static function ($part) use ($class, $alias) { |
||
| 148 | $parts = [ |
||
| 149 | 'from' => [new From($class, $alias)], |
||
| 150 | 'orderBy' => [new OrderBy('whatever', 'DESC')], |
||
| 151 | ]; |
||
| 152 | |||
| 153 | return $parts[$part]; |
||
| 154 | }); |
||
| 155 | $qb->expects($this->once()) |
||
| 156 | ->method('addOrderBy') |
||
| 157 | ->with("$alias.$id", null); |
||
| 158 | $qb->expects($this->once()) |
||
| 159 | ->method('getRootEntities') |
||
| 160 | ->willReturn([$class]); |
||
| 161 | $qb->expects($this->exactly(2)) |
||
| 162 | ->method('getRootAliases') |
||
| 163 | ->willReturn([$alias]); |
||
| 164 | |||
| 165 | $pq = $this->getMockBuilder(ProxyQuery::class) |
||
| 166 | ->setConstructorArgs([$qb]) |
||
| 167 | ->setMethods(['a']) |
||
| 168 | ->getMock(); |
||
| 169 | |||
| 170 | $pq->setDistinct($distinct); |
||
| 171 | |||
| 172 | /* Work */ |
||
| 173 | $pq->execute(); |
||
| 174 | } |
||
| 175 | |||
| 273 |