Skip to content

Commit

Permalink
IR: Support class and interface constants.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Apr 26, 2017
1 parent 0c2de13 commit ad3a722
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
48 changes: 46 additions & 2 deletions src/Compiler/IntermediateRepresentation/Into.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@
use Kitab\Compiler\Parser;
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
use PhpParser\PrettyPrinter;

class Into extends NodeVisitorAbstract
{
protected $_file = null;
protected $_file = null;
private $_prettyPrinter = null;

public function __construct()
{
$this->_file = new File();
$this->_file = new File();
$this->_prettyPrinter = new PrettyPrinter\Standard();
}

public function enterNode(Node $node)
Expand All @@ -55,6 +58,7 @@ public function enterNode(Node $node)
$classNode = $node;
$class = new Class_($classNode->namespacedName->toString());
$class->documentation = Parser::extractFromComment($classNode->getDocComment());
$class->constants = $this->intoConstants($classNode);
$class->methods = $this->intoMethods($classNode);

if ($classNode->flags & Node\Stmt\Class_::MODIFIER_ABSTRACT) {
Expand All @@ -78,6 +82,7 @@ public function enterNode(Node $node)
$interfaceNode = $node;
$interface = new Interface_($interfaceNode->namespacedName->toString());
$interface->documentation = Parser::extractFromComment($interfaceNode->getDocComment());
$interface->constants = $this->intoConstants($interfaceNode);
$interface->methods = $this->intoMethods($interfaceNode);

if (!empty($interfaceNode->extends)) {
Expand Down Expand Up @@ -106,6 +111,45 @@ function ($nodeName) {
return;
}

protected function intoConstants(Node\Stmt\ClassLike $node): array
{
$constants = [];

foreach ($node->stmts as $statement) {
if (!($statement instanceof Node\Stmt\ClassConst)) {
continue;
}

$defaultDocumentation = Parser::extractFromComment($statement->getDocComment());

if (true === $statement->isPublic()) {
$visibility = Constant::VISIBILITY_PUBLIC;
} else if (true === $statement->isProtected()) {
$visibility = Constant::VISIBILITY_PROTECTED;
} else {
$visibility = Constant::VISIBILITY_PRIVATE;
}

foreach ($statement->consts as $constantNode) {
$constant = new Constant($constantNode->name);
$constant->visibility = $visibility;
$constant->value = $this->_prettyPrinter->prettyPrint([$constantNode->value]);

$documentation = Parser::extractFromComment($constantNode->getDocComment());

if (empty($documentation)) {
$constant->documentation = $defaultDocumentation;
} else {
$constant->documentation = $documentation;
}

$constants[] = $constant;
}
}

return $constants;
}

protected function intoMethods(Node\Stmt\ClassLike $node): array
{
$methods = [];
Expand Down
11 changes: 11 additions & 0 deletions src/Compiler/IntermediateRepresentation/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,19 @@

class Method
{
/**
* Represent a public constant.
*/
const VISIBILITY_PUBLIC = 0;

/**
* Represent a protected constant.
*/
const VISIBILITY_PROTECTED = 1;

/**
* Represent a private constant.
*/
const VISIBILITY_PRIVATE = 2;

public $visibility = self::VISIBILITY_PUBLIC;
Expand Down
3 changes: 3 additions & 0 deletions src/Compiler/Target/Html/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@

class Search
{
/**
* Represent the path to the database file.
*/
const DATABASE_FILE = 'hoa://Kitab/Output/javascript/search-database.json';

private static $_database = null;
Expand Down
9 changes: 9 additions & 0 deletions src/Compiler/Target/Html/Template/Partial/Class.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ <h1>Class <code><?php echo $data->class->name ?></code></h1>

<?php if (!empty($data->class->constants)): ?>
<h2 id="constants">Constants</h2>

<dl>
<?php foreach ($data->class->constants as $constant): ?>
<dt aria-label="<?php echo $constant->name ?>"><code class="language-php"><?php echo $constant ?></code></dt>
<dd>
<section><?php echo $this->markdownToHtml($constant->documentation) ?></section>
</dd>
<?php endforeach ?>
</dl>
<?php endif ?>

<?php if (!empty($data->class->attributes)): ?>
Expand Down
9 changes: 9 additions & 0 deletions src/Compiler/Target/Html/Template/Partial/Interface.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ <h1>Interface <code><?php echo $data->interface->name ?></code></h1>

<?php if (!empty($data->interface->constants)): ?>
<h2 id="constants">Constants</h2>

<dl>
<?php foreach ($data->interface->constants as $constant): ?>
<dt aria-label="<?php echo $constant->name ?>"><code class="language-php"><?php echo $constant ?></code></dt>
<dd>
<section><?php echo $this->markdownToHtml($constant->documentation) ?></section>
</dd>
<?php endforeach ?>
</dl>
<?php endif ?>

<?php if (!empty($data->interface->methods)): ?>
Expand Down

0 comments on commit ad3a722

Please sign in to comment.