Skip to content
This repository has been archived by the owner on Dec 26, 2021. It is now read-only.

Commit

Permalink
Merge branch 'TokenOffsets'
Browse files Browse the repository at this point in the history
  • Loading branch information
llaville committed Oct 6, 2014
2 parents 2321ae1 + b6ad650 commit bae9de8
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Bartlett/Reflect.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
use Bartlett\Reflect\ManagerInterface;
use Bartlett\Reflect\ProviderManager;
use Bartlett\Reflect\Builder;
use Bartlett\Reflect\PhpParser\Lexer\TokenOffsets;

use PhpParser\Parser;
use PhpParser\Lexer;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;

Expand Down Expand Up @@ -82,7 +82,8 @@ public function parse(array $providers = null)
{
$this->builder = new Builder;

$parser = new Parser(new Lexer\Emulative);
$lexer = new TokenOffsets();
$parser = new Parser($lexer);
$traverser = new NodeTraverser;
$traverser->addVisitor(new NameResolver);
$traverser->addVisitor($this->builder);
Expand Down Expand Up @@ -113,6 +114,10 @@ public function parse(array $providers = null)
$this->builder->setCurrentFile($file->getPathname());

if (isset($event['notModified'])) {
$tokens = @token_get_all(
file_get_contents($file->getPathname())
);
$this->builder->setTokens($tokens);
// uses cached response (AST built by PHP-Parser)
$stmts = $traverser->traverse($event['notModified']);

Expand All @@ -131,6 +136,7 @@ public function parse(array $providers = null)
$stmts = $parser->parse(
file_get_contents($file->getPathname())
);
$this->builder->setTokens($lexer->getTokens());
$stmts = $traverser->traverse($stmts);

$this->dispatch(
Expand Down
39 changes: 39 additions & 0 deletions src/Bartlett/Reflect/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

use PhpParser\NodeVisitorAbstract;
use PhpParser\Node;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\ClassMethod;

/**
* Concrete Builder.
Expand Down Expand Up @@ -56,6 +58,34 @@ class Builder extends NodeVisitorAbstract
*/
private $namespace;

private $tokens;

private function isImplicitlyPublicProperty(array $tokens, Property $prop)
{
$i = $prop->getAttribute('startOffset');
return isset($tokens[$i]) && $tokens[$i][0] == T_VAR;
}

private function isImplicitlyPublicFunction(array $tokens, ClassMethod $method)
{
$i = $method->getAttribute('startOffset');
for ($c = count($tokens); $i < $c; ++$i) {
$t = $tokens[$i];
if ($t[0] == T_PUBLIC || $t[0] == T_PROTECTED || $t[0] == T_PRIVATE) {
return false;
}
if ($t[0] == T_FUNCTION) {
break;
}
}
return true;
}

public function setTokens(array $tokens)
{
$this->tokens = $tokens;
}

public function setCurrentFile($path)
{
$this->file = $path;
Expand Down Expand Up @@ -312,6 +342,15 @@ public function leaveNode(Node $node)
if (is_string($attr = $visibility($stmt))) {
$stmtAttributes['visibility'] = $attr;
}
if ($stmt instanceof Property) {
$stmtAttributes['implicitlyPublic'] =
$this->isImplicitlyPublicProperty($this->tokens, $stmt);

}
if ($stmt instanceof ClassMethod) {
$stmtAttributes['implicitlyPublic'] =
$this->isImplicitlyPublicFunction($this->tokens, $stmt);
}

if ($stmt instanceof \PhpParser\Node\Stmt\ClassConst) {
// @link http://www.php.net/manual/en/language.oop5.constants.php
Expand Down
11 changes: 11 additions & 0 deletions src/Bartlett/Reflect/Model/MethodModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function __construct($class, $name, $attributes)
{
$struct = array(
'modifiers' => array(),
'implicitlyPublic' => true,
);
$struct = array_merge($struct, $attributes);
parent::__construct($struct);
Expand Down Expand Up @@ -142,6 +143,16 @@ public function isPublic()
return $this->struct['visibility'] === 'public';
}

/**
* Checks if the method is implicitly public (PHP4 syntax).
*
* @return bool
*/
public function isImplicitlyPublic()
{
return $this->struct['implicitlyPublic'];
}

/**
* Returns the string representation of the MethodModel object.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Bartlett/Reflect/Model/PropertyModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function __construct($class, $name, $attributes)
'compileTime' => true,
'modifiers' => array(),
'visibility' => 'public',
'implicitlyPublic' => true,
);
$struct = array_merge($struct, $attributes);
parent::__construct($struct);
Expand Down Expand Up @@ -143,6 +144,16 @@ public function isStatic()
return in_array('static', $this->struct['modifiers']);
}

/**
* Checks if the property is implicitly public (PHP4 syntax).
*
* @return bool
*/
public function isImplicitlyPublic()
{
return $this->struct['implicitlyPublic'];
}

/**
* Returns the string representation of the PropertyModel object.
*
Expand Down
28 changes: 28 additions & 0 deletions src/Bartlett/Reflect/PhpParser/Lexer/TokenOffsets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Bartlett\Reflect\PhpParser\Lexer;

use PhpParser\Lexer\Emulative;

/**
*
*
* @author Nikita Popov
* @author Christoph M. Becker
* @link https://gist.github.com/nikic/04fce01e69ae5b7b44f8
* @link https://github.com/nikic/PHP-Parser/issues/136
*/

class TokenOffsets extends \PhpParser\Lexer\Emulative
{
public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null)
{
$token = parent::getNextToken($value, $startAttributes, $endAttributes);
$startAttributes['startOffset'] = $endAttributes['endOffset'] = $this->pos;
return $token;
}

public function getTokens() {
return $this->tokens;
}
}

0 comments on commit bae9de8

Please sign in to comment.