Skip to content

Commit 327a40e

Browse files
committed
Add missing lang.ast.types package
1 parent b1afdae commit 327a40e

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}

0 commit comments

Comments
 (0)