Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/5400' into develop
Browse files Browse the repository at this point in the history
Close #5400
  • Loading branch information
weierophinney committed Nov 5, 2013
2 parents ef29e42 + 556adc6 commit 583b876
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 48 deletions.
128 changes: 80 additions & 48 deletions library/Zend/Code/Scanner/PropertyScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@

class PropertyScanner implements ScannerInterface
{
const T_BOOLEAN = "boolean";
const T_INTEGER = "int";
const T_STRING = "string";
const T_ARRAY = "array";
const T_UNKNOWN = "unknown";

/**
* @var bool
*/
Expand Down Expand Up @@ -80,6 +86,11 @@ class PropertyScanner implements ScannerInterface
*/
protected $value;

/**
* @var string
*/
protected $valueType;

/**
* Constructor
*
Expand Down Expand Up @@ -125,6 +136,14 @@ public function getName()
return $this->name;
}

/**
* @return string
*/
public function getValueType()
{
return $this->valueType;
}

/**
* @return bool
*/
Expand Down Expand Up @@ -219,66 +238,79 @@ protected function scan()
/**
* Variables & Setup
*/
$tokens = &$this->tokens;
$value = '';
$concatenateValue = false;

$tokens = &$this->tokens;
reset($tokens);

SCANNER_TOP:
foreach($tokens as $token) {
$tempValue = $token;
if (!is_string($token)) {
list($tokenType, $tokenContent, $tokenLine) = $token;

$token = current($tokens);

if (!is_string($token)) {
list($tokenType, $tokenContent, $tokenLine) = $token;

switch ($tokenType) {
case T_DOC_COMMENT:
if ($this->docComment === null && $this->name === null) {
$this->docComment = $tokenContent;
}
goto SCANNER_CONTINUE;

case T_VARIABLE:
$this->name = ltrim($tokenContent, '$');
goto SCANNER_CONTINUE;

case T_PUBLIC:
// use defaults
goto SCANNER_CONTINUE;

case T_PROTECTED:
$this->isProtected = true;
$this->isPublic = false;
goto SCANNER_CONTINUE;
switch ($tokenType) {
case T_DOC_COMMENT:
if ($this->docComment === null && $this->name === null) {
$this->docComment = $tokenContent;
}
break;

case T_VARIABLE:
$this->name = ltrim($tokenContent, '$');
break;

case T_PUBLIC:
// use defaults
break;

case T_PROTECTED:
$this->isProtected = true;
$this->isPublic = false;
break;

case T_PRIVATE:
$this->isPrivate = true;
$this->isPublic = false;
break;

case T_STATIC:
$this->isStatic = true;
break;
default:
$tempValue = trim($tokenContent);
break;
}
}

case T_PRIVATE:
$this->isPrivate = true;
$this->isPublic = false;
goto SCANNER_CONTINUE;
//end value concatenation
if(!is_array($token) && trim($token) == ";") {
$concatenateValue = false;
}

case T_STATIC:
$this->isStatic = true;
goto SCANNER_CONTINUE;
if(true === $concatenateValue) {
$value .= $tempValue;
}

default:
if ($this->name !== null && trim($tokenContent) !== '') {
$this->value .= (is_string($token)) ? $token : $tokenContent;
if (substr($this->value, 0, 1) === '"' || substr($this->value, 0, 1) === "'") {
$this->value = substr($this->value, 1, -1); // Remove quotes
}
}
goto SCANNER_CONTINUE;
//start value concatenation
if(!is_array($token) && trim($token) == "=") {
$concatenateValue = true;
}
}

SCANNER_CONTINUE:

if (next($this->tokens) === false) {
goto SCANNER_END;
$this->valueType = self::T_UNKNOWN;
if($value == "false" || $value == "true") {
$this->valueType = self::T_BOOLEAN;
} elseif(is_numeric($value)) {
$this->valueType = self::T_INTEGER;
} elseif(0 === strpos($value, 'array') || 0 === strpos($value, "[")) {
$this->valueType = self::T_ARRAY;
} elseif (substr($value, 0, 1) === '"' || substr($value, 0, 1) === "'") {
$value = substr($value, 1, -1); // Remove quotes
$this->valueType = self::T_STRING;
}
goto SCANNER_TOP;

SCANNER_END:

$this->value = empty($value) ? null : $value;
$this->isScanned = true;
}
}
57 changes: 57 additions & 0 deletions tests/ZendTest/Code/Scanner/PropertyScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace ZendTest\Code\Scanner;

use Zend\Code\Scanner\FileScanner;
use Zend\Code\Scanner\TokenArrayScanner;
use PHPUnit_Framework_TestCase as TestCase;

class PropertyScannerTest extends TestCase
Expand Down Expand Up @@ -43,4 +44,60 @@ public function testPropertyScannerHasPropertyInformation()
$this->assertTrue($property->isPrivate());
$this->assertFalse($property->isStatic());
}

/**
* @group 5384
*/
public function testPropertyScannerReturnsProperValue()
{
$class = <<<'CLASS'
<?php
class Foo
{
protected $empty;
private $string = 'string';
private $int = 123;
private $array = array('test' => 2,2);
private $arraynew = ['test' => 2,2];
private $notarray = "['test' => 2,2]";
private $status = false;
}
CLASS;

$tokenScanner = new TokenArrayScanner(token_get_all($class));
$fooClass = $tokenScanner->getClass('Foo');
foreach ($fooClass->getProperties() as $property) {
$value = $property->getValue();
$valueType = $property->getValueType();
switch($property->getName()) {
case "empty":
$this->assertNull($value);
$this->assertEquals('unknown', $valueType);
break;
case "string":
$this->assertEquals('string', $value);
$this->assertEquals('string', $valueType);
break;
case "int":
$this->assertEquals('123', $value);
$this->assertEquals('int', $valueType);
break;
case "array":
$this->assertEquals("array('test'=>2,2)", $value);
$this->assertEquals('array', $valueType);
break;
case "arraynew":
$this->assertEquals("['test'=>2,2]", $value);
$this->assertEquals('array', $valueType);
break;
case "notarray":
$this->assertEquals('string', $valueType);
break;
case "status":
$this->assertTrue("false" === $value);
$this->assertEquals('boolean', $valueType);
break;
}
}
}
}

0 comments on commit 583b876

Please sign in to comment.