Skip to content

Commit e4d2c13

Browse files
committed
Merge branch 'feature/doctrine-types-3'
2 parents 3586d1b + 3d19dff commit e4d2c13

File tree

5 files changed

+117
-1
lines changed

5 files changed

+117
-1
lines changed

src/main/java/de/espend/idea/php/annotation/doctrine/util/DoctrineUtil.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ public static void visitCustomTypes(@NotNull Project project, @NotNull ColumnTyp
168168
return true;
169169
});
170170

171+
// Doctrine <= 2
171172
for (PhpClass phpClass : phpClasses) {
172173
String name = PhpElementsUtil.getMethodReturnAsString(phpClass, "getName");
173174
if(name != null) {
@@ -176,6 +177,23 @@ public static void visitCustomTypes(@NotNull Project project, @NotNull ColumnTyp
176177
}
177178
}
178179

180+
// Doctrine >= 3.0
181+
for (PhpClass phpClass : PhpIndex.getInstance(project).getClassesByFQN("\\Doctrine\\DBAL\\Types\\Types")) {
182+
for (Field ownField : phpClass.getOwnFields()) {
183+
if (ownField.isConstant() && ownField.getModifier().isPublic()) {
184+
PsiElement defaultValue = ownField.getDefaultValue();
185+
if (defaultValue != null) {
186+
String name = PhpElementsUtil.getStringValue(defaultValue);
187+
if(name != null) {
188+
found.add(name);
189+
visitor.visit(name, phpClass, defaultValue);
190+
}
191+
}
192+
}
193+
}
194+
}
195+
196+
// static fallback
179197
Stream.of("id", "string", "integer", "smallint", "bigint", "boolean", "decimal", "date", "time", "datetime", "text", "array", "float")
180198
.filter(s -> !found.contains(s))
181199
.forEach(s -> visitor.visit(s, null, null));

src/main/java/de/espend/idea/php/annotation/util/PhpElementsUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public static PhpClass getClassInsideAnnotation(StringLiteralExpression phpDocSt
158158
}
159159

160160
@Nullable
161-
private static String getStringValue(@Nullable PsiElement psiElement) {
161+
public static String getStringValue(@Nullable PsiElement psiElement) {
162162
return getStringValue(psiElement, 0);
163163
}
164164

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package de.espend.idea.php.annotation.tests.doctrine.util;
2+
3+
import com.intellij.codeInsight.lookup.LookupElement;
4+
import de.espend.idea.php.annotation.doctrine.util.DoctrineUtil;
5+
import de.espend.idea.php.annotation.tests.AnnotationLightCodeInsightFixtureTestCase;
6+
7+
import java.util.Collection;
8+
9+
public class DoctrineUtilTest extends AnnotationLightCodeInsightFixtureTestCase {
10+
public void setUp() throws Exception {
11+
super.setUp();
12+
myFixture.copyFileToProject("Type.php");
13+
myFixture.copyFileToProject("Types.php");
14+
}
15+
16+
public String getTestDataPath() {
17+
return "src/test/java/de/espend/idea/php/annotation/tests/doctrine/util/fixtures";
18+
}
19+
20+
public void testGetTypes() {
21+
Collection<LookupElement> types = DoctrineUtil.getTypes(getProject());
22+
23+
// static
24+
assertTrue(types.stream().anyMatch(lookupElement -> "id".equals(lookupElement.getLookupString())));
25+
assertTrue(types.stream().anyMatch(lookupElement -> "array".equals(lookupElement.getLookupString())));
26+
27+
// Types
28+
assertTrue(types.stream().anyMatch(lookupElement -> "datetime_immutable".equals(lookupElement.getLookupString())));
29+
30+
// Type by name
31+
assertTrue(types.stream().anyMatch(lookupElement -> "my_type".equals(lookupElement.getLookupString())));
32+
}
33+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Types
6+
{
7+
abstract class Type {}
8+
}
9+
10+
namespace App {
11+
class MyType extends \Doctrine\DBAL\Types\Type
12+
{
13+
public function getName()
14+
{
15+
return 'my_type';
16+
}
17+
}
18+
}
19+
20+
21+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Types;
6+
7+
/**
8+
* Default built-in types provided by Doctrine DBAL.
9+
*/
10+
final class Types
11+
{
12+
public const ASCII_STRING = 'ascii_string';
13+
public const BIGINT = 'bigint';
14+
public const BINARY = 'binary';
15+
public const BLOB = 'blob';
16+
public const BOOLEAN = 'boolean';
17+
public const DATE_MUTABLE = 'date';
18+
public const DATE_IMMUTABLE = 'date_immutable';
19+
public const DATEINTERVAL = 'dateinterval';
20+
public const DATETIME_MUTABLE = 'datetime';
21+
public const DATETIME_IMMUTABLE = 'datetime_immutable';
22+
public const DATETIMETZ_MUTABLE = 'datetimetz';
23+
public const DATETIMETZ_IMMUTABLE = 'datetimetz_immutable';
24+
public const DECIMAL = 'decimal';
25+
public const NUMBER = 'number';
26+
public const FLOAT = 'float';
27+
public const ENUM = 'enum';
28+
public const GUID = 'guid';
29+
public const INTEGER = 'integer';
30+
public const JSON = 'json';
31+
public const JSONB = 'jsonb';
32+
public const SIMPLE_ARRAY = 'simple_array';
33+
public const SMALLFLOAT = 'smallfloat';
34+
public const SMALLINT = 'smallint';
35+
public const STRING = 'string';
36+
public const TEXT = 'text';
37+
public const TIME_MUTABLE = 'time';
38+
public const TIME_IMMUTABLE = 'time_immutable';
39+
40+
/** @codeCoverageIgnore */
41+
private function __construct()
42+
{
43+
}
44+
}

0 commit comments

Comments
 (0)