File tree Expand file tree Collapse file tree 3 files changed +84
-0
lines changed
src/main/php/lang/ast/types Expand file tree Collapse file tree 3 files changed +84
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php namespace lang \ast \types ;
2
+
3
+ use lang \ast \nodes \{EnumCase , Property };
4
+
5
+ class Declaration implements Type {
6
+ private $ type , $ result ;
7
+
8
+ /**
9
+ * @param lang.ast.nodes.TypeDeclaration $type
10
+ * @param lang.ast.Result $result
11
+ */
12
+ public function __construct ($ type , $ result ) {
13
+ $ this ->type = $ type ;
14
+ $ this ->result = $ result ;
15
+ }
16
+
17
+ /** @return string */
18
+ public function name () { return ltrim ($ this ->type ->name , '\\' ); }
19
+
20
+ /**
21
+ * Returns whether a given member is an enum case
22
+ *
23
+ * @param string $member
24
+ * @return bool
25
+ */
26
+ public function isEnumCase ($ member ) {
27
+ if ('enum ' === $ this ->type ->kind ) {
28
+ return ($ this ->type ->body [$ member ] ?? null ) instanceof EnumCase;
29
+ } else if ('class ' === $ this ->type ->kind && '\\lang \\Enum ' === $ this ->type ->parent ) {
30
+ return ($ this ->type ->body ['$ ' .$ member ] ?? null ) instanceof Property;
31
+ }
32
+ return false ;
33
+ }
34
+ }
Original file line number Diff line number Diff line change
1
+ <?php namespace lang \ast \types ;
2
+
3
+ use lang \{Enum , ClassNotFoundException };
4
+
5
+ class Reflection implements Type {
6
+ private $ reflect ;
7
+
8
+ /** @param string $type */
9
+ public function __construct ($ type ) {
10
+ try {
11
+ $ this ->reflect = new \ReflectionClass ($ type );
12
+ } catch (\ReflectionException $ e ) {
13
+ throw new ClassNotFoundException ($ type );
14
+ }
15
+ }
16
+
17
+ /** @return string */
18
+ public function name () { return $ this ->reflect ->name ; }
19
+
20
+ /**
21
+ * Returns whether a given member is an enum case
22
+ *
23
+ * @param string $member
24
+ * @return bool
25
+ */
26
+ public function isEnumCase ($ member ) {
27
+ if ($ this ->reflect ->isSubclassOf (Enum::class)) {
28
+ return $ this ->reflect ->getStaticPropertyValue ($ member , null ) instanceof Enum;
29
+ } else if ($ this ->reflect ->isSubclassOf (\UnitEnum::class)) {
30
+ $ value = $ this ->reflect ->getConstant ($ member ) ?: $ this ->reflect ->getStaticPropertyValue ($ member , null );
31
+ return $ value instanceof \UnitEnum;
32
+ }
33
+ return false ;
34
+ }
35
+ }
Original file line number Diff line number Diff line change
1
+ <?php namespace lang \ast \types ;
2
+
3
+ interface Type {
4
+
5
+ /** @return string */
6
+ public function name ();
7
+
8
+ /**
9
+ * Returns whether a given member is an enum case
10
+ *
11
+ * @param string $member
12
+ * @return bool
13
+ */
14
+ public function isEnumCase ($ member );
15
+ }
You can’t perform that action at this time.
0 commit comments