Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

[WIP][Zend\Code][ZF2-63] solve ralph's puzzle #1129

Closed
wants to merge 1 commit into from

2 participants

@sasezaki

For sensible contributors - this is NOT status for review yet.

currently, this PR's commits will only pass test-suite.

I will update this area for what's changed (required).

todos

  • refactoring marked as "Not refactored yet"
  • complete skipped tests
  • CodeGenerator -> Code\Generator
  • more specific tests

related issue link
http://framework.zend.com/issues/browse/ZF2-63
testcode coverage
http://futanari.phper.jp/zf2/cov/pr-1129/20120502/home_kazusuke_dev_sasezaki_zf2.html

@sasezaki sasezaki closed this
@sasezaki

Now, this changes should be re-change in another commit. so close this.

@Maks3w
Collaborator

I will try to rescue this job to do a new PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 2, 2012
  1. @sasezaki
This page is out of date. Refresh to see the latest.
View
58 library/Zend/Code/Generator/ClassGenerator.php
@@ -139,7 +139,7 @@ public static function fromReflection(ClassReflection $classReflection)
$methods = array();
foreach ($classReflection->getMethods() as $reflectionMethod) {
/* @var $reflectionMethod \MethodReflection\Code\Reflection\ReflectionMethod */
- if ($reflectionMethod->getDeclaringClass()->getName() == $cg->getName()) {
+ if ($reflectionMethod->getDeclaringClass()->getName() == $cg->getNamespaceName()."\\".$cg->getName()) {
$methods[] = MethodGenerator::fromReflection($reflectionMethod);
}
}
@@ -195,14 +195,10 @@ public static function fromArray(array $array)
$cg->setImplementedInterfaces($value);
break;
case 'properties':
- foreach ($value as $pValue) {
- $cg->setProperty((!$pValue instanceof PropertyGenerator) ?: PropertyGenerator::fromArray($pValue));
- }
+ $cg->setProperties($value);
break;
case 'methods':
- foreach ($value as $mValue) {
- $cg->setMethod((!$mValue instanceof MethodGenerator) ?: MethodGenerator::fromArray($mValue));
- }
+ $cg->setMethods($value);
break;
}
}
@@ -458,19 +454,41 @@ public function getImplementedInterfaces()
public function setProperties(array $properties)
{
foreach ($properties as $property) {
- $this->setProperty($property);
+ if ($property instanceof PropertyGenerator) {
+ $this->setProperty($property);
+ } else {
+ if (is_string($property)) {
+ $this->addProperty($property);
+ } else if (is_array($property)) {
+ call_user_func_array(array($this, 'addProperty'), $property);
+ }
+ }
}
return $this;
}
/**
+ * @see
+ */
+ public function addProperty($name, $defaultValue = null, $flags = PropertyGenerator::FLAG_PUBLIC)
+ {
+ if (!is_string($name)) {
+ throw new Exception\InvalidArgumentException('setProperty() expects either a string or an instance of Zend\Code\Generator\PropertyGenerator');
+ }
+
+ $property = new PropertyGenerator($name, $defaultValue, $flags);
+ $this->setProperty($property);
+ }
+
+ /**
* setProperty()
*
* @param PropertyGenerator $property
* @return ClassGenerator
*/
public function setProperty(PropertyGenerator $property)
+ //public function addPropertyGenerator(PropertyGenerator $property)
{
//if (is_string($property)) {
// $property = new PropertyGenerator($property);
@@ -534,18 +552,40 @@ public function hasProperty($propertyName)
public function setMethods(array $methods)
{
foreach ($methods as $method) {
- $this->setMethod($method);
+ if ($method instanceof MethodGenerator) {
+ $this->setMethod($method);
+ } else {
+ if (is_string($method)) {
+ $this->addMethod($method);
+ } else if (is_array($method)){
+ call_user_func_array(array($this, 'addMethod'), $method);
+ }
+ }
}
return $this;
}
/**
+ * Add Method from scalars
+ */
+ public function addMethod($name = null, array $parameters = array(), $flags = MethodGenerator::FLAG_PUBLIC, $body = null, $docblock = null)
+ {
+ if (!is_string($name)) {
+ throw new Exception\InvalidArgumentException('setMethod() expects either a string method name or an instance of Zend\Code\Generator\MethodGenerator');
+ }
+ $method = new MethodGenerator($name, $parameters, $flags, $body, $docblock);
+ $this->setMethod($method);
+ }
+
+
+ /**
* setMethod()
*
* @param array|\MethodGenerator\Code\Generator\PhpMethod $method
* @return \ClassGenerator\Code\Generator\PhpClass
*/
public function setMethod(MethodGenerator $method)
+ //public function setMethodGenerator(MethodGenerator $method)
{
//if (is_string($method)) {
// $method = new MethodGenerator($method);
View
2  library/Zend/Code/Generator/Docblock/Tag.php
@@ -21,7 +21,7 @@
namespace Zend\Code\Generator\Docblock;
-use Zend\Code\Reflection\ReflectionDocblockTag,
+use Zend\Code\Reflection\Docblock\Tag as ReflectionDocblockTag,
Zend\Code\Generator\AbstractGenerator;
/**
View
15 library/Zend/Code/Generator/FileGenerator.php
@@ -115,6 +115,7 @@ public static function fromReflection(FileReflection $fileReflection)
/* @var $class \Zend\Code\Reflection\ReflectionClass */
$phpClass = ClassGenerator::fromReflection($class);
$phpClass->setContainingFileGenerator($file);
+
$file->setClass($phpClass);
$classStartLine = $class->getStartLine(true);
$classEndLine = $class->getEndLine();
@@ -140,8 +141,9 @@ public static function fromReflection(FileReflection $fileReflection)
}
$namespace = $fileReflection->getNamespace();
+
if ($namespace != '') {
- $file->setNamespace($fileReflection->getNamespace());
+ $file->setNamespace($namespace);
}
$uses = $fileReflection->getUses();
@@ -480,7 +482,13 @@ public function generate()
// if there are markers, put the body into the output
if (preg_match('#/\* Zend_CodeGenerator_Php_File-(.*?)Marker:#', $body)) {
- $output .= $body;
+ $tokens = token_get_all($body);
+ foreach($tokens as $token) {
+ if (is_array($token) && in_array($token[0], array(T_OPEN_TAG, T_COMMENT, T_DOC_COMMENT, T_WHITESPACE))) {
+ $output .= $token[1];
+ }
+ }
+ //$output .= $body;
$body = '';
}
@@ -537,6 +545,9 @@ public function generate()
if (preg_match('#'.$regex.'#', $output)) {
$output = preg_replace('#'.$regex.'#', $class->generate(), $output, 1);
} else {
+ if ($namespace) {
+ $class->setNamespaceName(null);
+ }
$output .= $class->generate() . self::LINE_FEED;
}
}
View
3  library/Zend/Code/Reflection/FileReflection.php
@@ -266,7 +266,8 @@ public function getClass($name = null)
*/
public function getContents()
{
- return $this->contents;
+ return file_get_contents($this->filePath);
+ //return $this->contents;
}
public function toString()
View
7 library/Zend/Code/Scanner/CachingFileScanner.php
@@ -29,6 +29,11 @@ public function __construct($file, AnnotationManager $annotationManager = null)
}
}
+ public static function clearCache()
+ {
+ static::$cache = array();
+ }
+
public function getAnnotationManager()
{
return $this->fileScanner->getAnnotationManager();
@@ -88,4 +93,4 @@ public function getFunctions()
{
return $this->fileScanner->getFunctions();
}
-}
+}
View
34 tests/Zend/Code/Generator/ClassGeneratorTest.php
@@ -91,27 +91,27 @@ public function testPropertyAccessors()
$properties = $classGenerator->getProperties();
$this->assertEquals(count($properties), 2);
- $this->assertInstanceOf('PropertyGenerator', current($properties));
+ $this->assertInstanceOf('Zend\Code\Generator\PropertyGenerator', current($properties));
$property = $classGenerator->getProperty('propTwo');
- $this->assertInstanceOf('PropertyGenerator', $property);
+ $this->assertInstanceOf('Zend\Code\Generator\PropertyGenerator', $property);
$this->assertEquals($property->getName(), 'propTwo');
// add a new property
- $classGenerator->setProperty('prop3');
+ $classGenerator->addProperty('prop3');
$this->assertEquals(count($classGenerator->getProperties()), 3);
}
public function testSetPropertyAlreadyExistsThrowsException()
{
$classGenerator = new ClassGenerator();
- $classGenerator->setProperty('prop3');
+ $classGenerator->addProperty('prop3');
$this->setExpectedException(
'Zend\Code\Generator\Exception\InvalidArgumentException',
'A property by name prop3 already exists in this class'
);
- $classGenerator->setProperty('prop3');
+ $classGenerator->addProperty('prop3');
}
public function testSetPropertyNoArrayOrPropertyThrowsException()
@@ -122,7 +122,7 @@ public function testSetPropertyNoArrayOrPropertyThrowsException()
'Zend\Code\Generator\Exception\InvalidArgumentException',
'setProperty() expects either a string or an instance of Zend\Code\Generator\PropertyGenerator'
);
- $classGenerator->setProperty(true);
+ $classGenerator->addProperty(true);
}
public function testMethodAccessors()
@@ -142,7 +142,7 @@ public function testMethodAccessors()
$this->assertEquals($method->getName(), 'methodOne');
// add a new property
- $classGenerator->setMethod('methodThree');
+ $classGenerator->addMethod('methodThree');
$this->assertEquals(count($classGenerator->getMethods()), 3);
}
@@ -155,7 +155,7 @@ public function testSetMethodNoMethodOrArrayThrowsException()
'setMethod() expects either a string method name or an instance of Zend\Code\Generator\MethodGenerator'
);
- $classGenerator->setMethod(true);
+ $classGenerator->addMethod(true);
}
public function testSetMethodNameAlreadyExistsThrowsException()
@@ -220,6 +220,24 @@ public function testToString()
),
));
+
+ $classGenerator = ClassGenerator::fromArray(
+ array(
+ 'name' => 'SampleClass',
+ //'abstract' => true,
+ 'flags' => ClassGenerator::FLAG_ABSTRACT,
+ 'name' => 'SampleClass',
+ 'extendedClass' => 'ExtendedClassName',
+ 'implementedInterfaces' => array('Iterator', 'Traversable'),
+ 'properties' => array('foo',
+ array('name' => 'bar')
+ ),
+ 'methods' => array(
+ array('name' => 'baz')
+ ),
+ ));
+
+
$expectedOutput = <<<EOS
abstract class SampleClass extends ExtendedClassName implements Iterator, Traversable
{
View
33 tests/Zend/Code/Generator/FileGeneratorTest.php
@@ -20,7 +20,8 @@
*/
namespace ZendTest\Code\Generator;
-use Zend\Code\Generator\FileGenerator,
+use Zend\Code\Generator\ClassGenerator,
+ Zend\Code\Generator\FileGenerator,
Zend\Code\Reflection\FileReflection;
/**
@@ -59,10 +60,10 @@ public function testIndentationGetterAndSetter()
public function testToString()
{
- $codeGenFile = new FileGenerator(array(
+ $codeGenFile = FileGenerator::fromArray(array(
'requiredFiles' => array('SampleClass.php'),
'class' => array(
- 'abstract' => true,
+ 'flags' => ClassGenerator::FLAG_ABSTRACT,
'name' => 'SampleClass',
'extendedClass' => 'ExtendedClassName',
'implementedInterfaces' => array('Iterator', 'Traversable')
@@ -92,7 +93,7 @@ public function testFromReflection()
{
$tempFile = tempnam(sys_get_temp_dir(), 'UnitFile');
- $codeGenFile = new FileGenerator(array(
+ $codeGenFile = FileGenerator::fromArray(array(
'class' => array(
'name' => 'SampleClass'
)
@@ -117,9 +118,9 @@ public function testFromFileReflection()
$file = __DIR__ . '/TestAsset/TestSampleSingleClass.php';
require_once $file;
- $codeGenFileFromDisk = FileGenerator::fromReflection(new FileReflection($file));
+ $codeGenFileFromDisk = FileGenerator::fromReflection($fileRefl = new FileReflection($file));
- $codeGenFileFromDisk->getClass()->setMethod(array('name' => 'foobar'));
+ $codeGenFileFromDisk->getClass()->addMethod('foobar');
$expectedOutput = <<<EOS
<?php
@@ -127,25 +128,29 @@ public function testFromFileReflection()
* File header here
*
* @author Ralph Schindler <ralph.schindler@zend.com>
- *
*/
+
+
+/* Zend_CodeGenerator_Php_File-ClassMarker: {ZendTest\Code\Generator\TestAsset\TestSampleSingleClass} */
+
+
namespace ZendTest\Code\Generator\TestAsset;
/**
* class docblock
- *
+ *
* @package Zend_Reflection_TestSampleSingleClass
- *
+ *
*/
class TestSampleSingleClass
{
/**
* Enter description here...
- *
+ *
* @return bool
- *
+ *
*/
public function someMethod()
{
@@ -161,12 +166,14 @@ public function foobar()
EOS;
+ //echo $codeGenFileFromDisk->generate();die;
$this->assertEquals($expectedOutput, $codeGenFileFromDisk->generate());
}
public function testFileLineEndingsAreAlwaysLineFeed()
{
- $codeGenFile = new FileGenerator(array(
+ //$codeGenFile = new FileGenerator(array(
+ $codeGenFile = FileGenerator::fromArray(array(
'requiredFiles' => array('SampleClass.php'),
'class' => array(
'abstract' => true,
@@ -177,7 +184,7 @@ public function testFileLineEndingsAreAlwaysLineFeed()
));
// explode by newline, this would leave CF in place if it were generated
- $lines = explode("\n", $codeGenFile);
+ $lines = explode("\n", $codeGenFile->generate());
$targetLength = strlen('require_once \'SampleClass.php\';');
$this->assertEquals($targetLength, strlen($lines[2]));
View
4 tests/Zend/Code/Scanner/CachingFileScannerTest.php
@@ -9,6 +9,8 @@ class CachingFileScannerTest extends \PHPUnit_Framework_TestCase
{
public function testCachingFileScannerWillUseSameInternalFileScannerWithMatchingFileNameAnAnnotationManagerObject()
{
+ CachingFileScanner::clearCache();
+
// single entry, based on file
$cfs1 = new CachingFileScanner(__DIR__ . '/../TestAsset/BarClass.php');
$this->assertContains('ZendTest\Code\TestAsset\BarClass', $cfs1->getClassNames());
@@ -68,4 +70,4 @@ protected function assertDifferentInternalFileScanner(CachingFileScanner $one, C
$this->assertNotSame($fileScannerPropOne->getValue($one), $fileScannerPropTwo->getValue($two));
}
-}
+}
Something went wrong with that request. Please try again.