From 6ec60f54a6a6728809487e1485bcd01e5f469b25 Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 23 Jul 2015 14:40:05 +0200 Subject: [PATCH 01/30] added interface generator and tests --- source/CodeGenerator/InterfaceBlock.php | 214 ++++++++++++++++++ source/CodeGenerator/InterfaceMethodBlock.php | 43 ++++ source/CodeGenerator/ParameterBlock.php | 1 - tests/mocks/MockInterface.php | 12 + tests/mocks/MockInterfaceTwo.php | 8 + tests/source/CodeGenearator/InterfaceTest.php | 71 ++++++ 6 files changed, 348 insertions(+), 1 deletion(-) create mode 100644 source/CodeGenerator/InterfaceBlock.php create mode 100644 source/CodeGenerator/InterfaceMethodBlock.php create mode 100644 tests/mocks/MockInterface.php create mode 100644 tests/mocks/MockInterfaceTwo.php create mode 100644 tests/source/CodeGenearator/InterfaceTest.php diff --git a/source/CodeGenerator/InterfaceBlock.php b/source/CodeGenerator/InterfaceBlock.php new file mode 100644 index 0000000..064c6e6 --- /dev/null +++ b/source/CodeGenerator/InterfaceBlock.php @@ -0,0 +1,214 @@ +_name = (string)$name; + + if (!is_null($parentInterfaceNames)) { + $this->setParentInterfaceNames($parentInterfaceNames); + } + } + + /** + * @param array $parentInterfaceNames + */ + public function setParentInterfaceNames(array $parentInterfaceNames) + { + foreach ($parentInterfaceNames as $parentInterfaceName) { + $this->addParentInterfaceName($parentInterfaceName); + } + } + + /** + * @param string $parentInterfaceName + */ + public function addParentInterfaceName($parentInterfaceName) + { + $this->_parentInterfaceNames[] = (string)$parentInterfaceName; + } + + /** + * @param \ReflectionClass $reflection + * @return InterfaceBlock + */ + public static function buildFromReflection(\ReflectionClass $reflection) + { + $class = new self($reflection->getShortName()); + $class->setNamespace($reflection->getNamespaceName()); + $reflectionParentInterfaces = $reflection->getInterfaces(); + + if (!empty($reflectionParentInterfaces)) { + $interfaces = []; + foreach ($reflectionParentInterfaces as $reflectionParentInterface) { + $interfaces[] = $reflectionParentInterface->getName(); + } + $class->setParentInterfaceNames($interfaces); + } + + foreach ($reflection->getMethods() as $reflectionMethod) { + if ($reflectionMethod->getDeclaringClass() == $reflection) { + $method = InterfaceMethodBlock::buildFromReflection($reflectionMethod); + $class->addMethod($method); + } + } + + $constants = $reflection->getConstants(); + if (count($constants)) { + $parentConstants = self::getAllConstantsOfParentInterfaces($reflection); + foreach ($constants as $name => $value) { + if (!in_array($name, $parentConstants)) { + $class->addConstant(new ConstantBlock($name, $value)); + } + } + } + + return $class; + } + + /** + * @param string $namespace + */ + public function setNamespace($namespace) + { + $this->_namespace = (string)$namespace; + } + + /** + * @param InterfaceMethodBlock $method + */ + public function addMethod(InterfaceMethodBlock $method) + { + $this->_methods[$method->getName()] = $method; + } + + /** + * @param ConstantBlock $constant + */ + public function addConstant(ConstantBlock $constant) + { + $this->_constants[$constant->getName()] = $constant; + } + + /** + * @param \ReflectionClass $reflection + * @return array + */ + protected static function getAllConstantsOfParentInterfaces(\ReflectionClass $reflection) + { + $parentConstants = []; + $parentInterfaces = $reflection->getInterfaces(); + foreach ($parentInterfaces as $parentInterface) { + $parentInterfaceConstants = $parentInterface->getConstants(); + $constantNames = array_keys($parentInterfaceConstants); + $parentConstants += $constantNames; + } + + return $parentConstants; + } + + /** + * @return string + */ + public function getName() + { + return $this->_name; + } + + /** + * @return string + */ + public function dump() + { + $lines = []; + $lines[] = $this->_dumpHeader(); + foreach ($this->_constants as $constant) { + $lines[] = ''; + $lines[] = $this->_indent($constant->dump()); + } + foreach ($this->_methods as $method) { + $lines[] = ''; + $lines[] = $this->_indent($method->dump()); + } + $lines[] = $this->_dumpFooter(); + + return $this->_dumpLines($lines); + } + + /** + * @return string + */ + private function _dumpHeader() + { + $lines = []; + if ($this->_namespace) { + $lines[] = 'namespace ' . $this->_namespace . ';'; + $lines[] = ''; + } + $classDeclaration = ''; + $classDeclaration .= 'interface ' . $this->_name; + if ($this->_parentInterfaceNames) { + $classDeclaration .= ' extends ' . $this->_getParentInterfaces(); + } + $classDeclaration .= ' {'; + $lines[] = $classDeclaration; + + return $this->_dumpLines($lines); + } + + /** + * @return string + */ + private function _getParentInterfaces() + { + $cleaned = []; + foreach ($this->_parentInterfaceNames as $parentInterfaceName) { + $cleaned[] = self::_normalizeClassName($parentInterfaceName); + } + + return join(', ', $cleaned); + } + + /** + * @return string + */ + private function _dumpFooter() + { + return '}'; + } + + /** + * @param $namespacedClassOrInterfaceName + */ + public function extractConstantsFromOtherClassOrInterface($namespacedClassOrInterfaceName) + { + $reflection = new \ReflectionClass($namespacedClassOrInterfaceName); + $constants = $reflection->getConstants(); + + foreach ($constants as $name => $val) { + $this->addConstant(new ConstantBlock($name, $val)); + } + } +} diff --git a/source/CodeGenerator/InterfaceMethodBlock.php b/source/CodeGenerator/InterfaceMethodBlock.php new file mode 100644 index 0000000..d37e437 --- /dev/null +++ b/source/CodeGenerator/InterfaceMethodBlock.php @@ -0,0 +1,43 @@ +setName($name); + parent::__construct(); + } + + /** + * @return string + */ + protected function _dumpHeader() + { + return 'public ' . parent::_dumpHeader(); + } + + /** + * @return string + */ + protected function _dumpBody() + { + return ';'; + } + + /** + * @param \ReflectionMethod $reflection + * @return InterfaceMethodBlock + */ + public static function buildFromReflection(\ReflectionMethod $reflection) + { + $method = new self($reflection->getName()); + $method->extractFromReflection($reflection); + + return $method; + } +} \ No newline at end of file diff --git a/source/CodeGenerator/ParameterBlock.php b/source/CodeGenerator/ParameterBlock.php index 4e35fad..6d9e7c5 100644 --- a/source/CodeGenerator/ParameterBlock.php +++ b/source/CodeGenerator/ParameterBlock.php @@ -26,7 +26,6 @@ class ParameterBlock extends Block { * @param mixed|null $defaultValue * @param boolean|null $passedByReference * @throws \Exception - * @internal param bool|null $isOptional */ public function __construct($name, $type = null, $optional = null, $defaultValue = null, $passedByReference = null) { $this->_name = (string) $name; diff --git a/tests/mocks/MockInterface.php b/tests/mocks/MockInterface.php new file mode 100644 index 0000000..d190e57 --- /dev/null +++ b/tests/mocks/MockInterface.php @@ -0,0 +1,12 @@ +addBlock($reflectedClass); + + $actual = $file->dump(); + $expected = file_get_contents($reflectionClass->getFileName()); + $this->assertSame($expected, $actual); + } + + public function testDumpSmall() + { + $file = new FileBlock(); + + $reflectionClass = new \ReflectionClass('CodeGeneratorMocks\\MockInterfaceTwo'); + $reflectedClass = InterfaceBlock::buildFromReflection($reflectionClass); + $file->addBlock($reflectedClass); + + $actual = $file->dump(); + $expected = file_get_contents($reflectionClass->getFileName()); + $this->assertSame($expected, $actual); + } + + public function testDumpExtract() + { + $expected = <<addBlock($interface); + $interface->setNamespace('CodeGeneratorMocks'); + $interface->extractConstantsFromOtherClassOrInterface('CodeGeneratorMocks\\MockInterfaceTwo'); + $interface->addConstant(new ConstantBlock('BAR', 'test')); + + $actual = $file->dump(); + $this->assertSame($expected, $actual); + } + + public function testGetName() + { + $className = 'Foo'; + $class = new InterfaceBlock($className, ['Bar']); + $this->assertSame($className, $class->getName()); + } +} From 50218251f1bfc6dafe9064d375678d21dd5fc57b Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 23 Jul 2015 14:43:09 +0200 Subject: [PATCH 02/30] reverted change --- source/CodeGenerator/ParameterBlock.php | 1 + 1 file changed, 1 insertion(+) diff --git a/source/CodeGenerator/ParameterBlock.php b/source/CodeGenerator/ParameterBlock.php index 6d9e7c5..4e35fad 100644 --- a/source/CodeGenerator/ParameterBlock.php +++ b/source/CodeGenerator/ParameterBlock.php @@ -26,6 +26,7 @@ class ParameterBlock extends Block { * @param mixed|null $defaultValue * @param boolean|null $passedByReference * @throws \Exception + * @internal param bool|null $isOptional */ public function __construct($name, $type = null, $optional = null, $defaultValue = null, $passedByReference = null) { $this->_name = (string) $name; From 8c0af2195d25485845a167199d8505ffb0de85c1 Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 23 Jul 2015 14:46:20 +0200 Subject: [PATCH 03/30] changed array syntax to help compatibility --- source/CodeGenerator/InterfaceBlock.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/source/CodeGenerator/InterfaceBlock.php b/source/CodeGenerator/InterfaceBlock.php index 064c6e6..cd71834 100644 --- a/source/CodeGenerator/InterfaceBlock.php +++ b/source/CodeGenerator/InterfaceBlock.php @@ -11,13 +11,13 @@ class InterfaceBlock extends Block private $_namespace; /** @var array */ - private $_parentInterfaceNames = []; + private $_parentInterfaceNames = array(); /** @var ConstantBlock[] */ - private $_constants = []; + private $_constants = array(); /** @var MethodBlock[] */ - private $_methods = []; + private $_methods = array(); /** * @param $name @@ -61,7 +61,7 @@ public static function buildFromReflection(\ReflectionClass $reflection) $reflectionParentInterfaces = $reflection->getInterfaces(); if (!empty($reflectionParentInterfaces)) { - $interfaces = []; + $interfaces = array(); foreach ($reflectionParentInterfaces as $reflectionParentInterface) { $interfaces[] = $reflectionParentInterface->getName(); } @@ -118,7 +118,7 @@ public function addConstant(ConstantBlock $constant) */ protected static function getAllConstantsOfParentInterfaces(\ReflectionClass $reflection) { - $parentConstants = []; + $parentConstants = array(); $parentInterfaces = $reflection->getInterfaces(); foreach ($parentInterfaces as $parentInterface) { $parentInterfaceConstants = $parentInterface->getConstants(); @@ -142,7 +142,7 @@ public function getName() */ public function dump() { - $lines = []; + $lines = array(); $lines[] = $this->_dumpHeader(); foreach ($this->_constants as $constant) { $lines[] = ''; @@ -162,7 +162,7 @@ public function dump() */ private function _dumpHeader() { - $lines = []; + $lines = array(); if ($this->_namespace) { $lines[] = 'namespace ' . $this->_namespace . ';'; $lines[] = ''; @@ -183,7 +183,7 @@ private function _dumpHeader() */ private function _getParentInterfaces() { - $cleaned = []; + $cleaned = array(); foreach ($this->_parentInterfaceNames as $parentInterfaceName) { $cleaned[] = self::_normalizeClassName($parentInterfaceName); } From 13aafe64b917dc3997940bb0d80ca42c9c73e52c Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 23 Jul 2015 15:20:25 +0200 Subject: [PATCH 04/30] added TraitBlock and require php5.4 --- composer.json | 33 ++--- source/CodeGenerator/TraitBlock.php | 144 ++++++++++++++++++++++ tests/mocks/MockCompositeTrait.php | 8 ++ tests/mocks/MockTrait.php | 44 +++++++ tests/mocks/MockTraitTwo.php | 16 +++ tests/source/CodeGenearator/TraitTest.php | 133 ++++++++++++++++++++ 6 files changed, 363 insertions(+), 15 deletions(-) create mode 100644 source/CodeGenerator/TraitBlock.php create mode 100644 tests/mocks/MockCompositeTrait.php create mode 100644 tests/mocks/MockTrait.php create mode 100644 tests/mocks/MockTraitTwo.php create mode 100644 tests/source/CodeGenearator/TraitTest.php diff --git a/composer.json b/composer.json index 2e46d77..95a75ef 100644 --- a/composer.json +++ b/composer.json @@ -1,18 +1,21 @@ { - "name": "tomaszdurka/codegenerator", - "authors": [ - { - "name": "Tomasz Durka", - "email": "tomasz@durka.pl" + "name": "tomaszdurka/codegenerator", + "authors": [ + { + "name": "Tomasz Durka", + "email": "tomasz@durka.pl" + } + ], + "require": { + "php": ">=5.4" + }, + "require-dev": { + "phpunit/phpunit": "~3.7.10", + "satooshi/php-coveralls": "~0.6.1" + }, + "autoload": { + "psr-0": { + "CodeGenerator\\": "source/" + } } - ], - "require-dev": { - "phpunit/phpunit": "~3.7.10", - "satooshi/php-coveralls": "~0.6.1" - }, - "autoload": { - "psr-0": { - "CodeGenerator\\": "source/" - } - } } diff --git a/source/CodeGenerator/TraitBlock.php b/source/CodeGenerator/TraitBlock.php new file mode 100644 index 0000000..bfcf29f --- /dev/null +++ b/source/CodeGenerator/TraitBlock.php @@ -0,0 +1,144 @@ +_name = (string)$name; + } + + /** + * @return string + */ + public function getName() + { + return $this->_name; + } + + /** + * @param string $namespace + */ + public function setNamespace($namespace) + { + $this->_namespace = (string)$namespace; + } + + /** + * @param string $name + */ + public function addUse($name) + { + $this->_uses[] = $name; + } + + /** + * @param PropertyBlock $property + */ + public function addProperty(PropertyBlock $property) + { + $this->_properties[$property->getName()] = $property; + } + + /** + * @param MethodBlock $method + */ + public function addMethod(MethodBlock $method) + { + $this->_methods[$method->getName()] = $method; + } + + /** + * @return string + */ + public function dump() + { + $lines = []; + $lines[] = $this->_dumpHeader(); + foreach ($this->_uses as $use) { + $lines[] = ''; + $lines[] = $this->_indent("use ${use};"); + } + foreach ($this->_properties as $property) { + $lines[] = ''; + $lines[] = $this->_indent($property->dump()); + } + foreach ($this->_methods as $method) { + $lines[] = ''; + $lines[] = $this->_indent($method->dump()); + } + $lines[] = $this->_dumpFooter(); + + return $this->_dumpLines($lines); + } + + /** + * @return string + */ + private function _dumpHeader() + { + $lines = []; + if ($this->_namespace) { + $lines[] = 'namespace ' . $this->_namespace . ';'; + $lines[] = ''; + } + $classDeclaration = 'trait ' . $this->_name; + $classDeclaration .= ' {'; + $lines[] = $classDeclaration; + + return $this->_dumpLines($lines); + } + + /** + * @return string + */ + private function _dumpFooter() + { + return '}'; + } + + /** + * @param \ReflectionClass $reflection + * @return TraitBlock + */ + public static function buildFromReflection(\ReflectionClass $reflection) + { + $class = new self($reflection->getShortName()); + $class->setNamespace($reflection->getNamespaceName()); + + foreach ($reflection->getMethods() as $reflectionMethod) { + if ($reflectionMethod->getDeclaringClass() == $reflection) { + $method = MethodBlock::buildFromReflection($reflectionMethod); + $class->addMethod($method); + } + } + + foreach ($reflection->getProperties() as $reflectionProperty) { + if ($reflectionProperty->getDeclaringClass() == $reflection) { + $property = PropertyBlock::buildFromReflection($reflectionProperty); + $class->addProperty($property); + } + } + + return $class; + } +} diff --git a/tests/mocks/MockCompositeTrait.php b/tests/mocks/MockCompositeTrait.php new file mode 100644 index 0000000..8e61654 --- /dev/null +++ b/tests/mocks/MockCompositeTrait.php @@ -0,0 +1,8 @@ +foo); + } + + public function withTypeHinting(\Countable $countable, array $array, callable $callable) { + echo 1; + } + + public function defaultValues($defaultValue = null, $defaultArray = array()) { + echo 2; + } + + public function withReferenceParam(&$param) { + } + + protected function abstractMethod() { + } + + private function _foo() { + // comment + // indentation + // back + } + + public static function staticMethod() { + } +} diff --git a/tests/mocks/MockTraitTwo.php b/tests/mocks/MockTraitTwo.php new file mode 100644 index 0000000..2e9874b --- /dev/null +++ b/tests/mocks/MockTraitTwo.php @@ -0,0 +1,16 @@ +addBlock($reflectedClass); + + $actual = $file->dump(); + $expected = file_get_contents($reflectionClass->getFileName()); + $this->assertSame($expected, $actual); + } + + public function testDumpComposite() + { + /** + * Traits that contain USE are treated like the contain the methods and properties themselves + * might not be the best solution to handle them like this + */ + $file = new FileBlock(); + + $reflectionClass = new \ReflectionClass('\\CodeGeneratorMocks\\MockCompositeTrait'); + + $reflectedClass = TraitBlock::buildFromReflection($reflectionClass); + $file->addBlock($reflectedClass); + + $actual = $file->dump(); + + $expected = <<<'TEST' +foo); + } + + public function withTypeHinting(\Countable $countable, array $array, callable $callable) { + echo 1; + } + + public function defaultValues($defaultValue = null, $defaultArray = array()) { + echo 2; + } + + public function withReferenceParam(&$param) { + } + + protected function abstractMethod() { + } + + private function _foo() { + // comment + // indentation + // back + } + + public static function staticMethod() { + } + + /** + * @return bool + */ + public function otherMethod() { + return false; + } +} + +TEST; + $this->assertSame($expected, $actual); + } + + + public function testGetName() + { + $className = 'Foo'; + $class = new TraitBlock($className); + $this->assertSame($className, $class->getName()); + } + + public function testByHand() + { + $file = new FileBlock(); + + $trait = new TraitBlock('TestTrait'); + $trait->addUse('\\CodeGeneratorMocks\\MockCompositeTrait'); + $trait->addMethod(new MethodBlock('testMethod', 'echo 1;')); + + $file->addBlock($trait); + + $expected = <<assertSame($expected, $file->dump()); + } +} From af6296738d6529479492649ecc74eb9104dc7acb Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 23 Jul 2015 15:21:34 +0200 Subject: [PATCH 05/30] fixed formatting --- composer.json | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/composer.json b/composer.json index 95a75ef..9108231 100644 --- a/composer.json +++ b/composer.json @@ -1,21 +1,21 @@ { - "name": "tomaszdurka/codegenerator", - "authors": [ - { - "name": "Tomasz Durka", - "email": "tomasz@durka.pl" - } - ], - "require": { - "php": ">=5.4" - }, - "require-dev": { - "phpunit/phpunit": "~3.7.10", - "satooshi/php-coveralls": "~0.6.1" - }, - "autoload": { - "psr-0": { - "CodeGenerator\\": "source/" - } + "name": "tomaszdurka/codegenerator", + "authors": [ + { + "name": "Tomasz Durka", + "email": "tomasz@durka.pl" } + ], + "require": { + "php": ">=5.4" + }, + "require-dev": { + "phpunit/phpunit": "~3.7.10", + "satooshi/php-coveralls": "~0.6.1" + }, + "autoload": { + "psr-0": { + "CodeGenerator\\": "source/" + } + } } From a0955eaa499d25e0e4d5f0efd60135d0328daee4 Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 23 Jul 2015 15:29:41 +0200 Subject: [PATCH 06/30] added cs fixer --- .php_cs | 28 ++++++++++++++++++++++++++++ composer.json | 37 +++++++++++++++++++------------------ 2 files changed, 47 insertions(+), 18 deletions(-) create mode 100644 .php_cs diff --git a/.php_cs b/.php_cs new file mode 100644 index 0000000..79f9173 --- /dev/null +++ b/.php_cs @@ -0,0 +1,28 @@ +in(__DIR__ . '/src') + ->exclude('Generated') +; +return Symfony\CS\Config\Config::create() + ->fixers( + array( + 'controls-spaces', + 'braces', + 'elseif', + 'eof_ending', + 'extra_empty_lines', + 'function_declaration', + 'include', + 'indentation', + 'linefeed', + 'php_closing_tag', + 'psr0', + 'short_tag', + 'trailing_spaces', + 'unused_use', + 'visibility' + ) + ) + ->finder($finder) +; diff --git a/composer.json b/composer.json index 9108231..1797cb2 100644 --- a/composer.json +++ b/composer.json @@ -1,21 +1,22 @@ { - "name": "tomaszdurka/codegenerator", - "authors": [ - { - "name": "Tomasz Durka", - "email": "tomasz@durka.pl" + "name": "tomaszdurka/codegenerator", + "authors": [ + { + "name": "Tomasz Durka", + "email": "tomasz@durka.pl" + } + ], + "require": { + "php": ">=5.4", + "fabpot/php-cs-fixer": "^1.9" + }, + "require-dev": { + "phpunit/phpunit": "~3.7.10", + "satooshi/php-coveralls": "~0.6.1" + }, + "autoload": { + "psr-0": { + "CodeGenerator\\": "source/" + } } - ], - "require": { - "php": ">=5.4" - }, - "require-dev": { - "phpunit/phpunit": "~3.7.10", - "satooshi/php-coveralls": "~0.6.1" - }, - "autoload": { - "psr-0": { - "CodeGenerator\\": "source/" - } - } } From 09797a40cf7cc4e3bc376b8b4e9a39ed33d931c1 Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 23 Jul 2015 15:30:57 +0200 Subject: [PATCH 07/30] cs fixed --- source/CodeGenerator/ArrayBlock.php | 18 +++-- source/CodeGenerator/Block.php | 43 ++++++++---- source/CodeGenerator/ClassBlock.php | 70 ++++++++++++------- source/CodeGenerator/ConstantBlock.php | 15 ++-- source/CodeGenerator/FileBlock.php | 11 +-- source/CodeGenerator/FunctionBlock.php | 61 ++++++++++------ source/CodeGenerator/InterfaceBlock.php | 16 +++-- source/CodeGenerator/InterfaceMethodBlock.php | 5 +- source/CodeGenerator/MethodBlock.php | 51 +++++++++----- source/CodeGenerator/ParameterBlock.php | 49 ++++++++----- source/CodeGenerator/PropertyBlock.php | 51 +++++++++----- source/CodeGenerator/TraitBlock.php | 9 +-- source/CodeGenerator/ValueBlock.php | 12 ++-- 13 files changed, 263 insertions(+), 148 deletions(-) diff --git a/source/CodeGenerator/ArrayBlock.php b/source/CodeGenerator/ArrayBlock.php index 4db1451..64f003e 100644 --- a/source/CodeGenerator/ArrayBlock.php +++ b/source/CodeGenerator/ArrayBlock.php @@ -2,28 +2,30 @@ namespace CodeGenerator; -class ArrayBlock extends Block { - +class ArrayBlock extends Block +{ /** @var array */ private $_value; /** * @param array $value */ - public function __construct(array $value = null) { + public function __construct(array $value = null) + { $this->_value = (array) $value; } /** * @return string */ - public function dump() { + public function dump() + { $entries = array(); $isAssociative = $this->isAssociative(); foreach ($this->_value as $key => $value) { $line = ''; if ($isAssociative) { - $line .= $key . ' => '; + $line .= $key.' => '; } $value = new ValueBlock($value); $line .= $value->dump(); @@ -31,9 +33,10 @@ public function dump() { } $content = implode(', ', $entries); if (strlen($content) < 100) { - return 'array(' . $content . ')'; + return 'array('.$content.')'; } else { $content = implode(",\n", $entries); + return $this->_dumpLine( 'array(', $this->_indent($content), @@ -42,7 +45,8 @@ public function dump() { } } - public function isAssociative() { + public function isAssociative() + { return (bool) count(array_filter(array_keys($this->_value), 'is_string')); } } diff --git a/source/CodeGenerator/Block.php b/source/CodeGenerator/Block.php index 98918a0..389a9a9 100644 --- a/source/CodeGenerator/Block.php +++ b/source/CodeGenerator/Block.php @@ -2,8 +2,8 @@ namespace CodeGenerator; -abstract class Block { - +abstract class Block +{ /** @var string */ protected static $_indentation = ' '; @@ -12,24 +12,29 @@ abstract class Block { */ abstract public function dump(); - public function __toString() { + public function __toString() + { return $this->dump(); } /** * @param string $content + * * @return string */ - protected function _indent($content) { - return preg_replace('/(:?^|[\n])/', '$1' . self::$_indentation, $content); + protected function _indent($content) + { + return preg_replace('/(:?^|[\n])/', '$1'.self::$_indentation, $content); } /** - * @param string $content - * @param boolean|null $untilUnsafe + * @param string $content + * @param bool|null $untilUnsafe + * * @return string */ - protected function _outdent($content, $untilUnsafe = null) { + protected function _outdent($content, $untilUnsafe = null) + { $indentation = self::$_indentation; if (!$indentation) { return $content; @@ -47,29 +52,35 @@ protected function _outdent($content, $untilUnsafe = null) { } } foreach ($lines as $key => $line) { - $lines[$key] = preg_replace('/^' . preg_quote(self::$_indentation) . '/', '$1', $line); + $lines[$key] = preg_replace('/^'.preg_quote(self::$_indentation).'/', '$1', $line); } $content = implode(PHP_EOL, $lines); if ($untilUnsafe) { $content = $this->_outdent($content, $untilUnsafe); } + return $content; } /** * @param string $line , $line, $line + * * @return string */ - protected function _dumpLine($line) { + protected function _dumpLine($line) + { $lines = func_get_args(); + return $this->_dumpLines($lines); } /** * @param string[] $lines + * * @return string */ - protected function _dumpLines(array $lines) { + protected function _dumpLines(array $lines) + { return implode(PHP_EOL, array_filter($lines, function ($element) { return !is_null($element); })); @@ -78,18 +89,22 @@ protected function _dumpLines(array $lines) { /** * @param string $indentation */ - public static function setIndentation($indentation) { + public static function setIndentation($indentation) + { self::$_indentation = (string) $indentation; } /** * @param string $className + * * @return string */ - protected static function _normalizeClassName($className) { + protected static function _normalizeClassName($className) + { if (strpos($className, '\\') !== 0) { - $className = '\\' . $className; + $className = '\\'.$className; } + return $className; } } diff --git a/source/CodeGenerator/ClassBlock.php b/source/CodeGenerator/ClassBlock.php index ef0e804..bbf7f29 100644 --- a/source/CodeGenerator/ClassBlock.php +++ b/source/CodeGenerator/ClassBlock.php @@ -2,8 +2,8 @@ namespace CodeGenerator; -class ClassBlock extends Block { - +class ClassBlock extends Block +{ /** @var string */ private $_name; @@ -28,7 +28,7 @@ class ClassBlock extends Block { /** @var MethodBlock[] */ private $_methods = array(); - /** @var boolean */ + /** @var bool */ private $_abstract; /** @@ -36,7 +36,8 @@ class ClassBlock extends Block { * @param string|null $parentClassName * @param array|null $interfaces */ - public function __construct($name, $parentClassName = null, array $interfaces = null) { + public function __construct($name, $parentClassName = null, array $interfaces = null) + { $this->_name = (string) $name; if (null !== $parentClassName) { $this->setParentClassName($parentClassName); @@ -49,79 +50,90 @@ public function __construct($name, $parentClassName = null, array $interfaces = /** * @return string */ - public function getName() { + public function getName() + { return $this->_name; } /** * @param string $parentClassName */ - public function setParentClassName($parentClassName) { + public function setParentClassName($parentClassName) + { $this->_parentClassName = (string) $parentClassName; } /** * @param string $namespace */ - public function setNamespace($namespace) { + public function setNamespace($namespace) + { $this->_namespace = (string) $namespace; } /** * @param string[] $interfaces */ - public function setInterfaces(array $interfaces) { + public function setInterfaces(array $interfaces) + { foreach ($interfaces as $interface) { $this->addInterface($interface); } } /** - * @param boolean $abstract + * @param bool $abstract */ - public function setAbstract($abstract) { + public function setAbstract($abstract) + { $this->_abstract = (bool) $abstract; } /** * @param string $name */ - public function addUse($name) { + public function addUse($name) + { $this->_uses[] = $name; } /** * @param ConstantBlock $constant */ - public function addConstant(ConstantBlock $constant) { + public function addConstant(ConstantBlock $constant) + { $this->_constants[$constant->getName()] = $constant; } /** * @param PropertyBlock $property */ - public function addProperty(PropertyBlock $property) { + public function addProperty(PropertyBlock $property) + { $this->_properties[$property->getName()] = $property; } /** * @param MethodBlock $method */ - public function addMethod(MethodBlock $method) { + public function addMethod(MethodBlock $method) + { $this->_methods[$method->getName()] = $method; } /** * @param string $interface */ - public function addInterface($interface) { + public function addInterface($interface) + { $this->_interfaces[] = $interface; } /** * @return string */ - public function dump() { + public function dump() + { $lines = array(); $lines[] = $this->_dumpHeader(); foreach ($this->_uses as $use) { @@ -141,56 +153,63 @@ public function dump() { $lines[] = $this->_indent($method->dump()); } $lines[] = $this->_dumpFooter(); + return $this->_dumpLines($lines); } /** * @return string */ - private function _dumpHeader() { + private function _dumpHeader() + { $lines = array(); if ($this->_namespace) { - $lines[] = 'namespace ' . $this->_namespace . ';'; + $lines[] = 'namespace '.$this->_namespace.';'; $lines[] = ''; } $classDeclaration = ''; if ($this->_abstract) { $classDeclaration .= 'abstract '; } - $classDeclaration .= 'class ' . $this->_name; + $classDeclaration .= 'class '.$this->_name; if ($this->_parentClassName) { - $classDeclaration .= ' extends ' . $this->_getParentClassName(); + $classDeclaration .= ' extends '.$this->_getParentClassName(); } if ($this->_interfaces) { - $classDeclaration .= ' implements ' . implode(', ', $this->_getInterfaces()); + $classDeclaration .= ' implements '.implode(', ', $this->_getInterfaces()); } $classDeclaration .= ' {'; $lines[] = $classDeclaration; + return $this->_dumpLines($lines); } /** * @return string[] */ - private function _getInterfaces() { + private function _getInterfaces() + { return array_map(array('\\CodeGenerator\\ClassBlock', '_normalizeClassName'), $this->_interfaces); } /** * @return string */ - private function _getParentClassName() { + private function _getParentClassName() + { return self::_normalizeClassName($this->_parentClassName); } /** * @return string */ - private function _dumpFooter() { + private function _dumpFooter() + { return '}'; } - public static function buildFromReflection(\ReflectionClass $reflection) { + public static function buildFromReflection(\ReflectionClass $reflection) + { $class = new self($reflection->getShortName()); $class->setNamespace($reflection->getNamespaceName()); $reflectionParentClass = $reflection->getParentClass(); @@ -222,6 +241,7 @@ public static function buildFromReflection(\ReflectionClass $reflection) { $class->addConstant(new ConstantBlock($name, $value)); } } + return $class; } } diff --git a/source/CodeGenerator/ConstantBlock.php b/source/CodeGenerator/ConstantBlock.php index d7dc031..1654185 100644 --- a/source/CodeGenerator/ConstantBlock.php +++ b/source/CodeGenerator/ConstantBlock.php @@ -2,8 +2,8 @@ namespace CodeGenerator; -class ConstantBlock extends Block { - +class ConstantBlock extends Block +{ /** @var string */ private $_name; @@ -14,7 +14,8 @@ class ConstantBlock extends Block { * @param string $name * @param string|int $value */ - public function __construct($name, $value) { + public function __construct($name, $value) + { $this->_name = (string) $name; $this->_value = $value; } @@ -22,11 +23,13 @@ public function __construct($name, $value) { /** * @return string */ - public function getName() { + public function getName() + { return $this->_name; } - public function dump() { - return 'const ' . $this->_name . ' = ' . var_export($this->_value, true) . ';'; + public function dump() + { + return 'const '.$this->_name.' = '.var_export($this->_value, true).';'; } } diff --git a/source/CodeGenerator/FileBlock.php b/source/CodeGenerator/FileBlock.php index b80fc33..6dea7e8 100644 --- a/source/CodeGenerator/FileBlock.php +++ b/source/CodeGenerator/FileBlock.php @@ -2,19 +2,21 @@ namespace CodeGenerator; -class FileBlock extends Block { - +class FileBlock extends Block +{ /** @var Block[] */ private $_blocks = array(); /** * @param Block $block */ - public function addBlock(Block $block) { + public function addBlock(Block $block) + { $this->_blocks[] = $block; } - public function dump() { + public function dump() + { $lines = array(); $lines[] = '_blocks as $block) { @@ -22,6 +24,7 @@ public function dump() { $lines[] = $block->dump(); } $lines[] = ''; + return $this->_dumpLines($lines); } } diff --git a/source/CodeGenerator/FunctionBlock.php b/source/CodeGenerator/FunctionBlock.php index 354014e..edbc93b 100644 --- a/source/CodeGenerator/FunctionBlock.php +++ b/source/CodeGenerator/FunctionBlock.php @@ -2,8 +2,8 @@ namespace CodeGenerator; -class FunctionBlock extends Block { - +class FunctionBlock extends Block +{ /** @var string|null */ protected $_name; @@ -19,7 +19,8 @@ class FunctionBlock extends Block { /** * @param callable|string|null $body */ - public function __construct($body = null) { + public function __construct($body = null) + { if (null !== $body) { if ($body instanceof \Closure) { $this->extractFromClosure($body); @@ -32,24 +33,28 @@ public function __construct($body = null) { /** * @param string $name */ - public function setName($name) { + public function setName($name) + { $this->_name = (string) $name; } /** * @return string|null */ - public function getName() { + public function getName() + { return $this->_name; } /** * @param ParameterBlock $parameter + * * @throws \Exception */ - public function addParameter(ParameterBlock $parameter) { + public function addParameter(ParameterBlock $parameter) + { if (array_key_exists($parameter->getName(), $this->_parameters)) { - throw new \Exception('Parameter `' . $parameter->getName() . '` is already set.'); + throw new \Exception('Parameter `'.$parameter->getName().'` is already set.'); } $this->_parameters[$parameter->getName()] = $parameter; } @@ -57,7 +62,8 @@ public function addParameter(ParameterBlock $parameter) { /** * @param string $code */ - public function setCode($code) { + public function setCode($code) + { if (null !== $code) { $code = $this->_outdent((string) $code, true); } @@ -67,7 +73,8 @@ public function setCode($code) { /** * @param string|null $docBlock */ - public function setDocBlock($docBlock) { + public function setDocBlock($docBlock) + { if (null !== $docBlock) { $docBlock = (string) $docBlock; } @@ -77,10 +84,12 @@ public function setDocBlock($docBlock) { /** * @param \ReflectionFunctionAbstract $reflection */ - public function setBodyFromReflection(\ReflectionFunctionAbstract $reflection) { + public function setBodyFromReflection(\ReflectionFunctionAbstract $reflection) + { /** @var $reflection \ReflectionMethod */ if (is_a($reflection, '\\ReflectionMethod') && $reflection->isAbstract()) { $this->_code = null; + return; } $file = new \SplFileObject($reflection->getFileName()); @@ -110,7 +119,8 @@ public function setBodyFromReflection(\ReflectionFunctionAbstract $reflection) { /** * @param \ReflectionFunctionAbstract $reflection */ - public function setParametersFromReflection(\ReflectionFunctionAbstract $reflection) { + public function setParametersFromReflection(\ReflectionFunctionAbstract $reflection) + { foreach ($reflection->getParameters() as $reflectionParameter) { $parameter = ParameterBlock::buildFromReflection($reflectionParameter); $this->addParameter($parameter); @@ -120,57 +130,65 @@ public function setParametersFromReflection(\ReflectionFunctionAbstract $reflect /** * @param \ReflectionFunctionAbstract $reflection */ - public function setDocBlockFromReflection(\ReflectionFunctionAbstract $reflection) { + public function setDocBlockFromReflection(\ReflectionFunctionAbstract $reflection) + { $docBlock = $reflection->getDocComment(); if ($docBlock) { - $docBlock = preg_replace('/([\n\r])(' . self::$_indentation . ')+/', '$1', $docBlock); + $docBlock = preg_replace('/([\n\r])('.self::$_indentation.')+/', '$1', $docBlock); $this->setDocBlock($docBlock); } } - public function dump() { + public function dump() + { return $this->_dumpLine( $this->_dumpDocBlock(), - $this->_dumpHeader() . $this->_dumpBody() + $this->_dumpHeader().$this->_dumpBody() ); } /** * @return string */ - protected function _dumpDocBlock() { + protected function _dumpDocBlock() + { return $this->_docBlock; } /** * @return string */ - protected function _dumpHeader() { + protected function _dumpHeader() + { $content = 'function'; if ($this->_name) { - $content .= ' ' . $this->_name; + $content .= ' '.$this->_name; } $content .= '('; $content .= implode(', ', $this->_parameters); $content .= ')'; + return $content; } /** * @return string */ - protected function _dumpBody() { + protected function _dumpBody() + { $code = $this->_code; if ($code) { $code = $this->_indent($code); } + return $this->_dumpLine(' {', $code, '}'); } /** * @param \ReflectionFunctionAbstract $reflection */ - public function extractFromReflection(\ReflectionFunctionAbstract $reflection) { + public function extractFromReflection(\ReflectionFunctionAbstract $reflection) + { $this->setBodyFromReflection($reflection); $this->setParametersFromReflection($reflection); $this->setDocBlockFromReflection($reflection); @@ -179,7 +197,8 @@ public function extractFromReflection(\ReflectionFunctionAbstract $reflection) { /** * @param \Closure $closure */ - public function extractFromClosure(\Closure $closure) { + public function extractFromClosure(\Closure $closure) + { $this->extractFromReflection(new \ReflectionFunction($closure)); } } diff --git a/source/CodeGenerator/InterfaceBlock.php b/source/CodeGenerator/InterfaceBlock.php index cd71834..b5e4121 100644 --- a/source/CodeGenerator/InterfaceBlock.php +++ b/source/CodeGenerator/InterfaceBlock.php @@ -25,7 +25,7 @@ class InterfaceBlock extends Block */ public function __construct($name, array $parentInterfaceNames = null) { - $this->_name = (string)$name; + $this->_name = (string) $name; if (!is_null($parentInterfaceNames)) { $this->setParentInterfaceNames($parentInterfaceNames); @@ -47,11 +47,12 @@ public function setParentInterfaceNames(array $parentInterfaceNames) */ public function addParentInterfaceName($parentInterfaceName) { - $this->_parentInterfaceNames[] = (string)$parentInterfaceName; + $this->_parentInterfaceNames[] = (string) $parentInterfaceName; } /** * @param \ReflectionClass $reflection + * * @return InterfaceBlock */ public static function buildFromReflection(\ReflectionClass $reflection) @@ -93,7 +94,7 @@ public static function buildFromReflection(\ReflectionClass $reflection) */ public function setNamespace($namespace) { - $this->_namespace = (string)$namespace; + $this->_namespace = (string) $namespace; } /** @@ -114,6 +115,7 @@ public function addConstant(ConstantBlock $constant) /** * @param \ReflectionClass $reflection + * * @return array */ protected static function getAllConstantsOfParentInterfaces(\ReflectionClass $reflection) @@ -164,13 +166,13 @@ private function _dumpHeader() { $lines = array(); if ($this->_namespace) { - $lines[] = 'namespace ' . $this->_namespace . ';'; + $lines[] = 'namespace '.$this->_namespace.';'; $lines[] = ''; } $classDeclaration = ''; - $classDeclaration .= 'interface ' . $this->_name; + $classDeclaration .= 'interface '.$this->_name; if ($this->_parentInterfaceNames) { - $classDeclaration .= ' extends ' . $this->_getParentInterfaces(); + $classDeclaration .= ' extends '.$this->_getParentInterfaces(); } $classDeclaration .= ' {'; $lines[] = $classDeclaration; @@ -188,7 +190,7 @@ private function _getParentInterfaces() $cleaned[] = self::_normalizeClassName($parentInterfaceName); } - return join(', ', $cleaned); + return implode(', ', $cleaned); } /** diff --git a/source/CodeGenerator/InterfaceMethodBlock.php b/source/CodeGenerator/InterfaceMethodBlock.php index d37e437..94b8502 100644 --- a/source/CodeGenerator/InterfaceMethodBlock.php +++ b/source/CodeGenerator/InterfaceMethodBlock.php @@ -18,7 +18,7 @@ public function __construct($name) */ protected function _dumpHeader() { - return 'public ' . parent::_dumpHeader(); + return 'public '.parent::_dumpHeader(); } /** @@ -31,6 +31,7 @@ protected function _dumpBody() /** * @param \ReflectionMethod $reflection + * * @return InterfaceMethodBlock */ public static function buildFromReflection(\ReflectionMethod $reflection) @@ -40,4 +41,4 @@ public static function buildFromReflection(\ReflectionMethod $reflection) return $method; } -} \ No newline at end of file +} diff --git a/source/CodeGenerator/MethodBlock.php b/source/CodeGenerator/MethodBlock.php index 90ae557..d47ff31 100644 --- a/source/CodeGenerator/MethodBlock.php +++ b/source/CodeGenerator/MethodBlock.php @@ -2,22 +2,23 @@ namespace CodeGenerator; -class MethodBlock extends FunctionBlock { - +class MethodBlock extends FunctionBlock +{ /** @var string */ private $_visibility; - /** @var boolean */ + /** @var bool */ private $_static; - /** @var boolean */ + /** @var bool */ private $_abstract; /** * @param string $name * @param callable|string|null $body */ - public function __construct($name, $body = null) { + public function __construct($name, $body = null) + { $this->setName($name); $this->setVisibility('public'); $this->setStatic(false); @@ -28,28 +29,32 @@ public function __construct($name, $body = null) { /** * @param string $visibility */ - public function setVisibility($visibility) { + public function setVisibility($visibility) + { $this->_visibility = (string) $visibility; } /** - * @param boolean $static + * @param bool $static */ - public function setStatic($static) { + public function setStatic($static) + { $this->_static = (bool) $static; } /** - * @param boolean $abstract + * @param bool $abstract */ - public function setAbstract($abstract) { + public function setAbstract($abstract) + { $this->_abstract = (bool) $abstract; } /** * @param \ReflectionFunctionAbstract $reflection */ - public function extractFromReflection(\ReflectionFunctionAbstract $reflection) { + public function extractFromReflection(\ReflectionFunctionAbstract $reflection) + { parent::extractFromReflection($reflection); if ($reflection instanceof \ReflectionMethod) { $this->setVisibilityFromReflection($reflection); @@ -61,7 +66,8 @@ public function extractFromReflection(\ReflectionFunctionAbstract $reflection) { /** * @param \ReflectionMethod $reflection */ - public function setVisibilityFromReflection(\ReflectionMethod $reflection) { + public function setVisibilityFromReflection(\ReflectionMethod $reflection) + { if ($reflection->isPublic()) { $this->setVisibility('public'); } @@ -76,18 +82,21 @@ public function setVisibilityFromReflection(\ReflectionMethod $reflection) { /** * @param \ReflectionMethod $reflection */ - public function setAbstractFromReflection(\ReflectionMethod $reflection) { + public function setAbstractFromReflection(\ReflectionMethod $reflection) + { $this->setAbstract($reflection->isAbstract()); } /** * @param \ReflectionMethod $reflection */ - public function setStaticFromReflection(\ReflectionMethod $reflection) { + public function setStaticFromReflection(\ReflectionMethod $reflection) + { $this->setStatic($reflection->isStatic()); } - protected function _dumpHeader() { + protected function _dumpHeader() + { $code = ''; if ($this->_abstract) { $code .= 'abstract '; @@ -96,24 +105,30 @@ protected function _dumpHeader() { if ($this->_static) { $code .= ' static'; } - $code .= ' ' . parent::_dumpHeader(); + $code .= ' '.parent::_dumpHeader(); + return $code; } - protected function _dumpBody() { + protected function _dumpBody() + { if ($this->_abstract) { return ';'; } + return parent::_dumpBody(); } /** * @param \ReflectionMethod $reflection + * * @return MethodBlock */ - public static function buildFromReflection(\ReflectionMethod $reflection) { + public static function buildFromReflection(\ReflectionMethod $reflection) + { $method = new self($reflection->getName()); $method->extractFromReflection($reflection); + return $method; } } diff --git a/source/CodeGenerator/ParameterBlock.php b/source/CodeGenerator/ParameterBlock.php index 4e35fad..6e73771 100644 --- a/source/CodeGenerator/ParameterBlock.php +++ b/source/CodeGenerator/ParameterBlock.php @@ -2,8 +2,8 @@ namespace CodeGenerator; -class ParameterBlock extends Block { - +class ParameterBlock extends Block +{ /** @var string */ private $_name; @@ -13,22 +13,25 @@ class ParameterBlock extends Block { /** @var mixed */ private $_defaultValue; - /** @var boolean */ + /** @var bool */ private $_optional; - /** @var boolean */ + /** @var bool */ private $_passedByReference; /** - * @param string $name - * @param string|null $type - * @param null $optional - * @param mixed|null $defaultValue - * @param boolean|null $passedByReference + * @param string $name + * @param string|null $type + * @param null $optional + * @param mixed|null $defaultValue + * @param bool|null $passedByReference + * * @throws \Exception + * * @internal param bool|null $isOptional */ - public function __construct($name, $type = null, $optional = null, $defaultValue = null, $passedByReference = null) { + public function __construct($name, $type = null, $optional = null, $defaultValue = null, $passedByReference = null) + { $this->_name = (string) $name; if (null !== $type) { $this->_type = (string) $type; @@ -46,52 +49,61 @@ public function __construct($name, $type = null, $optional = null, $defaultValue /** * @return string */ - public function getName() { + public function getName() + { return $this->_name; } /** * @return string */ - public function dump() { + public function dump() + { $content = ''; if ($this->_type) { - $content .= $this->_getType() . ' '; + $content .= $this->_getType().' '; } if ($this->_passedByReference) { $content .= '&'; } - $content .= '$' . $this->_name; + $content .= '$'.$this->_name; if ($this->_optional) { - $content .= ' = ' . $this->_dumpDefaultValue(); + $content .= ' = '.$this->_dumpDefaultValue(); } + return $content; } - protected function _dumpDefaultValue() { + protected function _dumpDefaultValue() + { if (null === $this->_defaultValue) { return 'null'; } $value = new ValueBlock($this->_defaultValue); + return $value->dump(); } /** * @return null|string */ - protected function _getType() { + protected function _getType() + { $type = $this->_type; if (!in_array($type, [null, 'array', 'callable'], true)) { $type = self::_normalizeClassName($type); } + return $type; } /** * @param \ReflectionParameter $reflection + * * @return ParameterBlock */ - public static function buildFromReflection(\ReflectionParameter $reflection) { + public static function buildFromReflection(\ReflectionParameter $reflection) + { $type = null; if ($reflection->isCallable()) { $type = 'callable'; @@ -106,6 +118,7 @@ public static function buildFromReflection(\ReflectionParameter $reflection) { if ($reflection->isDefaultValueAvailable()) { $defaultValue = $reflection->getDefaultValue(); } + return new self($reflection->getName(), $type, $reflection->isOptional(), $defaultValue, $reflection->isPassedByReference()); } } diff --git a/source/CodeGenerator/PropertyBlock.php b/source/CodeGenerator/PropertyBlock.php index c18f621..d358d05 100644 --- a/source/CodeGenerator/PropertyBlock.php +++ b/source/CodeGenerator/PropertyBlock.php @@ -2,8 +2,8 @@ namespace CodeGenerator; -class PropertyBlock extends Block { - +class PropertyBlock extends Block +{ /** @var string */ private $_name; @@ -19,7 +19,8 @@ class PropertyBlock extends Block { /** * @param string $name */ - public function __construct($name) { + public function __construct($name) + { $this->_name = (string) $name; $this->setVisibility('public'); } @@ -27,35 +28,40 @@ public function __construct($name) { /** * @return string */ - public function getName() { + public function getName() + { return $this->_name; } /** * @param string $visibility */ - public function setVisibility($visibility) { + public function setVisibility($visibility) + { $this->_visibility = (string) $visibility; } /** * @param mixed $value */ - public function setDefaultValue($value) { + public function setDefaultValue($value) + { $this->_defaultValue = $value; } /** * @param string|null $docBlock */ - public function setDocBlock($docBlock) { + public function setDocBlock($docBlock) + { if (null !== $docBlock) { $docBlock = (string) $docBlock; } $this->_docBlock = $docBlock; } - public function dump() { + public function dump() + { return $this->_dumpLine( $this->_dumpDocBlock(), $this->_dumpValue() @@ -65,7 +71,8 @@ public function dump() { /** * @param \ReflectionProperty $reflection */ - public function extractFromReflection(\ReflectionProperty $reflection) { + public function extractFromReflection(\ReflectionProperty $reflection) + { $this->_setVisibilityFromReflection($reflection); $this->_setDefaultValueFromReflection($reflection); $this->_setDocBlockFromReflection($reflection); @@ -74,27 +81,31 @@ public function extractFromReflection(\ReflectionProperty $reflection) { /** * @return string */ - protected function _dumpDocBlock() { + protected function _dumpDocBlock() + { return $this->_docBlock; } /** * @return string */ - protected function _dumpValue() { - $content = $this->_visibility . ' $' . $this->_name; + protected function _dumpValue() + { + $content = $this->_visibility.' $'.$this->_name; if (null !== $this->_defaultValue) { $value = new ValueBlock($this->_defaultValue); - $content .= ' = ' . $value->dump(); + $content .= ' = '.$value->dump(); } $content .= ';'; + return $content; } /** * @param \ReflectionProperty $reflection */ - protected function _setVisibilityFromReflection(\ReflectionProperty $reflection) { + protected function _setVisibilityFromReflection(\ReflectionProperty $reflection) + { if ($reflection->isPublic()) { $this->setVisibility('public'); } @@ -106,10 +117,11 @@ protected function _setVisibilityFromReflection(\ReflectionProperty $reflection) } } - protected function _setDocBlockFromReflection(\ReflectionProperty $reflection) { + protected function _setDocBlockFromReflection(\ReflectionProperty $reflection) + { $docBlock = $reflection->getDocComment(); if ($docBlock) { - $docBlock = preg_replace('/([\n\r])(' . self::$_indentation . ')+/', '$1', $docBlock); + $docBlock = preg_replace('/([\n\r])('.self::$_indentation.')+/', '$1', $docBlock); $this->setDocBlock($docBlock); } } @@ -117,7 +129,8 @@ protected function _setDocBlockFromReflection(\ReflectionProperty $reflection) { /** * @param \ReflectionProperty $reflection */ - protected function _setDefaultValueFromReflection(\ReflectionProperty $reflection) { + protected function _setDefaultValueFromReflection(\ReflectionProperty $reflection) + { $defaultProperties = $reflection->getDeclaringClass()->getDefaultProperties(); $value = $defaultProperties[$this->getName()]; if (null !== $value) { @@ -127,9 +140,11 @@ protected function _setDefaultValueFromReflection(\ReflectionProperty $reflectio /** * @param \ReflectionProperty $reflection + * * @return PropertyBlock */ - public static function buildFromReflection(\ReflectionProperty $reflection) { + public static function buildFromReflection(\ReflectionProperty $reflection) + { $property = new self($reflection->getName()); $property->extractFromReflection($reflection); // $property->setDefaultValue($reflection->getValue()); diff --git a/source/CodeGenerator/TraitBlock.php b/source/CodeGenerator/TraitBlock.php index bfcf29f..93f7075 100644 --- a/source/CodeGenerator/TraitBlock.php +++ b/source/CodeGenerator/TraitBlock.php @@ -24,7 +24,7 @@ class TraitBlock extends Block */ public function __construct($name) { - $this->_name = (string)$name; + $this->_name = (string) $name; } /** @@ -40,7 +40,7 @@ public function getName() */ public function setNamespace($namespace) { - $this->_namespace = (string)$namespace; + $this->_namespace = (string) $namespace; } /** @@ -98,10 +98,10 @@ private function _dumpHeader() { $lines = []; if ($this->_namespace) { - $lines[] = 'namespace ' . $this->_namespace . ';'; + $lines[] = 'namespace '.$this->_namespace.';'; $lines[] = ''; } - $classDeclaration = 'trait ' . $this->_name; + $classDeclaration = 'trait '.$this->_name; $classDeclaration .= ' {'; $lines[] = $classDeclaration; @@ -118,6 +118,7 @@ private function _dumpFooter() /** * @param \ReflectionClass $reflection + * * @return TraitBlock */ public static function buildFromReflection(\ReflectionClass $reflection) diff --git a/source/CodeGenerator/ValueBlock.php b/source/CodeGenerator/ValueBlock.php index b41233a..d6bcbc6 100644 --- a/source/CodeGenerator/ValueBlock.php +++ b/source/CodeGenerator/ValueBlock.php @@ -2,26 +2,30 @@ namespace CodeGenerator; -class ValueBlock extends Block { - +class ValueBlock extends Block +{ /** @var mixed */ private $_value; /** * @param mixed $value */ - public function __construct($value) { + public function __construct($value) + { $this->_value = $value; } /** * @return string */ - public function dump() { + public function dump() + { if (is_array($this->_value)) { $array = new ArrayBlock($this->_value); + return $array->dump(); } + return var_export($this->_value, true); } } From d4f412e5dea10a6745739b7be65c82d4aa8cad13 Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 23 Jul 2015 15:32:05 +0200 Subject: [PATCH 08/30] cs fix for tests --- tests/bootstrap.php | 6 ++-- tests/helpers/TestHelper.php | 31 ++++++++++-------- tests/mocks/MockAbstractClass.php | 6 ++-- tests/mocks/MockClass.php | 27 ++++++++++------ tests/mocks/MockCompositeTrait.php | 5 +-- tests/mocks/MockInterface.php | 6 ++-- tests/mocks/MockInterfaceTwo.php | 6 ++-- tests/mocks/MockTrait.php | 27 ++++++++++------ tests/mocks/MockTraitTwo.php | 9 +++--- tests/source/CodeGenearator/ArrayTest.php | 17 ++++++---- tests/source/CodeGenearator/BlockTest.php | 32 +++++++++++-------- tests/source/CodeGenearator/ClassTest.php | 10 +++--- tests/source/CodeGenearator/FunctionTest.php | 18 ++++++----- tests/source/CodeGenearator/InterfaceTest.php | 2 +- tests/source/CodeGenearator/TraitTest.php | 5 ++- 15 files changed, 118 insertions(+), 89 deletions(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index ff79b74..20f9b7e 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,6 +1,6 @@ addPsr4('CodeGeneratorMocks\\', __DIR__ . '/mocks'); -$autoLoader->addPsr4('CodeGeneratorHelpers\\', __DIR__ . '/helpers'); +$autoLoader = require __DIR__.'/../vendor/autoload.php'; +$autoLoader->addPsr4('CodeGeneratorMocks\\', __DIR__.'/mocks'); +$autoLoader->addPsr4('CodeGeneratorHelpers\\', __DIR__.'/helpers'); diff --git a/tests/helpers/TestHelper.php b/tests/helpers/TestHelper.php index d9e81bf..939f1b4 100644 --- a/tests/helpers/TestHelper.php +++ b/tests/helpers/TestHelper.php @@ -1,21 +1,24 @@ setAccessible(true); - /** - * @param Block $object - * @param string $methodName - * @param array|null $arguments - * @return mixed - */ - public static function invokeMethod(Block $object, $methodName, array $arguments = null) { - $arguments = (array) $arguments; - $reflection = new \ReflectionMethod(get_class($object), $methodName); - $reflection->setAccessible(true); - return $reflection->invokeArgs($object, $arguments); - } + return $reflection->invokeArgs($object, $arguments); + } } diff --git a/tests/mocks/MockAbstractClass.php b/tests/mocks/MockAbstractClass.php index 8733d91..0ff113a 100644 --- a/tests/mocks/MockAbstractClass.php +++ b/tests/mocks/MockAbstractClass.php @@ -1,8 +1,8 @@ foo); } - public function withTypeHinting(\Countable $countable, array $array, callable $callable) { + public function withTypeHinting(\Countable $countable, array $array, callable $callable) + { } - public function defaultValues($defaultValue = null, $defaultArray = array()) { + public function defaultValues($defaultValue = null, $defaultArray = array()) + { } - public function withReferenceParam(&$param) { + public function withReferenceParam(&$param) + { } - protected function abstractMethod() { + protected function abstractMethod() + { } - private function _foo() { + private function _foo() + { // comment // indentation // back } - public static function staticMethod() { + public static function staticMethod() + { } } diff --git a/tests/mocks/MockCompositeTrait.php b/tests/mocks/MockCompositeTrait.php index 8e61654..4850fb7 100644 --- a/tests/mocks/MockCompositeTrait.php +++ b/tests/mocks/MockCompositeTrait.php @@ -1,8 +1,9 @@ foo); } - public function withTypeHinting(\Countable $countable, array $array, callable $callable) { + public function withTypeHinting(\Countable $countable, array $array, callable $callable) + { echo 1; } - public function defaultValues($defaultValue = null, $defaultArray = array()) { + public function defaultValues($defaultValue = null, $defaultArray = array()) + { echo 2; } - public function withReferenceParam(&$param) { + public function withReferenceParam(&$param) + { } - protected function abstractMethod() { + protected function abstractMethod() + { } - private function _foo() { + private function _foo() + { // comment // indentation // back } - public static function staticMethod() { + public static function staticMethod() + { } } diff --git a/tests/mocks/MockTraitTwo.php b/tests/mocks/MockTraitTwo.php index 2e9874b..9590ba3 100644 --- a/tests/mocks/MockTraitTwo.php +++ b/tests/mocks/MockTraitTwo.php @@ -1,16 +1,17 @@ assertNotRegExp("/\n/", $array->dump()); $this->_assertSame($value, $array); } - public function testDumpLong() { + public function testDumpLong() + { $value = array_fill(0, 100, 'foo'); $array = new ArrayBlock($value); $this->assertRegExp("/\n /", $array->dump()); @@ -22,11 +24,12 @@ public function testDumpLong() { } /** - * @param array $expected + * @param array $expected * @param ArrayBlock $actual */ - private function _assertSame(array $expected, ArrayBlock $actual) { - $code = 'return ' . $actual->dump() . ';'; + private function _assertSame(array $expected, ArrayBlock $actual) + { + $code = 'return '.$actual->dump().';'; $evaluatedActual = eval($code); $this->assertSame($expected, $evaluatedActual); } diff --git a/tests/source/CodeGenearator/BlockTest.php b/tests/source/CodeGenearator/BlockTest.php index 9715559..460d66b 100644 --- a/tests/source/CodeGenearator/BlockTest.php +++ b/tests/source/CodeGenearator/BlockTest.php @@ -6,15 +6,16 @@ use CodeGenerator\FileBlock; use CodeGeneratorHelpers\TestHelper; -class CG_BlockTest extends \PHPUnit_Framework_TestCase { - - public function testOutdent() { +class BlockTest extends \PHPUnit_Framework_TestCase +{ + public function testOutdent() + { $block = new FileBlock(); $cases = array( - " foo" => "foo", - "foo" => "foo", + ' foo' => 'foo', + 'foo' => 'foo', " foo\nbar" => "foo\nbar", - " foo" => " foo", + ' foo' => ' foo', ); foreach ($cases as $input => $expected) { $output = TestHelper::invokeMethod($block, '_outdent', array($input)); @@ -22,12 +23,13 @@ public function testOutdent() { } } - public function testOutdentUntilSafe() { + public function testOutdentUntilSafe() + { $block = new FileBlock(); $cases = array( - " foo\nbar" => " foo\nbar", + " foo\nbar" => " foo\nbar", " foo\n bar" => " foo\nbar", - " foo" => "foo", + ' foo' => 'foo', ); foreach ($cases as $input => $expected) { $output = TestHelper::invokeMethod($block, '_outdent', array($input, true)); @@ -35,10 +37,11 @@ public function testOutdentUntilSafe() { } } - public function testIndent() { + public function testIndent() + { $block = new FileBlock(); $cases = array( - "foo\nbar" => " foo\n bar", + "foo\nbar" => " foo\n bar", " foo\n bar" => " foo\n bar", ); foreach ($cases as $input => $expected) { @@ -47,12 +50,13 @@ public function testIndent() { } } - public function testSetIndentation() { + public function testSetIndentation() + { Block::setIndentation(' '); $block = new FileBlock(); - $output = TestHelper::invokeMethod($block, '_indent', array("foo", true)); - $this->assertSame(" foo", $output); + $output = TestHelper::invokeMethod($block, '_indent', array('foo', true)); + $this->assertSame(' foo', $output); $output = TestHelper::invokeMethod($block, '_outdent', array(" foo\n bar", true)); $this->assertSame("foo\n bar", $output); diff --git a/tests/source/CodeGenearator/ClassTest.php b/tests/source/CodeGenearator/ClassTest.php index c8d9288..c3c06d2 100644 --- a/tests/source/CodeGenearator/ClassTest.php +++ b/tests/source/CodeGenearator/ClassTest.php @@ -5,9 +5,10 @@ use CodeGenerator\ClassBlock; use CodeGenerator\FileBlock; -class CG_ClassTest extends \PHPUnit_Framework_TestCase { - - public function testDump() { +class ClassTest extends \PHPUnit_Framework_TestCase +{ + public function testDump() + { $classes = array('CodeGeneratorMocks\\MockAbstractClass', 'CodeGeneratorMocks\\MockClass'); foreach ($classes as $className) { $file = new FileBlock(); @@ -22,7 +23,8 @@ public function testDump() { } } - public function testGetName() { + public function testGetName() + { $className = 'Foo'; $class = new ClassBlock($className, 'Bar', array('Countable')); $this->assertSame($className, $class->getName()); diff --git a/tests/source/CodeGenearator/FunctionTest.php b/tests/source/CodeGenearator/FunctionTest.php index a762d3e..d673a9b 100644 --- a/tests/source/CodeGenearator/FunctionTest.php +++ b/tests/source/CodeGenearator/FunctionTest.php @@ -4,22 +4,24 @@ use CodeGenerator\FunctionBlock; -class CG_FunctionTest extends \PHPUnit_Framework_TestCase { - - public function testExtractFromClosure() { +class FunctionTest extends \PHPUnit_Framework_TestCase +{ + public function testExtractFromClosure() + { $closure = function ($a, $b) { return $a * $b; }; $function = new FunctionBlock($closure); - eval('$multiply = ' . $function->dump() . ';'); - /** @var $multiply \Closure */ + eval('$multiply = '.$function->dump().';'); + /* @var $multiply \Closure */ $this->assertSame(12, $multiply(3, 4)); } - public function testSetCodeString() { + public function testSetCodeString() + { $function = new FunctionBlock('return true;'); - eval('$true = ' . $function->dump() . ';'); - /** @var $true \Closure */ + eval('$true = '.$function->dump().';'); + /* @var $true \Closure */ $this->assertTrue($true()); } } diff --git a/tests/source/CodeGenearator/InterfaceTest.php b/tests/source/CodeGenearator/InterfaceTest.php index 3988ab2..c8a56bc 100644 --- a/tests/source/CodeGenearator/InterfaceTest.php +++ b/tests/source/CodeGenearator/InterfaceTest.php @@ -6,7 +6,7 @@ use CodeGenerator\FileBlock; use CodeGenerator\InterfaceBlock; -class CG_InterfaceTest extends \PHPUnit_Framework_TestCase +class InterfaceTest extends \PHPUnit_Framework_TestCase { public function testDump() { diff --git a/tests/source/CodeGenearator/TraitTest.php b/tests/source/CodeGenearator/TraitTest.php index c244720..53181d3 100644 --- a/tests/source/CodeGenearator/TraitTest.php +++ b/tests/source/CodeGenearator/TraitTest.php @@ -6,7 +6,7 @@ use CodeGenerator\MethodBlock; use CodeGenerator\TraitBlock; -class CG_TraitTest extends \PHPUnit_Framework_TestCase +class TraitTest extends \PHPUnit_Framework_TestCase { public function testDump() { @@ -24,7 +24,7 @@ public function testDump() public function testDumpComposite() { - /** + /* * Traits that contain USE are treated like the contain the methods and properties themselves * might not be the best solution to handle them like this */ @@ -97,7 +97,6 @@ public function otherMethod() { $this->assertSame($expected, $actual); } - public function testGetName() { $className = 'Foo'; From fba94d5941a0290b2f1fc6990532f532bfe7021c Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 23 Jul 2015 15:38:47 +0200 Subject: [PATCH 09/30] formatting --- source/CodeGenerator/ArrayBlock.php | 10 +- source/CodeGenerator/Block.php | 82 +++++---- source/CodeGenerator/ClassBlock.php | 174 +++++++++--------- source/CodeGenerator/ConstantBlock.php | 6 +- source/CodeGenerator/FileBlock.php | 4 +- source/CodeGenerator/FunctionBlock.php | 131 +++++++------ source/CodeGenerator/InterfaceBlock.php | 44 ++--- source/CodeGenerator/InterfaceMethodBlock.php | 26 +-- source/CodeGenerator/MethodBlock.php | 44 ++--- source/CodeGenerator/ParameterBlock.php | 88 ++++----- source/CodeGenerator/PropertyBlock.php | 144 +++++++-------- source/CodeGenerator/TraitBlock.php | 80 ++++---- tests/bootstrap.php | 6 +- tests/helpers/TestHelper.php | 6 +- tests/mocks/MockClass.php | 12 +- tests/mocks/MockCompositeTrait.php | 9 - tests/mocks/MockInterface.php | 2 +- tests/mocks/MockTrait.php | 12 +- tests/source/CodeGenearator/ArrayTest.php | 24 +-- tests/source/CodeGenearator/BlockTest.php | 22 +-- tests/source/CodeGenearator/ClassTest.php | 4 +- tests/source/CodeGenearator/FunctionTest.php | 4 +- tests/source/CodeGenearator/TraitTest.php | 75 -------- 23 files changed, 469 insertions(+), 540 deletions(-) delete mode 100644 tests/mocks/MockCompositeTrait.php diff --git a/source/CodeGenerator/ArrayBlock.php b/source/CodeGenerator/ArrayBlock.php index 64f003e..91d6b5d 100644 --- a/source/CodeGenerator/ArrayBlock.php +++ b/source/CodeGenerator/ArrayBlock.php @@ -12,7 +12,7 @@ class ArrayBlock extends Block */ public function __construct(array $value = null) { - $this->_value = (array) $value; + $this->_value = (array)$value; } /** @@ -20,12 +20,12 @@ public function __construct(array $value = null) */ public function dump() { - $entries = array(); + $entries = []; $isAssociative = $this->isAssociative(); foreach ($this->_value as $key => $value) { $line = ''; if ($isAssociative) { - $line .= $key.' => '; + $line .= $key . ' => '; } $value = new ValueBlock($value); $line .= $value->dump(); @@ -33,7 +33,7 @@ public function dump() } $content = implode(', ', $entries); if (strlen($content) < 100) { - return 'array('.$content.')'; + return 'array(' . $content . ')'; } else { $content = implode(",\n", $entries); @@ -47,6 +47,6 @@ public function dump() public function isAssociative() { - return (bool) count(array_filter(array_keys($this->_value), 'is_string')); + return (bool)count(array_filter(array_keys($this->_value), 'is_string')); } } diff --git a/source/CodeGenerator/Block.php b/source/CodeGenerator/Block.php index 389a9a9..0c27e61 100644 --- a/source/CodeGenerator/Block.php +++ b/source/CodeGenerator/Block.php @@ -8,15 +8,37 @@ abstract class Block protected static $_indentation = ' '; /** + * @param string $indentation + */ + public static function setIndentation($indentation) + { + self::$_indentation = (string)$indentation; + } + + /** + * @param string $className + * * @return string */ - abstract public function dump(); + protected static function _normalizeClassName($className) + { + if (strpos($className, '\\') !== 0) { + $className = '\\' . $className; + } + + return $className; + } public function __toString() { return $this->dump(); } + /** + * @return string + */ + abstract public function dump(); + /** * @param string $content * @@ -24,11 +46,11 @@ public function __toString() */ protected function _indent($content) { - return preg_replace('/(:?^|[\n])/', '$1'.self::$_indentation, $content); + return preg_replace('/(:?^|[\n])/', '$1' . self::$_indentation, $content); } /** - * @param string $content + * @param string $content * @param bool|null $untilUnsafe * * @return string @@ -41,18 +63,24 @@ protected function _outdent($content, $untilUnsafe = null) } $lines = explode(PHP_EOL, $content); if ($untilUnsafe) { - $nonemptyLines = array_filter($lines, function ($line) { - return (bool) trim($line); - }); - $unsafeLines = array_filter($nonemptyLines, function ($line) use ($indentation) { - return strpos($line, $indentation) !== 0; - }); + $nonemptyLines = array_filter( + $lines, + function ($line) { + return (bool)trim($line); + } + ); + $unsafeLines = array_filter( + $nonemptyLines, + function ($line) use ($indentation) { + return strpos($line, $indentation) !== 0; + } + ); if (count($unsafeLines) || !count($nonemptyLines)) { return $content; } } foreach ($lines as $key => $line) { - $lines[$key] = preg_replace('/^'.preg_quote(self::$_indentation).'/', '$1', $line); + $lines[$key] = preg_replace('/^' . preg_quote(self::$_indentation) . '/', '$1', $line); } $content = implode(PHP_EOL, $lines); if ($untilUnsafe) { @@ -81,30 +109,14 @@ protected function _dumpLine($line) */ protected function _dumpLines(array $lines) { - return implode(PHP_EOL, array_filter($lines, function ($element) { - return !is_null($element); - })); - } - - /** - * @param string $indentation - */ - public static function setIndentation($indentation) - { - self::$_indentation = (string) $indentation; - } - - /** - * @param string $className - * - * @return string - */ - protected static function _normalizeClassName($className) - { - if (strpos($className, '\\') !== 0) { - $className = '\\'.$className; - } - - return $className; + return implode( + PHP_EOL, + array_filter( + $lines, + function ($element) { + return !is_null($element); + } + ) + ); } } diff --git a/source/CodeGenerator/ClassBlock.php b/source/CodeGenerator/ClassBlock.php index bbf7f29..c024941 100644 --- a/source/CodeGenerator/ClassBlock.php +++ b/source/CodeGenerator/ClassBlock.php @@ -17,28 +17,28 @@ class ClassBlock extends Block private $_interfaces; /** @var string[] */ - private $_uses = array(); + private $_uses = []; /** @var ConstantBlock[] */ - private $_constants = array(); + private $_constants = []; /** @var PropertyBlock[] */ - private $_properties = array(); + private $_properties = []; /** @var MethodBlock[] */ - private $_methods = array(); + private $_methods = []; /** @var bool */ private $_abstract; /** - * @param string $name + * @param string $name * @param string|null $parentClassName - * @param array|null $interfaces + * @param array|null $interfaces */ public function __construct($name, $parentClassName = null, array $interfaces = null) { - $this->_name = (string) $name; + $this->_name = (string)$name; if (null !== $parentClassName) { $this->setParentClassName($parentClassName); } @@ -48,61 +48,90 @@ public function __construct($name, $parentClassName = null, array $interfaces = } /** - * @return string + * @param string $parentClassName */ - public function getName() + public function setParentClassName($parentClassName) { - return $this->_name; + $this->_parentClassName = (string)$parentClassName; } /** - * @param string $parentClassName + * @param string[] $interfaces */ - public function setParentClassName($parentClassName) + public function setInterfaces(array $interfaces) { - $this->_parentClassName = (string) $parentClassName; + foreach ($interfaces as $interface) { + $this->addInterface($interface); + } } /** - * @param string $namespace + * @param string $interface */ - public function setNamespace($namespace) + public function addInterface($interface) { - $this->_namespace = (string) $namespace; + $this->_interfaces[] = $interface; } - /** - * @param string[] $interfaces - */ - public function setInterfaces(array $interfaces) + public static function buildFromReflection(\ReflectionClass $reflection) { - foreach ($interfaces as $interface) { - $this->addInterface($interface); + $class = new self($reflection->getShortName()); + $class->setNamespace($reflection->getNamespaceName()); + $reflectionParentClass = $reflection->getParentClass(); + if ($reflectionParentClass) { + $class->setParentClassName($reflectionParentClass->getName()); + } + $class->setAbstract($reflection->isAbstract()); + if ($interfaces = $reflection->getInterfaceNames()) { + if ($reflectionParentClass) { + $parentInterfaces = $reflection->getParentClass()->getInterfaceNames(); + $interfaces = array_diff($interfaces, $parentInterfaces); + } + $class->setInterfaces($interfaces); + } + foreach ($reflection->getMethods() as $reflectionMethod) { + if ($reflectionMethod->getDeclaringClass() == $reflection) { + $method = MethodBlock::buildFromReflection($reflectionMethod); + $class->addMethod($method); + } } + foreach ($reflection->getProperties() as $reflectionProperty) { + if ($reflectionProperty->getDeclaringClass() == $reflection) { + $property = PropertyBlock::buildFromReflection($reflectionProperty); + $class->addProperty($property); + } + } + foreach ($reflection->getConstants() as $name => $value) { + if (!$reflection->getParentClass()->hasConstant($name)) { + $class->addConstant(new ConstantBlock($name, $value)); + } + } + + return $class; } /** - * @param bool $abstract + * @param string $namespace */ - public function setAbstract($abstract) + public function setNamespace($namespace) { - $this->_abstract = (bool) $abstract; + $this->_namespace = (string)$namespace; } /** - * @param string $name + * @param bool $abstract */ - public function addUse($name) + public function setAbstract($abstract) { - $this->_uses[] = $name; + $this->_abstract = (bool)$abstract; } /** - * @param ConstantBlock $constant + * @param MethodBlock $method */ - public function addConstant(ConstantBlock $constant) + public function addMethod(MethodBlock $method) { - $this->_constants[$constant->getName()] = $constant; + $this->_methods[$method->getName()] = $method; } /** @@ -114,19 +143,27 @@ public function addProperty(PropertyBlock $property) } /** - * @param MethodBlock $method + * @param ConstantBlock $constant */ - public function addMethod(MethodBlock $method) + public function addConstant(ConstantBlock $constant) { - $this->_methods[$method->getName()] = $method; + $this->_constants[$constant->getName()] = $constant; } /** - * @param string $interface + * @return string */ - public function addInterface($interface) + public function getName() { - $this->_interfaces[] = $interface; + return $this->_name; + } + + /** + * @param string $name + */ + public function addUse($name) + { + $this->_uses[] = $name; } /** @@ -134,7 +171,7 @@ public function addInterface($interface) */ public function dump() { - $lines = array(); + $lines = []; $lines[] = $this->_dumpHeader(); foreach ($this->_uses as $use) { $lines[] = ''; @@ -162,42 +199,42 @@ public function dump() */ private function _dumpHeader() { - $lines = array(); + $lines = []; if ($this->_namespace) { - $lines[] = 'namespace '.$this->_namespace.';'; + $lines[] = 'namespace ' . $this->_namespace . ';'; $lines[] = ''; } $classDeclaration = ''; if ($this->_abstract) { $classDeclaration .= 'abstract '; } - $classDeclaration .= 'class '.$this->_name; + $classDeclaration .= 'class ' . $this->_name; if ($this->_parentClassName) { - $classDeclaration .= ' extends '.$this->_getParentClassName(); + $classDeclaration .= ' extends ' . $this->_getParentClassName(); } if ($this->_interfaces) { - $classDeclaration .= ' implements '.implode(', ', $this->_getInterfaces()); + $classDeclaration .= ' implements ' . implode(', ', $this->_getInterfaces()); } - $classDeclaration .= ' {'; $lines[] = $classDeclaration; + $lines[] = '{'; return $this->_dumpLines($lines); } /** - * @return string[] + * @return string */ - private function _getInterfaces() + private function _getParentClassName() { - return array_map(array('\\CodeGenerator\\ClassBlock', '_normalizeClassName'), $this->_interfaces); + return self::_normalizeClassName($this->_parentClassName); } /** - * @return string + * @return string[] */ - private function _getParentClassName() + private function _getInterfaces() { - return self::_normalizeClassName($this->_parentClassName); + return array_map(['\\CodeGenerator\\ClassBlock', '_normalizeClassName'], $this->_interfaces); } /** @@ -207,41 +244,4 @@ private function _dumpFooter() { return '}'; } - - public static function buildFromReflection(\ReflectionClass $reflection) - { - $class = new self($reflection->getShortName()); - $class->setNamespace($reflection->getNamespaceName()); - $reflectionParentClass = $reflection->getParentClass(); - if ($reflectionParentClass) { - $class->setParentClassName($reflectionParentClass->getName()); - } - $class->setAbstract($reflection->isAbstract()); - if ($interfaces = $reflection->getInterfaceNames()) { - if ($reflectionParentClass) { - $parentInterfaces = $reflection->getParentClass()->getInterfaceNames(); - $interfaces = array_diff($interfaces, $parentInterfaces); - } - $class->setInterfaces($interfaces); - } - foreach ($reflection->getMethods() as $reflectionMethod) { - if ($reflectionMethod->getDeclaringClass() == $reflection) { - $method = MethodBlock::buildFromReflection($reflectionMethod); - $class->addMethod($method); - } - } - foreach ($reflection->getProperties() as $reflectionProperty) { - if ($reflectionProperty->getDeclaringClass() == $reflection) { - $property = PropertyBlock::buildFromReflection($reflectionProperty); - $class->addProperty($property); - } - } - foreach ($reflection->getConstants() as $name => $value) { - if (!$reflection->getParentClass()->hasConstant($name)) { - $class->addConstant(new ConstantBlock($name, $value)); - } - } - - return $class; - } } diff --git a/source/CodeGenerator/ConstantBlock.php b/source/CodeGenerator/ConstantBlock.php index 1654185..5697997 100644 --- a/source/CodeGenerator/ConstantBlock.php +++ b/source/CodeGenerator/ConstantBlock.php @@ -11,12 +11,12 @@ class ConstantBlock extends Block private $_value; /** - * @param string $name + * @param string $name * @param string|int $value */ public function __construct($name, $value) { - $this->_name = (string) $name; + $this->_name = (string)$name; $this->_value = $value; } @@ -30,6 +30,6 @@ public function getName() public function dump() { - return 'const '.$this->_name.' = '.var_export($this->_value, true).';'; + return 'const ' . $this->_name . ' = ' . var_export($this->_value, true) . ';'; } } diff --git a/source/CodeGenerator/FileBlock.php b/source/CodeGenerator/FileBlock.php index 6dea7e8..469f904 100644 --- a/source/CodeGenerator/FileBlock.php +++ b/source/CodeGenerator/FileBlock.php @@ -5,7 +5,7 @@ class FileBlock extends Block { /** @var Block[] */ - private $_blocks = array(); + private $_blocks = []; /** * @param Block $block @@ -17,7 +17,7 @@ public function addBlock(Block $block) public function dump() { - $lines = array(); + $lines = []; $lines[] = '_blocks as $block) { $lines[] = ''; diff --git a/source/CodeGenerator/FunctionBlock.php b/source/CodeGenerator/FunctionBlock.php index edbc93b..04a9bcc 100644 --- a/source/CodeGenerator/FunctionBlock.php +++ b/source/CodeGenerator/FunctionBlock.php @@ -6,15 +6,12 @@ class FunctionBlock extends Block { /** @var string|null */ protected $_name; - - /** @var ParameterBlock[] */ - private $_parameters = array(); - /** @var string */ protected $_code; - /** @var string|null */ protected $_docBlock; + /** @var ParameterBlock[] */ + private $_parameters = []; /** * @param callable|string|null $body @@ -31,54 +28,21 @@ public function __construct($body = null) } /** - * @param string $name - */ - public function setName($name) - { - $this->_name = (string) $name; - } - - /** - * @return string|null - */ - public function getName() - { - return $this->_name; - } - - /** - * @param ParameterBlock $parameter - * - * @throws \Exception - */ - public function addParameter(ParameterBlock $parameter) - { - if (array_key_exists($parameter->getName(), $this->_parameters)) { - throw new \Exception('Parameter `'.$parameter->getName().'` is already set.'); - } - $this->_parameters[$parameter->getName()] = $parameter; - } - - /** - * @param string $code + * @param \Closure $closure */ - public function setCode($code) + public function extractFromClosure(\Closure $closure) { - if (null !== $code) { - $code = $this->_outdent((string) $code, true); - } - $this->_code = $code; + $this->extractFromReflection(new \ReflectionFunction($closure)); } /** - * @param string|null $docBlock + * @param \ReflectionFunctionAbstract $reflection */ - public function setDocBlock($docBlock) + public function extractFromReflection(\ReflectionFunctionAbstract $reflection) { - if (null !== $docBlock) { - $docBlock = (string) $docBlock; - } - $this->_docBlock = $docBlock; + $this->setBodyFromReflection($reflection); + $this->setParametersFromReflection($reflection); + $this->setDocBlockFromReflection($reflection); } /** @@ -116,6 +80,17 @@ public function setBodyFromReflection(\ReflectionFunctionAbstract $reflection) $this->setCode($code); } + /** + * @param string $code + */ + public function setCode($code) + { + if (null !== $code) { + $code = $this->_outdent((string)$code, true); + } + $this->_code = $code; + } + /** * @param \ReflectionFunctionAbstract $reflection */ @@ -127,6 +102,19 @@ public function setParametersFromReflection(\ReflectionFunctionAbstract $reflect } } + /** + * @param ParameterBlock $parameter + * + * @throws \Exception + */ + public function addParameter(ParameterBlock $parameter) + { + if (array_key_exists($parameter->getName(), $this->_parameters)) { + throw new \Exception('Parameter `' . $parameter->getName() . '` is already set.'); + } + $this->_parameters[$parameter->getName()] = $parameter; + } + /** * @param \ReflectionFunctionAbstract $reflection */ @@ -134,16 +122,43 @@ public function setDocBlockFromReflection(\ReflectionFunctionAbstract $reflectio { $docBlock = $reflection->getDocComment(); if ($docBlock) { - $docBlock = preg_replace('/([\n\r])('.self::$_indentation.')+/', '$1', $docBlock); + $docBlock = preg_replace('/([\n\r])(' . self::$_indentation . ')+/', '$1', $docBlock); $this->setDocBlock($docBlock); } } + /** + * @param string|null $docBlock + */ + public function setDocBlock($docBlock) + { + if (null !== $docBlock) { + $docBlock = (string)$docBlock; + } + $this->_docBlock = $docBlock; + } + + /** + * @return string|null + */ + public function getName() + { + return $this->_name; + } + + /** + * @param string $name + */ + public function setName($name) + { + $this->_name = (string)$name; + } + public function dump() { return $this->_dumpLine( $this->_dumpDocBlock(), - $this->_dumpHeader().$this->_dumpBody() + $this->_dumpHeader() . $this->_dumpBody() ); } @@ -162,7 +177,7 @@ protected function _dumpHeader() { $content = 'function'; if ($this->_name) { - $content .= ' '.$this->_name; + $content .= ' ' . $this->_name; } $content .= '('; $content .= implode(', ', $this->_parameters); @@ -183,22 +198,4 @@ protected function _dumpBody() return $this->_dumpLine(' {', $code, '}'); } - - /** - * @param \ReflectionFunctionAbstract $reflection - */ - public function extractFromReflection(\ReflectionFunctionAbstract $reflection) - { - $this->setBodyFromReflection($reflection); - $this->setParametersFromReflection($reflection); - $this->setDocBlockFromReflection($reflection); - } - - /** - * @param \Closure $closure - */ - public function extractFromClosure(\Closure $closure) - { - $this->extractFromReflection(new \ReflectionFunction($closure)); - } } diff --git a/source/CodeGenerator/InterfaceBlock.php b/source/CodeGenerator/InterfaceBlock.php index b5e4121..6aad654 100644 --- a/source/CodeGenerator/InterfaceBlock.php +++ b/source/CodeGenerator/InterfaceBlock.php @@ -11,13 +11,13 @@ class InterfaceBlock extends Block private $_namespace; /** @var array */ - private $_parentInterfaceNames = array(); + private $_parentInterfaceNames = []; /** @var ConstantBlock[] */ - private $_constants = array(); + private $_constants = []; /** @var MethodBlock[] */ - private $_methods = array(); + private $_methods = []; /** * @param $name @@ -25,7 +25,7 @@ class InterfaceBlock extends Block */ public function __construct($name, array $parentInterfaceNames = null) { - $this->_name = (string) $name; + $this->_name = (string)$name; if (!is_null($parentInterfaceNames)) { $this->setParentInterfaceNames($parentInterfaceNames); @@ -47,7 +47,7 @@ public function setParentInterfaceNames(array $parentInterfaceNames) */ public function addParentInterfaceName($parentInterfaceName) { - $this->_parentInterfaceNames[] = (string) $parentInterfaceName; + $this->_parentInterfaceNames[] = (string)$parentInterfaceName; } /** @@ -62,7 +62,7 @@ public static function buildFromReflection(\ReflectionClass $reflection) $reflectionParentInterfaces = $reflection->getInterfaces(); if (!empty($reflectionParentInterfaces)) { - $interfaces = array(); + $interfaces = []; foreach ($reflectionParentInterfaces as $reflectionParentInterface) { $interfaces[] = $reflectionParentInterface->getName(); } @@ -94,7 +94,7 @@ public static function buildFromReflection(\ReflectionClass $reflection) */ public function setNamespace($namespace) { - $this->_namespace = (string) $namespace; + $this->_namespace = (string)$namespace; } /** @@ -105,14 +105,6 @@ public function addMethod(InterfaceMethodBlock $method) $this->_methods[$method->getName()] = $method; } - /** - * @param ConstantBlock $constant - */ - public function addConstant(ConstantBlock $constant) - { - $this->_constants[$constant->getName()] = $constant; - } - /** * @param \ReflectionClass $reflection * @@ -120,7 +112,7 @@ public function addConstant(ConstantBlock $constant) */ protected static function getAllConstantsOfParentInterfaces(\ReflectionClass $reflection) { - $parentConstants = array(); + $parentConstants = []; $parentInterfaces = $reflection->getInterfaces(); foreach ($parentInterfaces as $parentInterface) { $parentInterfaceConstants = $parentInterface->getConstants(); @@ -131,6 +123,14 @@ protected static function getAllConstantsOfParentInterfaces(\ReflectionClass $re return $parentConstants; } + /** + * @param ConstantBlock $constant + */ + public function addConstant(ConstantBlock $constant) + { + $this->_constants[$constant->getName()] = $constant; + } + /** * @return string */ @@ -144,7 +144,7 @@ public function getName() */ public function dump() { - $lines = array(); + $lines = []; $lines[] = $this->_dumpHeader(); foreach ($this->_constants as $constant) { $lines[] = ''; @@ -164,15 +164,15 @@ public function dump() */ private function _dumpHeader() { - $lines = array(); + $lines = []; if ($this->_namespace) { - $lines[] = 'namespace '.$this->_namespace.';'; + $lines[] = 'namespace ' . $this->_namespace . ';'; $lines[] = ''; } $classDeclaration = ''; - $classDeclaration .= 'interface '.$this->_name; + $classDeclaration .= 'interface ' . $this->_name; if ($this->_parentInterfaceNames) { - $classDeclaration .= ' extends '.$this->_getParentInterfaces(); + $classDeclaration .= ' extends ' . $this->_getParentInterfaces(); } $classDeclaration .= ' {'; $lines[] = $classDeclaration; @@ -185,7 +185,7 @@ private function _dumpHeader() */ private function _getParentInterfaces() { - $cleaned = array(); + $cleaned = []; foreach ($this->_parentInterfaceNames as $parentInterfaceName) { $cleaned[] = self::_normalizeClassName($parentInterfaceName); } diff --git a/source/CodeGenerator/InterfaceMethodBlock.php b/source/CodeGenerator/InterfaceMethodBlock.php index 94b8502..a6461cf 100644 --- a/source/CodeGenerator/InterfaceMethodBlock.php +++ b/source/CodeGenerator/InterfaceMethodBlock.php @@ -14,31 +14,31 @@ public function __construct($name) } /** - * @return string + * @param \ReflectionMethod $reflection + * + * @return InterfaceMethodBlock */ - protected function _dumpHeader() + public static function buildFromReflection(\ReflectionMethod $reflection) { - return 'public '.parent::_dumpHeader(); + $method = new self($reflection->getName()); + $method->extractFromReflection($reflection); + + return $method; } /** * @return string */ - protected function _dumpBody() + protected function _dumpHeader() { - return ';'; + return 'public ' . parent::_dumpHeader(); } /** - * @param \ReflectionMethod $reflection - * - * @return InterfaceMethodBlock + * @return string */ - public static function buildFromReflection(\ReflectionMethod $reflection) + protected function _dumpBody() { - $method = new self($reflection->getName()); - $method->extractFromReflection($reflection); - - return $method; + return ';'; } } diff --git a/source/CodeGenerator/MethodBlock.php b/source/CodeGenerator/MethodBlock.php index d47ff31..a4038fd 100644 --- a/source/CodeGenerator/MethodBlock.php +++ b/source/CodeGenerator/MethodBlock.php @@ -14,7 +14,7 @@ class MethodBlock extends FunctionBlock private $_abstract; /** - * @param string $name + * @param string $name * @param callable|string|null $body */ public function __construct($name, $body = null) @@ -31,7 +31,7 @@ public function __construct($name, $body = null) */ public function setVisibility($visibility) { - $this->_visibility = (string) $visibility; + $this->_visibility = (string)$visibility; } /** @@ -39,7 +39,7 @@ public function setVisibility($visibility) */ public function setStatic($static) { - $this->_static = (bool) $static; + $this->_static = (bool)$static; } /** @@ -47,7 +47,20 @@ public function setStatic($static) */ public function setAbstract($abstract) { - $this->_abstract = (bool) $abstract; + $this->_abstract = (bool)$abstract; + } + + /** + * @param \ReflectionMethod $reflection + * + * @return MethodBlock + */ + public static function buildFromReflection(\ReflectionMethod $reflection) + { + $method = new self($reflection->getName()); + $method->extractFromReflection($reflection); + + return $method; } /** @@ -82,17 +95,17 @@ public function setVisibilityFromReflection(\ReflectionMethod $reflection) /** * @param \ReflectionMethod $reflection */ - public function setAbstractFromReflection(\ReflectionMethod $reflection) + public function setStaticFromReflection(\ReflectionMethod $reflection) { - $this->setAbstract($reflection->isAbstract()); + $this->setStatic($reflection->isStatic()); } /** * @param \ReflectionMethod $reflection */ - public function setStaticFromReflection(\ReflectionMethod $reflection) + public function setAbstractFromReflection(\ReflectionMethod $reflection) { - $this->setStatic($reflection->isStatic()); + $this->setAbstract($reflection->isAbstract()); } protected function _dumpHeader() @@ -105,7 +118,7 @@ protected function _dumpHeader() if ($this->_static) { $code .= ' static'; } - $code .= ' '.parent::_dumpHeader(); + $code .= ' ' . parent::_dumpHeader(); return $code; } @@ -118,17 +131,4 @@ protected function _dumpBody() return parent::_dumpBody(); } - - /** - * @param \ReflectionMethod $reflection - * - * @return MethodBlock - */ - public static function buildFromReflection(\ReflectionMethod $reflection) - { - $method = new self($reflection->getName()); - $method->extractFromReflection($reflection); - - return $method; - } } diff --git a/source/CodeGenerator/ParameterBlock.php b/source/CodeGenerator/ParameterBlock.php index 6e73771..1b6d6f1 100644 --- a/source/CodeGenerator/ParameterBlock.php +++ b/source/CodeGenerator/ParameterBlock.php @@ -20,11 +20,11 @@ class ParameterBlock extends Block private $_passedByReference; /** - * @param string $name + * @param string $name * @param string|null $type - * @param null $optional - * @param mixed|null $defaultValue - * @param bool|null $passedByReference + * @param null $optional + * @param mixed|null $defaultValue + * @param bool|null $passedByReference * * @throws \Exception * @@ -32,18 +32,49 @@ class ParameterBlock extends Block */ public function __construct($name, $type = null, $optional = null, $defaultValue = null, $passedByReference = null) { - $this->_name = (string) $name; + $this->_name = (string)$name; if (null !== $type) { - $this->_type = (string) $type; + $this->_type = (string)$type; } - $this->_optional = (bool) $optional; + $this->_optional = (bool)$optional; if (null !== $defaultValue) { if (!$this->_optional) { throw new \Exception('Cannot set default value for non-optional parameter'); } $this->_defaultValue = $defaultValue; } - $this->_passedByReference = (bool) $passedByReference; + $this->_passedByReference = (bool)$passedByReference; + } + + /** + * @param \ReflectionParameter $reflection + * + * @return ParameterBlock + */ + public static function buildFromReflection(\ReflectionParameter $reflection) + { + $type = null; + if ($reflection->isCallable()) { + $type = 'callable'; + } + if ($reflection->isArray()) { + $type = 'array'; + } + if ($reflection->getClass()) { + $type = $reflection->getClass()->getName(); + } + $defaultValue = null; + if ($reflection->isDefaultValueAvailable()) { + $defaultValue = $reflection->getDefaultValue(); + } + + return new self( + $reflection->getName(), + $type, + $reflection->isOptional(), + $defaultValue, + $reflection->isPassedByReference() + ); } /** @@ -61,29 +92,19 @@ public function dump() { $content = ''; if ($this->_type) { - $content .= $this->_getType().' '; + $content .= $this->_getType() . ' '; } if ($this->_passedByReference) { $content .= '&'; } - $content .= '$'.$this->_name; + $content .= '$' . $this->_name; if ($this->_optional) { - $content .= ' = '.$this->_dumpDefaultValue(); + $content .= ' = ' . $this->_dumpDefaultValue(); } return $content; } - protected function _dumpDefaultValue() - { - if (null === $this->_defaultValue) { - return 'null'; - } - $value = new ValueBlock($this->_defaultValue); - - return $value->dump(); - } - /** * @return null|string */ @@ -97,28 +118,13 @@ protected function _getType() return $type; } - /** - * @param \ReflectionParameter $reflection - * - * @return ParameterBlock - */ - public static function buildFromReflection(\ReflectionParameter $reflection) + protected function _dumpDefaultValue() { - $type = null; - if ($reflection->isCallable()) { - $type = 'callable'; - } - if ($reflection->isArray()) { - $type = 'array'; - } - if ($reflection->getClass()) { - $type = $reflection->getClass()->getName(); - } - $defaultValue = null; - if ($reflection->isDefaultValueAvailable()) { - $defaultValue = $reflection->getDefaultValue(); + if (null === $this->_defaultValue) { + return 'null'; } + $value = new ValueBlock($this->_defaultValue); - return new self($reflection->getName(), $type, $reflection->isOptional(), $defaultValue, $reflection->isPassedByReference()); + return $value->dump(); } } diff --git a/source/CodeGenerator/PropertyBlock.php b/source/CodeGenerator/PropertyBlock.php index d358d05..9b7981d 100644 --- a/source/CodeGenerator/PropertyBlock.php +++ b/source/CodeGenerator/PropertyBlock.php @@ -4,68 +4,44 @@ class PropertyBlock extends Block { + /** @var string|null */ + protected $_docBlock; /** @var string */ private $_name; - /** @var string */ private $_visibility; - /** @var mixed */ private $_defaultValue; - /** @var string|null */ - protected $_docBlock; - /** * @param string $name */ public function __construct($name) { - $this->_name = (string) $name; + $this->_name = (string)$name; $this->setVisibility('public'); } - /** - * @return string - */ - public function getName() - { - return $this->_name; - } - /** * @param string $visibility */ public function setVisibility($visibility) { - $this->_visibility = (string) $visibility; + $this->_visibility = (string)$visibility; } /** - * @param mixed $value - */ - public function setDefaultValue($value) - { - $this->_defaultValue = $value; - } - - /** - * @param string|null $docBlock + * @param \ReflectionProperty $reflection + * + * @return PropertyBlock */ - public function setDocBlock($docBlock) + public static function buildFromReflection(\ReflectionProperty $reflection) { - if (null !== $docBlock) { - $docBlock = (string) $docBlock; - } - $this->_docBlock = $docBlock; - } + $property = new self($reflection->getName()); + $property->extractFromReflection($reflection); - public function dump() - { - return $this->_dumpLine( - $this->_dumpDocBlock(), - $this->_dumpValue() - ); + // $property->setDefaultValue($reflection->getValue()); + return $property; } /** @@ -79,75 +55,97 @@ public function extractFromReflection(\ReflectionProperty $reflection) } /** - * @return string + * @param \ReflectionProperty $reflection */ - protected function _dumpDocBlock() + protected function _setVisibilityFromReflection(\ReflectionProperty $reflection) { - return $this->_docBlock; + if ($reflection->isPublic()) { + $this->setVisibility('public'); + } + if ($reflection->isProtected()) { + $this->setVisibility('protected'); + } + if ($reflection->isPrivate()) { + $this->setVisibility('private'); + } } /** - * @return string + * @param \ReflectionProperty $reflection */ - protected function _dumpValue() + protected function _setDefaultValueFromReflection(\ReflectionProperty $reflection) { - $content = $this->_visibility.' $'.$this->_name; - if (null !== $this->_defaultValue) { - $value = new ValueBlock($this->_defaultValue); - $content .= ' = '.$value->dump(); + $defaultProperties = $reflection->getDeclaringClass()->getDefaultProperties(); + $value = $defaultProperties[$this->getName()]; + if (null !== $value) { + $this->setDefaultValue($value); } - $content .= ';'; + } - return $content; + /** + * @return string + */ + public function getName() + { + return $this->_name; } /** - * @param \ReflectionProperty $reflection + * @param mixed $value */ - protected function _setVisibilityFromReflection(\ReflectionProperty $reflection) + public function setDefaultValue($value) { - if ($reflection->isPublic()) { - $this->setVisibility('public'); - } - if ($reflection->isProtected()) { - $this->setVisibility('protected'); - } - if ($reflection->isPrivate()) { - $this->setVisibility('private'); - } + $this->_defaultValue = $value; } protected function _setDocBlockFromReflection(\ReflectionProperty $reflection) { $docBlock = $reflection->getDocComment(); if ($docBlock) { - $docBlock = preg_replace('/([\n\r])('.self::$_indentation.')+/', '$1', $docBlock); + $docBlock = preg_replace('/([\n\r])(' . self::$_indentation . ')+/', '$1', $docBlock); $this->setDocBlock($docBlock); } } /** - * @param \ReflectionProperty $reflection + * @param string|null $docBlock */ - protected function _setDefaultValueFromReflection(\ReflectionProperty $reflection) + public function setDocBlock($docBlock) { - $defaultProperties = $reflection->getDeclaringClass()->getDefaultProperties(); - $value = $defaultProperties[$this->getName()]; - if (null !== $value) { - $this->setDefaultValue($value); + if (null !== $docBlock) { + $docBlock = (string)$docBlock; } + $this->_docBlock = $docBlock; + } + + public function dump() + { + return $this->_dumpLine( + $this->_dumpDocBlock(), + $this->_dumpValue() + ); } /** - * @param \ReflectionProperty $reflection - * - * @return PropertyBlock + * @return string */ - public static function buildFromReflection(\ReflectionProperty $reflection) + protected function _dumpDocBlock() { - $property = new self($reflection->getName()); - $property->extractFromReflection($reflection); - // $property->setDefaultValue($reflection->getValue()); - return $property; + return $this->_docBlock; + } + + /** + * @return string + */ + protected function _dumpValue() + { + $content = $this->_visibility . ' $' . $this->_name; + if (null !== $this->_defaultValue) { + $value = new ValueBlock($this->_defaultValue); + $content .= ' = ' . $value->dump(); + } + $content .= ';'; + + return $content; } } diff --git a/source/CodeGenerator/TraitBlock.php b/source/CodeGenerator/TraitBlock.php index 93f7075..be25f6b 100644 --- a/source/CodeGenerator/TraitBlock.php +++ b/source/CodeGenerator/TraitBlock.php @@ -24,15 +24,34 @@ class TraitBlock extends Block */ public function __construct($name) { - $this->_name = (string) $name; + $this->_name = (string)$name; } /** - * @return string + * @param \ReflectionClass $reflection + * + * @return TraitBlock */ - public function getName() + public static function buildFromReflection(\ReflectionClass $reflection) { - return $this->_name; + $class = new self($reflection->getShortName()); + $class->setNamespace($reflection->getNamespaceName()); + + foreach ($reflection->getMethods() as $reflectionMethod) { + if ($reflectionMethod->getDeclaringClass() == $reflection) { + $method = MethodBlock::buildFromReflection($reflectionMethod); + $class->addMethod($method); + } + } + + foreach ($reflection->getProperties() as $reflectionProperty) { + if ($reflectionProperty->getDeclaringClass() == $reflection) { + $property = PropertyBlock::buildFromReflection($reflectionProperty); + $class->addProperty($property); + } + } + + return $class; } /** @@ -40,15 +59,15 @@ public function getName() */ public function setNamespace($namespace) { - $this->_namespace = (string) $namespace; + $this->_namespace = (string)$namespace; } /** - * @param string $name + * @param MethodBlock $method */ - public function addUse($name) + public function addMethod(MethodBlock $method) { - $this->_uses[] = $name; + $this->_methods[$method->getName()] = $method; } /** @@ -60,11 +79,19 @@ public function addProperty(PropertyBlock $property) } /** - * @param MethodBlock $method + * @return string */ - public function addMethod(MethodBlock $method) + public function getName() { - $this->_methods[$method->getName()] = $method; + return $this->_name; + } + + /** + * @param string $name + */ + public function addUse($name) + { + $this->_uses[] = $name; } /** @@ -98,10 +125,10 @@ private function _dumpHeader() { $lines = []; if ($this->_namespace) { - $lines[] = 'namespace '.$this->_namespace.';'; + $lines[] = 'namespace ' . $this->_namespace . ';'; $lines[] = ''; } - $classDeclaration = 'trait '.$this->_name; + $classDeclaration = 'trait ' . $this->_name; $classDeclaration .= ' {'; $lines[] = $classDeclaration; @@ -115,31 +142,4 @@ private function _dumpFooter() { return '}'; } - - /** - * @param \ReflectionClass $reflection - * - * @return TraitBlock - */ - public static function buildFromReflection(\ReflectionClass $reflection) - { - $class = new self($reflection->getShortName()); - $class->setNamespace($reflection->getNamespaceName()); - - foreach ($reflection->getMethods() as $reflectionMethod) { - if ($reflectionMethod->getDeclaringClass() == $reflection) { - $method = MethodBlock::buildFromReflection($reflectionMethod); - $class->addMethod($method); - } - } - - foreach ($reflection->getProperties() as $reflectionProperty) { - if ($reflectionProperty->getDeclaringClass() == $reflection) { - $property = PropertyBlock::buildFromReflection($reflectionProperty); - $class->addProperty($property); - } - } - - return $class; - } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 20f9b7e..ff79b74 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,6 +1,6 @@ addPsr4('CodeGeneratorMocks\\', __DIR__.'/mocks'); -$autoLoader->addPsr4('CodeGeneratorHelpers\\', __DIR__.'/helpers'); +$autoLoader = require __DIR__ . '/../vendor/autoload.php'; +$autoLoader->addPsr4('CodeGeneratorMocks\\', __DIR__ . '/mocks'); +$autoLoader->addPsr4('CodeGeneratorHelpers\\', __DIR__ . '/helpers'); diff --git a/tests/helpers/TestHelper.php b/tests/helpers/TestHelper.php index 939f1b4..c9fb369 100644 --- a/tests/helpers/TestHelper.php +++ b/tests/helpers/TestHelper.php @@ -7,15 +7,15 @@ class TestHelper { /** - * @param Block $object - * @param string $methodName + * @param Block $object + * @param string $methodName * @param array|null $arguments * * @return mixed */ public static function invokeMethod(Block $object, $methodName, array $arguments = null) { - $arguments = (array) $arguments; + $arguments = (array)$arguments; $reflection = new \ReflectionMethod(get_class($object), $methodName); $reflection->setAccessible(true); diff --git a/tests/mocks/MockClass.php b/tests/mocks/MockClass.php index 7785d1e..e5ab19a 100644 --- a/tests/mocks/MockClass.php +++ b/tests/mocks/MockClass.php @@ -7,13 +7,17 @@ class MockClass extends \CodeGeneratorMocks\MockAbstractClass const FOO = 1; /** @var array */ - public $foo = array(1, 2); + public $foo = [1, 2]; /** @var int */ protected $_bar = 1; private $_foo; + public static function staticMethod() + { + } + /** * @return int */ @@ -26,7 +30,7 @@ public function withTypeHinting(\Countable $countable, array $array, callable $c { } - public function defaultValues($defaultValue = null, $defaultArray = array()) + public function defaultValues($defaultValue = null, $defaultArray = []) { } @@ -44,8 +48,4 @@ private function _foo() // indentation // back } - - public static function staticMethod() - { - } } diff --git a/tests/mocks/MockCompositeTrait.php b/tests/mocks/MockCompositeTrait.php deleted file mode 100644 index 4850fb7..0000000 --- a/tests/mocks/MockCompositeTrait.php +++ /dev/null @@ -1,9 +0,0 @@ -assertNotRegExp("/\n/", $array->dump()); $this->_assertSame($value, $array); } - public function testDumpLong() - { - $value = array_fill(0, 100, 'foo'); - $array = new ArrayBlock($value); - $this->assertRegExp("/\n /", $array->dump()); - $this->assertCount(count($value) + 2, explode("\n", $array->dump())); - $this->_assertSame($value, $array); - } - /** - * @param array $expected + * @param array $expected * @param ArrayBlock $actual */ private function _assertSame(array $expected, ArrayBlock $actual) { - $code = 'return '.$actual->dump().';'; + $code = 'return ' . $actual->dump() . ';'; $evaluatedActual = eval($code); $this->assertSame($expected, $evaluatedActual); } + + public function testDumpLong() + { + $value = array_fill(0, 100, 'foo'); + $array = new ArrayBlock($value); + $this->assertRegExp("/\n /", $array->dump()); + $this->assertCount(count($value) + 2, explode("\n", $array->dump())); + $this->_assertSame($value, $array); + } } diff --git a/tests/source/CodeGenearator/BlockTest.php b/tests/source/CodeGenearator/BlockTest.php index 460d66b..8599278 100644 --- a/tests/source/CodeGenearator/BlockTest.php +++ b/tests/source/CodeGenearator/BlockTest.php @@ -11,14 +11,14 @@ class BlockTest extends \PHPUnit_Framework_TestCase public function testOutdent() { $block = new FileBlock(); - $cases = array( + $cases = [ ' foo' => 'foo', 'foo' => 'foo', " foo\nbar" => "foo\nbar", ' foo' => ' foo', - ); + ]; foreach ($cases as $input => $expected) { - $output = TestHelper::invokeMethod($block, '_outdent', array($input)); + $output = TestHelper::invokeMethod($block, '_outdent', [$input]); $this->assertSame($expected, $output); } } @@ -26,13 +26,13 @@ public function testOutdent() public function testOutdentUntilSafe() { $block = new FileBlock(); - $cases = array( + $cases = [ " foo\nbar" => " foo\nbar", " foo\n bar" => " foo\nbar", ' foo' => 'foo', - ); + ]; foreach ($cases as $input => $expected) { - $output = TestHelper::invokeMethod($block, '_outdent', array($input, true)); + $output = TestHelper::invokeMethod($block, '_outdent', [$input, true]); $this->assertSame($expected, $output); } } @@ -40,12 +40,12 @@ public function testOutdentUntilSafe() public function testIndent() { $block = new FileBlock(); - $cases = array( + $cases = [ "foo\nbar" => " foo\n bar", " foo\n bar" => " foo\n bar", - ); + ]; foreach ($cases as $input => $expected) { - $output = TestHelper::invokeMethod($block, '_indent', array($input, true)); + $output = TestHelper::invokeMethod($block, '_indent', [$input, true]); $this->assertSame($expected, $output); } } @@ -55,9 +55,9 @@ public function testSetIndentation() Block::setIndentation(' '); $block = new FileBlock(); - $output = TestHelper::invokeMethod($block, '_indent', array('foo', true)); + $output = TestHelper::invokeMethod($block, '_indent', ['foo', true]); $this->assertSame(' foo', $output); - $output = TestHelper::invokeMethod($block, '_outdent', array(" foo\n bar", true)); + $output = TestHelper::invokeMethod($block, '_outdent', [" foo\n bar", true]); $this->assertSame("foo\n bar", $output); Block::setIndentation(' '); diff --git a/tests/source/CodeGenearator/ClassTest.php b/tests/source/CodeGenearator/ClassTest.php index c3c06d2..7e91f3f 100644 --- a/tests/source/CodeGenearator/ClassTest.php +++ b/tests/source/CodeGenearator/ClassTest.php @@ -9,7 +9,7 @@ class ClassTest extends \PHPUnit_Framework_TestCase { public function testDump() { - $classes = array('CodeGeneratorMocks\\MockAbstractClass', 'CodeGeneratorMocks\\MockClass'); + $classes = ['CodeGeneratorMocks\\MockAbstractClass', 'CodeGeneratorMocks\\MockClass']; foreach ($classes as $className) { $file = new FileBlock(); @@ -26,7 +26,7 @@ public function testDump() public function testGetName() { $className = 'Foo'; - $class = new ClassBlock($className, 'Bar', array('Countable')); + $class = new ClassBlock($className, 'Bar', ['Countable']); $this->assertSame($className, $class->getName()); } } diff --git a/tests/source/CodeGenearator/FunctionTest.php b/tests/source/CodeGenearator/FunctionTest.php index d673a9b..41d1aba 100644 --- a/tests/source/CodeGenearator/FunctionTest.php +++ b/tests/source/CodeGenearator/FunctionTest.php @@ -12,7 +12,7 @@ public function testExtractFromClosure() return $a * $b; }; $function = new FunctionBlock($closure); - eval('$multiply = '.$function->dump().';'); + eval('$multiply = ' . $function->dump() . ';'); /* @var $multiply \Closure */ $this->assertSame(12, $multiply(3, 4)); } @@ -20,7 +20,7 @@ public function testExtractFromClosure() public function testSetCodeString() { $function = new FunctionBlock('return true;'); - eval('$true = '.$function->dump().';'); + eval('$true = ' . $function->dump() . ';'); /* @var $true \Closure */ $this->assertTrue($true()); } diff --git a/tests/source/CodeGenearator/TraitTest.php b/tests/source/CodeGenearator/TraitTest.php index 53181d3..bbb2ab6 100644 --- a/tests/source/CodeGenearator/TraitTest.php +++ b/tests/source/CodeGenearator/TraitTest.php @@ -22,81 +22,6 @@ public function testDump() $this->assertSame($expected, $actual); } - public function testDumpComposite() - { - /* - * Traits that contain USE are treated like the contain the methods and properties themselves - * might not be the best solution to handle them like this - */ - $file = new FileBlock(); - - $reflectionClass = new \ReflectionClass('\\CodeGeneratorMocks\\MockCompositeTrait'); - - $reflectedClass = TraitBlock::buildFromReflection($reflectionClass); - $file->addBlock($reflectedClass); - - $actual = $file->dump(); - - $expected = <<<'TEST' -foo); - } - - public function withTypeHinting(\Countable $countable, array $array, callable $callable) { - echo 1; - } - - public function defaultValues($defaultValue = null, $defaultArray = array()) { - echo 2; - } - - public function withReferenceParam(&$param) { - } - - protected function abstractMethod() { - } - - private function _foo() { - // comment - // indentation - // back - } - - public static function staticMethod() { - } - - /** - * @return bool - */ - public function otherMethod() { - return false; - } -} - -TEST; - $this->assertSame($expected, $actual); - } - public function testGetName() { $className = 'Foo'; From 4ca412bb0b486b76aab41243da82ecd70dd8b420 Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 23 Jul 2015 16:45:58 +0200 Subject: [PATCH 10/30] cs changes to the generators --- source/CodeGenerator/ArrayBlock.php | 6 +++--- source/CodeGenerator/ClassBlock.php | 12 ++++++++---- source/CodeGenerator/FunctionBlock.php | 2 +- source/CodeGenerator/InterfaceBlock.php | 14 +++++++++----- source/CodeGenerator/TraitBlock.php | 12 ++++++++---- tests/bootstrap.php | 6 +++--- tests/helpers/TestHelper.php | 6 +++--- tests/source/CodeGenearator/ArrayTest.php | 4 ++-- tests/source/CodeGenearator/FunctionTest.php | 4 ++-- tests/source/CodeGenearator/InterfaceTest.php | 10 ++++++++-- tests/source/CodeGenearator/TraitTest.php | 7 ++++--- 11 files changed, 51 insertions(+), 32 deletions(-) diff --git a/source/CodeGenerator/ArrayBlock.php b/source/CodeGenerator/ArrayBlock.php index 91d6b5d..e799835 100644 --- a/source/CodeGenerator/ArrayBlock.php +++ b/source/CodeGenerator/ArrayBlock.php @@ -33,14 +33,14 @@ public function dump() } $content = implode(', ', $entries); if (strlen($content) < 100) { - return 'array(' . $content . ')'; + return '[' . $content . ']'; } else { $content = implode(",\n", $entries); return $this->_dumpLine( - 'array(', + '[', $this->_indent($content), - ')' + ']' ); } } diff --git a/source/CodeGenerator/ClassBlock.php b/source/CodeGenerator/ClassBlock.php index c024941..d86fb20 100644 --- a/source/CodeGenerator/ClassBlock.php +++ b/source/CodeGenerator/ClassBlock.php @@ -174,21 +174,25 @@ public function dump() $lines = []; $lines[] = $this->_dumpHeader(); foreach ($this->_uses as $use) { - $lines[] = ''; $lines[] = $this->_indent("use ${use};"); + $lines[] = ''; } foreach ($this->_constants as $constant) { - $lines[] = ''; $lines[] = $this->_indent($constant->dump()); + $lines[] = ''; } foreach ($this->_properties as $property) { - $lines[] = ''; $lines[] = $this->_indent($property->dump()); + $lines[] = ''; } foreach ($this->_methods as $method) { - $lines[] = ''; $lines[] = $this->_indent($method->dump()); + $lines[] = ''; } + if (!empty($this->_uses) || !empty($this->_constants) || !empty($this->_properties) || !empty($this->_methods)) { + array_pop($lines); + } + $lines[] = $this->_dumpFooter(); return $this->_dumpLines($lines); diff --git a/source/CodeGenerator/FunctionBlock.php b/source/CodeGenerator/FunctionBlock.php index 04a9bcc..64c077a 100644 --- a/source/CodeGenerator/FunctionBlock.php +++ b/source/CodeGenerator/FunctionBlock.php @@ -196,6 +196,6 @@ protected function _dumpBody() $code = $this->_indent($code); } - return $this->_dumpLine(' {', $code, '}'); + return $this->_dumpLine('', '{', $code, '}'); } } diff --git a/source/CodeGenerator/InterfaceBlock.php b/source/CodeGenerator/InterfaceBlock.php index 6aad654..919799f 100644 --- a/source/CodeGenerator/InterfaceBlock.php +++ b/source/CodeGenerator/InterfaceBlock.php @@ -147,13 +147,17 @@ public function dump() $lines = []; $lines[] = $this->_dumpHeader(); foreach ($this->_constants as $constant) { - $lines[] = ''; $lines[] = $this->_indent($constant->dump()); + $lines[] = ''; } foreach ($this->_methods as $method) { - $lines[] = ''; $lines[] = $this->_indent($method->dump()); + $lines[] = ''; } + if (!empty($this->_constants) || !empty($this->_methods)) { + array_pop($lines); + } + $lines[] = $this->_dumpFooter(); return $this->_dumpLines($lines); @@ -169,13 +173,13 @@ private function _dumpHeader() $lines[] = 'namespace ' . $this->_namespace . ';'; $lines[] = ''; } - $classDeclaration = ''; - $classDeclaration .= 'interface ' . $this->_name; + + $classDeclaration = 'interface ' . $this->_name; if ($this->_parentInterfaceNames) { $classDeclaration .= ' extends ' . $this->_getParentInterfaces(); } - $classDeclaration .= ' {'; $lines[] = $classDeclaration; + $lines[] = '{'; return $this->_dumpLines($lines); } diff --git a/source/CodeGenerator/TraitBlock.php b/source/CodeGenerator/TraitBlock.php index be25f6b..c32e496 100644 --- a/source/CodeGenerator/TraitBlock.php +++ b/source/CodeGenerator/TraitBlock.php @@ -102,16 +102,19 @@ public function dump() $lines = []; $lines[] = $this->_dumpHeader(); foreach ($this->_uses as $use) { - $lines[] = ''; $lines[] = $this->_indent("use ${use};"); + $lines[] = ''; } foreach ($this->_properties as $property) { - $lines[] = ''; $lines[] = $this->_indent($property->dump()); + $lines[] = ''; } foreach ($this->_methods as $method) { - $lines[] = ''; $lines[] = $this->_indent($method->dump()); + $lines[] = ''; + } + if (!empty($this->_uses) || !empty($this->_properties) || !empty($this->_methods)) { + array_pop($lines); } $lines[] = $this->_dumpFooter(); @@ -129,8 +132,9 @@ private function _dumpHeader() $lines[] = ''; } $classDeclaration = 'trait ' . $this->_name; - $classDeclaration .= ' {'; + $lines[] = $classDeclaration; + $lines[] = '{'; return $this->_dumpLines($lines); } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index ff79b74..20f9b7e 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,6 +1,6 @@ addPsr4('CodeGeneratorMocks\\', __DIR__ . '/mocks'); -$autoLoader->addPsr4('CodeGeneratorHelpers\\', __DIR__ . '/helpers'); +$autoLoader = require __DIR__.'/../vendor/autoload.php'; +$autoLoader->addPsr4('CodeGeneratorMocks\\', __DIR__.'/mocks'); +$autoLoader->addPsr4('CodeGeneratorHelpers\\', __DIR__.'/helpers'); diff --git a/tests/helpers/TestHelper.php b/tests/helpers/TestHelper.php index c9fb369..939f1b4 100644 --- a/tests/helpers/TestHelper.php +++ b/tests/helpers/TestHelper.php @@ -7,15 +7,15 @@ class TestHelper { /** - * @param Block $object - * @param string $methodName + * @param Block $object + * @param string $methodName * @param array|null $arguments * * @return mixed */ public static function invokeMethod(Block $object, $methodName, array $arguments = null) { - $arguments = (array)$arguments; + $arguments = (array) $arguments; $reflection = new \ReflectionMethod(get_class($object), $methodName); $reflection->setAccessible(true); diff --git a/tests/source/CodeGenearator/ArrayTest.php b/tests/source/CodeGenearator/ArrayTest.php index 3cf4468..875f1d4 100644 --- a/tests/source/CodeGenearator/ArrayTest.php +++ b/tests/source/CodeGenearator/ArrayTest.php @@ -15,12 +15,12 @@ public function testDumpShort() } /** - * @param array $expected + * @param array $expected * @param ArrayBlock $actual */ private function _assertSame(array $expected, ArrayBlock $actual) { - $code = 'return ' . $actual->dump() . ';'; + $code = 'return '.$actual->dump().';'; $evaluatedActual = eval($code); $this->assertSame($expected, $evaluatedActual); } diff --git a/tests/source/CodeGenearator/FunctionTest.php b/tests/source/CodeGenearator/FunctionTest.php index 41d1aba..d673a9b 100644 --- a/tests/source/CodeGenearator/FunctionTest.php +++ b/tests/source/CodeGenearator/FunctionTest.php @@ -12,7 +12,7 @@ public function testExtractFromClosure() return $a * $b; }; $function = new FunctionBlock($closure); - eval('$multiply = ' . $function->dump() . ';'); + eval('$multiply = '.$function->dump().';'); /* @var $multiply \Closure */ $this->assertSame(12, $multiply(3, 4)); } @@ -20,7 +20,7 @@ public function testExtractFromClosure() public function testSetCodeString() { $function = new FunctionBlock('return true;'); - eval('$true = ' . $function->dump() . ';'); + eval('$true = '.$function->dump().';'); /* @var $true \Closure */ $this->assertTrue($true()); } diff --git a/tests/source/CodeGenearator/InterfaceTest.php b/tests/source/CodeGenearator/InterfaceTest.php index c8a56bc..0ead3a7 100644 --- a/tests/source/CodeGenearator/InterfaceTest.php +++ b/tests/source/CodeGenearator/InterfaceTest.php @@ -18,6 +18,9 @@ public function testDump() $actual = $file->dump(); $expected = file_get_contents($reflectionClass->getFileName()); + + echo $expected; + echo $actual; $this->assertSame($expected, $actual); } @@ -31,6 +34,9 @@ public function testDumpSmall() $actual = $file->dump(); $expected = file_get_contents($reflectionClass->getFileName()); + + echo $expected; + echo $actual; $this->assertSame($expected, $actual); } @@ -41,8 +47,8 @@ public function testDumpExtract() namespace CodeGeneratorMocks; -interface MockInterfaceThree { - +interface MockInterfaceThree +{ const FOO = 2; const BAR = 'test'; diff --git a/tests/source/CodeGenearator/TraitTest.php b/tests/source/CodeGenearator/TraitTest.php index bbb2ab6..6aaf1c6 100644 --- a/tests/source/CodeGenearator/TraitTest.php +++ b/tests/source/CodeGenearator/TraitTest.php @@ -42,11 +42,12 @@ public function testByHand() $expected = << Date: Thu, 23 Jul 2015 16:56:09 +0200 Subject: [PATCH 11/30] fixed test bugs resulting from naming problems in namespaces --- source/CodeGenerator/ClassBlock.php | 2 +- tests/mocks/MockAbstractClass.php | 2 +- tests/mocks/MockClass.php | 2 +- tests/mocks/MockInterface.php | 4 ++-- tests/mocks/MockInterfaceTwo.php | 2 +- tests/mocks/MockTrait.php | 2 +- tests/mocks/MockTraitTwo.php | 2 +- tests/source/CodeGenearator/InterfaceTest.php | 6 ------ 8 files changed, 8 insertions(+), 14 deletions(-) diff --git a/source/CodeGenerator/ClassBlock.php b/source/CodeGenerator/ClassBlock.php index d86fb20..e841eeb 100644 --- a/source/CodeGenerator/ClassBlock.php +++ b/source/CodeGenerator/ClassBlock.php @@ -102,7 +102,7 @@ public static function buildFromReflection(\ReflectionClass $reflection) } } foreach ($reflection->getConstants() as $name => $value) { - if (!$reflection->getParentClass()->hasConstant($name)) { + if (!$reflection->getParentClass() || ($reflection->getParentClass() && !$reflection->getParentClass()->hasConstant($name))) { $class->addConstant(new ConstantBlock($name, $value)); } } diff --git a/tests/mocks/MockAbstractClass.php b/tests/mocks/MockAbstractClass.php index 0ff113a..1705948 100644 --- a/tests/mocks/MockAbstractClass.php +++ b/tests/mocks/MockAbstractClass.php @@ -1,6 +1,6 @@ dump(); $expected = file_get_contents($reflectionClass->getFileName()); - - echo $expected; - echo $actual; $this->assertSame($expected, $actual); } @@ -34,9 +31,6 @@ public function testDumpSmall() $actual = $file->dump(); $expected = file_get_contents($reflectionClass->getFileName()); - - echo $expected; - echo $actual; $this->assertSame($expected, $actual); } From 4d6a3560b6699d8fbd7b9f590a9f27afcb3fae47 Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 23 Jul 2015 16:59:25 +0200 Subject: [PATCH 12/30] complied name to cs fix --- tests/bootstrap.php | 2 +- tests/mocks/MockAbstractClass.php | 2 +- tests/mocks/MockClass.php | 4 ++-- tests/mocks/MockInterface.php | 2 +- tests/mocks/MockInterfaceTwo.php | 2 +- tests/mocks/MockTrait.php | 2 +- tests/mocks/MockTraitTwo.php | 2 +- tests/source/CodeGenearator/ClassTest.php | 2 +- tests/source/CodeGenearator/InterfaceTest.php | 10 +++++----- tests/source/CodeGenearator/TraitTest.php | 6 +++--- 10 files changed, 17 insertions(+), 17 deletions(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 20f9b7e..719bbeb 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -2,5 +2,5 @@ /** @var \Composer\Autoload\ClassLoader $autoLoader */ $autoLoader = require __DIR__.'/../vendor/autoload.php'; -$autoLoader->addPsr4('CodeGeneratorMocks\\', __DIR__.'/mocks'); +$autoLoader->addPsr4('CodeGeneratormocks\\', __DIR__.'/mocks'); $autoLoader->addPsr4('CodeGeneratorHelpers\\', __DIR__.'/helpers'); diff --git a/tests/mocks/MockAbstractClass.php b/tests/mocks/MockAbstractClass.php index 1705948..0ff113a 100644 --- a/tests/mocks/MockAbstractClass.php +++ b/tests/mocks/MockAbstractClass.php @@ -1,6 +1,6 @@ addBlock($reflectedClass); @@ -25,7 +25,7 @@ public function testDumpSmall() { $file = new FileBlock(); - $reflectionClass = new \ReflectionClass('CodeGeneratorMocks\\MockInterfaceTwo'); + $reflectionClass = new \ReflectionClass('CodeGeneratormocks\\MockInterfaceTwo'); $reflectedClass = InterfaceBlock::buildFromReflection($reflectionClass); $file->addBlock($reflectedClass); @@ -39,7 +39,7 @@ public function testDumpExtract() $expected = <<addBlock($interface); - $interface->setNamespace('CodeGeneratorMocks'); - $interface->extractConstantsFromOtherClassOrInterface('CodeGeneratorMocks\\MockInterfaceTwo'); + $interface->setNamespace('CodeGeneratormocks'); + $interface->extractConstantsFromOtherClassOrInterface('CodeGeneratormocks\\MockInterfaceTwo'); $interface->addConstant(new ConstantBlock('BAR', 'test')); $actual = $file->dump(); diff --git a/tests/source/CodeGenearator/TraitTest.php b/tests/source/CodeGenearator/TraitTest.php index 6aaf1c6..351cd20 100644 --- a/tests/source/CodeGenearator/TraitTest.php +++ b/tests/source/CodeGenearator/TraitTest.php @@ -12,7 +12,7 @@ public function testDump() { $file = new FileBlock(); - $reflectionClass = new \ReflectionClass('\\CodeGeneratorMocks\\MockTrait'); + $reflectionClass = new \ReflectionClass('\\CodeGeneratormocks\\MockTrait'); $reflectedClass = TraitBlock::buildFromReflection($reflectionClass); $file->addBlock($reflectedClass); @@ -34,7 +34,7 @@ public function testByHand() $file = new FileBlock(); $trait = new TraitBlock('TestTrait'); - $trait->addUse('\\CodeGeneratorMocks\\MockCompositeTrait'); + $trait->addUse('\\CodeGeneratormocks\\MockCompositeTrait'); $trait->addMethod(new MethodBlock('testMethod', 'echo 1;')); $file->addBlock($trait); @@ -44,7 +44,7 @@ public function testByHand() trait TestTrait { - use \CodeGeneratorMocks\MockCompositeTrait; + use \CodeGeneratormocks\MockCompositeTrait; public function testMethod() { From b6a737be8a0cd41d30feee36f9c65a6208eb7ce0 Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 23 Jul 2015 17:15:26 +0200 Subject: [PATCH 13/30] Release 0.5.0 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 1797cb2..6a610a5 100644 --- a/composer.json +++ b/composer.json @@ -8,9 +8,9 @@ ], "require": { "php": ">=5.4", - "fabpot/php-cs-fixer": "^1.9" }, "require-dev": { + "fabpot/php-cs-fixer": "^1.9", "phpunit/phpunit": "~3.7.10", "satooshi/php-coveralls": "~0.6.1" }, From 51b2d79b53cef3990c3439215fb354959b34ab39 Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 23 Jul 2015 17:15:57 +0200 Subject: [PATCH 14/30] Release 0.5.0 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 6a610a5..551056b 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ } ], "require": { - "php": ">=5.4", + "php": ">=5.4" }, "require-dev": { "fabpot/php-cs-fixer": "^1.9", From 56c73ef653225f4f5d9121fbfd91f274b17122e2 Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 23 Jul 2015 17:28:53 +0200 Subject: [PATCH 15/30] added static properties --- source/CodeGenerator/PropertyBlock.php | 26 +++++++++++++++++++++++++- tests/mocks/MockClass.php | 3 +++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/source/CodeGenerator/PropertyBlock.php b/source/CodeGenerator/PropertyBlock.php index c18f621..4e46024 100644 --- a/source/CodeGenerator/PropertyBlock.php +++ b/source/CodeGenerator/PropertyBlock.php @@ -10,6 +10,9 @@ class PropertyBlock extends Block { /** @var string */ private $_visibility; + /** @var bool */ + private $_static; + /** @var mixed */ private $_defaultValue; @@ -22,6 +25,7 @@ class PropertyBlock extends Block { public function __construct($name) { $this->_name = (string) $name; $this->setVisibility('public'); + $this->setStatic(false); } /** @@ -38,6 +42,14 @@ public function setVisibility($visibility) { $this->_visibility = (string) $visibility; } + /** + * @param boolean $static + */ + public function setStatic($static) + { + $this->_static = (bool)$static; + } + /** * @param mixed $value */ @@ -66,6 +78,7 @@ public function dump() { * @param \ReflectionProperty $reflection */ public function extractFromReflection(\ReflectionProperty $reflection) { + $this->_setStaticFromReflection($reflection); $this->_setVisibilityFromReflection($reflection); $this->_setDefaultValueFromReflection($reflection); $this->_setDocBlockFromReflection($reflection); @@ -82,7 +95,9 @@ protected function _dumpDocBlock() { * @return string */ protected function _dumpValue() { - $content = $this->_visibility . ' $' . $this->_name; + $content = $this->_visibility; + $content .= ($this->_static) ? ' static' : ''; + $content .= ' $' . $this->_name; if (null !== $this->_defaultValue) { $value = new ValueBlock($this->_defaultValue); $content .= ' = ' . $value->dump(); @@ -106,6 +121,15 @@ protected function _setVisibilityFromReflection(\ReflectionProperty $reflection) } } + /** + * @param \ReflectionProperty $reflection + */ + protected function _setStaticFromReflection(\ReflectionProperty $reflection) { + if ($reflection->isStatic()) { + $this->setStatic(true); + } + } + protected function _setDocBlockFromReflection(\ReflectionProperty $reflection) { $docBlock = $reflection->getDocComment(); if ($docBlock) { diff --git a/tests/mocks/MockClass.php b/tests/mocks/MockClass.php index a8805ae..a541a5d 100644 --- a/tests/mocks/MockClass.php +++ b/tests/mocks/MockClass.php @@ -9,6 +9,9 @@ class MockClass extends \CodeGeneratorMocks\MockAbstractClass { /** @var array */ public $foo = array(1, 2); + /** @var array */ + public static $bar = array(1, 2); + /** @var int */ protected $_bar = 1; From 0394fcac463283125afd658118d55de0f884e2a0 Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Wed, 21 Oct 2015 16:58:55 +0200 Subject: [PATCH 16/30] - changed dumo behavior --- source/CodeGenerator/ArrayBlock.php | 2 +- source/CodeGenerator/Block.php | 19 ++- source/CodeGenerator/ClassBlock.php | 2 +- source/CodeGenerator/ConstantBlock.php | 2 +- source/CodeGenerator/DocBlock.php | 121 +++++++++++++++++++ source/CodeGenerator/FileBlock.php | 2 +- source/CodeGenerator/FunctionBlock.php | 2 +- source/CodeGenerator/InterfaceBlock.php | 2 +- source/CodeGenerator/ParameterBlock.php | 2 +- source/CodeGenerator/PropertyBlock.php | 2 +- source/CodeGenerator/TraitBlock.php | 2 +- source/CodeGenerator/ValueBlock.php | 2 +- tests/mocks/MockDocBlock.php | 6 + tests/source/CodeGenearator/DocBlockTest.php | 21 ++++ 14 files changed, 176 insertions(+), 11 deletions(-) create mode 100644 source/CodeGenerator/DocBlock.php create mode 100644 tests/mocks/MockDocBlock.php create mode 100644 tests/source/CodeGenearator/DocBlockTest.php diff --git a/source/CodeGenerator/ArrayBlock.php b/source/CodeGenerator/ArrayBlock.php index e799835..5075239 100644 --- a/source/CodeGenerator/ArrayBlock.php +++ b/source/CodeGenerator/ArrayBlock.php @@ -18,7 +18,7 @@ public function __construct(array $value = null) /** * @return string */ - public function dump() + protected function dumpContent() { $entries = []; $isAssociative = $this->isAssociative(); diff --git a/source/CodeGenerator/Block.php b/source/CodeGenerator/Block.php index 0c27e61..82d4430 100644 --- a/source/CodeGenerator/Block.php +++ b/source/CodeGenerator/Block.php @@ -4,6 +4,9 @@ abstract class Block { + /** @var DocBlock */ + protected $docBlock; + /** @var string */ protected static $_indentation = ' '; @@ -29,6 +32,9 @@ protected static function _normalizeClassName($className) return $className; } + /** + * @return string + */ public function __toString() { return $this->dump(); @@ -37,7 +43,18 @@ public function __toString() /** * @return string */ - abstract public function dump(); + public function dump() + { + if ($this->docBlock) { + return $this->docBlock . '' . $this->dumpContent(); + } + + return $this->dumpContent(); + } + /** + * @return string + */ + abstract protected function dumpContent(); /** * @param string $content diff --git a/source/CodeGenerator/ClassBlock.php b/source/CodeGenerator/ClassBlock.php index e841eeb..ffadada 100644 --- a/source/CodeGenerator/ClassBlock.php +++ b/source/CodeGenerator/ClassBlock.php @@ -169,7 +169,7 @@ public function addUse($name) /** * @return string */ - public function dump() + protected function dumpContent() { $lines = []; $lines[] = $this->_dumpHeader(); diff --git a/source/CodeGenerator/ConstantBlock.php b/source/CodeGenerator/ConstantBlock.php index 5697997..6bdb6a7 100644 --- a/source/CodeGenerator/ConstantBlock.php +++ b/source/CodeGenerator/ConstantBlock.php @@ -28,7 +28,7 @@ public function getName() return $this->_name; } - public function dump() + protected function dumpContent() { return 'const ' . $this->_name . ' = ' . var_export($this->_value, true) . ';'; } diff --git a/source/CodeGenerator/DocBlock.php b/source/CodeGenerator/DocBlock.php new file mode 100644 index 0000000..e58727c --- /dev/null +++ b/source/CodeGenerator/DocBlock.php @@ -0,0 +1,121 @@ +tags[] = ['tag' => '@' . $tagName, 'content' => $content]; + + return $this; + } + + /** + * @param $text + * + * @return $this + */ + public function addText($text) + { + $splitText = str_split($text, 50); + + foreach ($splitText as $line) { + $this->texts[] = $line; + } + + return $this; + } + + /** + * @return string + */ + public function dump() + { + return $this->dumpContent(); + } + + /** + * @return string + */ + protected function dumpContent() + { + $lines = []; + $lines[] = '/**'; + if (count($this->texts)) { + foreach ($this->texts as $line) { + $lines[] = ' * ' . $line; + } + $lines[] = ' *'; + } + + foreach ($this->tags as $row) { + $content = $row['tag'] . ' ' . $row['content']; + $lines[] = ' * ' . $content; + } + $lines[] = ' */'; + $lines[] = ''; + + return $this->_dumpLines($lines); + } +} diff --git a/source/CodeGenerator/FileBlock.php b/source/CodeGenerator/FileBlock.php index 469f904..17c1428 100644 --- a/source/CodeGenerator/FileBlock.php +++ b/source/CodeGenerator/FileBlock.php @@ -15,7 +15,7 @@ public function addBlock(Block $block) $this->_blocks[] = $block; } - public function dump() + protected function dumpContent() { $lines = []; $lines[] = '_name = (string)$name; } - public function dump() + protected function dumpContent() { return $this->_dumpLine( $this->_dumpDocBlock(), diff --git a/source/CodeGenerator/InterfaceBlock.php b/source/CodeGenerator/InterfaceBlock.php index 919799f..9c6245d 100644 --- a/source/CodeGenerator/InterfaceBlock.php +++ b/source/CodeGenerator/InterfaceBlock.php @@ -142,7 +142,7 @@ public function getName() /** * @return string */ - public function dump() + protected function dumpContent() { $lines = []; $lines[] = $this->_dumpHeader(); diff --git a/source/CodeGenerator/ParameterBlock.php b/source/CodeGenerator/ParameterBlock.php index 1b6d6f1..ad01eca 100644 --- a/source/CodeGenerator/ParameterBlock.php +++ b/source/CodeGenerator/ParameterBlock.php @@ -88,7 +88,7 @@ public function getName() /** * @return string */ - public function dump() + protected function dumpContent() { $content = ''; if ($this->_type) { diff --git a/source/CodeGenerator/PropertyBlock.php b/source/CodeGenerator/PropertyBlock.php index ecbdbff..16ba2ec 100644 --- a/source/CodeGenerator/PropertyBlock.php +++ b/source/CodeGenerator/PropertyBlock.php @@ -138,7 +138,7 @@ public function setDocBlock($docBlock) $this->_docBlock = $docBlock; } - public function dump() + protected function dumpContent() { return $this->_dumpLine( $this->_dumpDocBlock(), diff --git a/source/CodeGenerator/TraitBlock.php b/source/CodeGenerator/TraitBlock.php index c32e496..d2d0662 100644 --- a/source/CodeGenerator/TraitBlock.php +++ b/source/CodeGenerator/TraitBlock.php @@ -97,7 +97,7 @@ public function addUse($name) /** * @return string */ - public function dump() + protected function dumpContent() { $lines = []; $lines[] = $this->_dumpHeader(); diff --git a/source/CodeGenerator/ValueBlock.php b/source/CodeGenerator/ValueBlock.php index d6bcbc6..e8605c9 100644 --- a/source/CodeGenerator/ValueBlock.php +++ b/source/CodeGenerator/ValueBlock.php @@ -18,7 +18,7 @@ public function __construct($value) /** * @return string */ - public function dump() + protected function dumpContent() { if (is_array($this->_value)) { $array = new ArrayBlock($this->_value); diff --git a/tests/mocks/MockDocBlock.php b/tests/mocks/MockDocBlock.php new file mode 100644 index 0000000..a91cddd --- /dev/null +++ b/tests/mocks/MockDocBlock.php @@ -0,0 +1,6 @@ +/** + * Class MockDocBlock + * + * @package CodeGeneratormocks + * @author Christian Burgas + */ diff --git a/tests/source/CodeGenearator/DocBlockTest.php b/tests/source/CodeGenearator/DocBlockTest.php new file mode 100644 index 0000000..91a0a15 --- /dev/null +++ b/tests/source/CodeGenearator/DocBlockTest.php @@ -0,0 +1,21 @@ +addText('Class MockDocBlock'); + $block->addTag('package', 'CodeGeneratormocks'); + $block->addTag('author', 'Christian Burgas'); + + $expected = file_get_contents(__DIR__ . '/../../mocks/MockDocBlock.php'); + + self::assertEquals($expected, $block->dump()); + } +} From a2472406e351cd547e74ac8f76b0b47b66baac90 Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 22 Oct 2015 09:59:31 +0200 Subject: [PATCH 17/30] - refactoring and new doc block handling --- source/CodeGenerator/ArrayBlock.php | 12 +- source/CodeGenerator/Block.php | 58 ++++++--- source/CodeGenerator/ClassBlock.php | 92 +++++++------- source/CodeGenerator/ConstantBlock.php | 12 +- source/CodeGenerator/DocBlock.php | 119 +++++++++++++++--- source/CodeGenerator/FileBlock.php | 8 +- source/CodeGenerator/FunctionBlock.php | 79 ++++-------- source/CodeGenerator/InterfaceBlock.php | 60 ++++----- source/CodeGenerator/InterfaceMethodBlock.php | 6 +- source/CodeGenerator/MethodBlock.php | 28 ++--- source/CodeGenerator/ParameterBlock.php | 40 +++--- source/CodeGenerator/PropertyBlock.php | 75 ++++------- source/CodeGenerator/TraitBlock.php | 54 ++++---- source/CodeGenerator/ValueBlock.php | 10 +- tests/mocks/MockClass.php | 12 +- tests/mocks/MockDocBlock.php | 2 +- tests/mocks/MockTrait.php | 8 +- tests/source/CodeGenearator/BlockTest.php | 10 +- tests/source/CodeGenearator/DocBlockTest.php | 34 +++++ 19 files changed, 405 insertions(+), 314 deletions(-) diff --git a/source/CodeGenerator/ArrayBlock.php b/source/CodeGenerator/ArrayBlock.php index 5075239..c93adc2 100644 --- a/source/CodeGenerator/ArrayBlock.php +++ b/source/CodeGenerator/ArrayBlock.php @@ -5,14 +5,14 @@ class ArrayBlock extends Block { /** @var array */ - private $_value; + private $value; /** * @param array $value */ public function __construct(array $value = null) { - $this->_value = (array)$value; + $this->value = (array)$value; } /** @@ -22,7 +22,7 @@ protected function dumpContent() { $entries = []; $isAssociative = $this->isAssociative(); - foreach ($this->_value as $key => $value) { + foreach ($this->value as $key => $value) { $line = ''; if ($isAssociative) { $line .= $key . ' => '; @@ -37,9 +37,9 @@ protected function dumpContent() } else { $content = implode(",\n", $entries); - return $this->_dumpLine( + return $this->dumpLine( '[', - $this->_indent($content), + $this->indent($content), ']' ); } @@ -47,6 +47,6 @@ protected function dumpContent() public function isAssociative() { - return (bool)count(array_filter(array_keys($this->_value), 'is_string')); + return (bool)count(array_filter(array_keys($this->value), 'is_string')); } } diff --git a/source/CodeGenerator/Block.php b/source/CodeGenerator/Block.php index 82d4430..ca85764 100644 --- a/source/CodeGenerator/Block.php +++ b/source/CodeGenerator/Block.php @@ -4,18 +4,17 @@ abstract class Block { + /** @var string */ + protected static $indentation = ' '; /** @var DocBlock */ protected $docBlock; - /** @var string */ - protected static $_indentation = ' '; - /** * @param string $indentation */ public static function setIndentation($indentation) { - self::$_indentation = (string)$indentation; + self::$indentation = (string)$indentation; } /** @@ -23,7 +22,7 @@ public static function setIndentation($indentation) * * @return string */ - protected static function _normalizeClassName($className) + protected static function normalizeClassName($className) { if (strpos($className, '\\') !== 0) { $className = '\\' . $className; @@ -32,6 +31,18 @@ protected static function _normalizeClassName($className) return $className; } + /** + * @param DocBlock $docBlock + * + * @return $this + */ + public function setDocBlock($docBlock) + { + $this->docBlock = $docBlock; + + return $this; + } + /** * @return string */ @@ -46,11 +57,17 @@ public function __toString() public function dump() { if ($this->docBlock) { - return $this->docBlock . '' . $this->dumpContent(); + $docBlockText = $this->docBlock->dump(); + if ($docBlockText) { + $docBlockText .= PHP_EOL; + } + + return $docBlockText . '' . $this->dumpContent(); } return $this->dumpContent(); } + /** * @return string */ @@ -61,9 +78,9 @@ abstract protected function dumpContent(); * * @return string */ - protected function _indent($content) + protected function indent($content) { - return preg_replace('/(:?^|[\n])/', '$1' . self::$_indentation, $content); + return preg_replace('/(:?^|[\n])/', '$1' . self::$indentation, $content); } /** @@ -72,9 +89,9 @@ protected function _indent($content) * * @return string */ - protected function _outdent($content, $untilUnsafe = null) + protected function outdent($content, $untilUnsafe = null) { - $indentation = self::$_indentation; + $indentation = self::$indentation; if (!$indentation) { return $content; } @@ -97,11 +114,11 @@ function ($line) use ($indentation) { } } foreach ($lines as $key => $line) { - $lines[$key] = preg_replace('/^' . preg_quote(self::$_indentation) . '/', '$1', $line); + $lines[$key] = preg_replace('/^' . preg_quote(self::$indentation) . '/', '$1', $line); } $content = implode(PHP_EOL, $lines); if ($untilUnsafe) { - $content = $this->_outdent($content, $untilUnsafe); + $content = $this->outdent($content, $untilUnsafe); } return $content; @@ -112,11 +129,22 @@ function ($line) use ($indentation) { * * @return string */ - protected function _dumpLine($line) + protected function dumpLine($line) { $lines = func_get_args(); - return $this->_dumpLines($lines); + return $this->dumpLines($lines); + } + + /** + * @param \Reflector $reflection + */ + protected function setDocBlockFromReflection(\Reflector $reflection) + { + $block = DocBlock::createMethodDocBlockFromReflection($reflection); + if ($block) { + $this->setDocBlock($block); + } } /** @@ -124,7 +152,7 @@ protected function _dumpLine($line) * * @return string */ - protected function _dumpLines(array $lines) + protected function dumpLines(array $lines) { return implode( PHP_EOL, diff --git a/source/CodeGenerator/ClassBlock.php b/source/CodeGenerator/ClassBlock.php index ffadada..548245a 100644 --- a/source/CodeGenerator/ClassBlock.php +++ b/source/CodeGenerator/ClassBlock.php @@ -5,31 +5,31 @@ class ClassBlock extends Block { /** @var string */ - private $_name; + private $name; /** @var string */ - private $_namespace; + private $namespace; /** @var string */ - private $_parentClassName; + private $parentClassName; /** @var string[] */ - private $_interfaces; + private $interfaces; /** @var string[] */ - private $_uses = []; + private $uses = []; /** @var ConstantBlock[] */ - private $_constants = []; + private $constants = []; /** @var PropertyBlock[] */ - private $_properties = []; + private $properties = []; /** @var MethodBlock[] */ - private $_methods = []; + private $methods = []; /** @var bool */ - private $_abstract; + private $abstract; /** * @param string $name @@ -38,7 +38,7 @@ class ClassBlock extends Block */ public function __construct($name, $parentClassName = null, array $interfaces = null) { - $this->_name = (string)$name; + $this->name = (string)$name; if (null !== $parentClassName) { $this->setParentClassName($parentClassName); } @@ -52,7 +52,7 @@ public function __construct($name, $parentClassName = null, array $interfaces = */ public function setParentClassName($parentClassName) { - $this->_parentClassName = (string)$parentClassName; + $this->parentClassName = (string)$parentClassName; } /** @@ -70,7 +70,7 @@ public function setInterfaces(array $interfaces) */ public function addInterface($interface) { - $this->_interfaces[] = $interface; + $this->interfaces[] = $interface; } public static function buildFromReflection(\ReflectionClass $reflection) @@ -115,7 +115,7 @@ public static function buildFromReflection(\ReflectionClass $reflection) */ public function setNamespace($namespace) { - $this->_namespace = (string)$namespace; + $this->namespace = (string)$namespace; } /** @@ -123,7 +123,7 @@ public function setNamespace($namespace) */ public function setAbstract($abstract) { - $this->_abstract = (bool)$abstract; + $this->abstract = (bool)$abstract; } /** @@ -131,7 +131,7 @@ public function setAbstract($abstract) */ public function addMethod(MethodBlock $method) { - $this->_methods[$method->getName()] = $method; + $this->methods[$method->getName()] = $method; } /** @@ -139,7 +139,7 @@ public function addMethod(MethodBlock $method) */ public function addProperty(PropertyBlock $property) { - $this->_properties[$property->getName()] = $property; + $this->properties[$property->getName()] = $property; } /** @@ -147,7 +147,7 @@ public function addProperty(PropertyBlock $property) */ public function addConstant(ConstantBlock $constant) { - $this->_constants[$constant->getName()] = $constant; + $this->constants[$constant->getName()] = $constant; } /** @@ -155,7 +155,7 @@ public function addConstant(ConstantBlock $constant) */ public function getName() { - return $this->_name; + return $this->name; } /** @@ -163,7 +163,7 @@ public function getName() */ public function addUse($name) { - $this->_uses[] = $name; + $this->uses[] = $name; } /** @@ -172,79 +172,79 @@ public function addUse($name) protected function dumpContent() { $lines = []; - $lines[] = $this->_dumpHeader(); - foreach ($this->_uses as $use) { - $lines[] = $this->_indent("use ${use};"); + $lines[] = $this->dumpHeader(); + foreach ($this->uses as $use) { + $lines[] = $this->indent("use ${use};"); $lines[] = ''; } - foreach ($this->_constants as $constant) { - $lines[] = $this->_indent($constant->dump()); + foreach ($this->constants as $constant) { + $lines[] = $this->indent($constant->dump()); $lines[] = ''; } - foreach ($this->_properties as $property) { - $lines[] = $this->_indent($property->dump()); + foreach ($this->properties as $property) { + $lines[] = $this->indent($property->dump()); $lines[] = ''; } - foreach ($this->_methods as $method) { - $lines[] = $this->_indent($method->dump()); + foreach ($this->methods as $method) { + $lines[] = $this->indent($method->dump()); $lines[] = ''; } - if (!empty($this->_uses) || !empty($this->_constants) || !empty($this->_properties) || !empty($this->_methods)) { + if (!empty($this->uses) || !empty($this->constants) || !empty($this->properties) || !empty($this->methods)) { array_pop($lines); } - $lines[] = $this->_dumpFooter(); + $lines[] = $this->dumpFooter(); - return $this->_dumpLines($lines); + return $this->dumpLines($lines); } /** * @return string */ - private function _dumpHeader() + private function dumpHeader() { $lines = []; - if ($this->_namespace) { - $lines[] = 'namespace ' . $this->_namespace . ';'; + if ($this->namespace) { + $lines[] = 'namespace ' . $this->namespace . ';'; $lines[] = ''; } $classDeclaration = ''; - if ($this->_abstract) { + if ($this->abstract) { $classDeclaration .= 'abstract '; } - $classDeclaration .= 'class ' . $this->_name; - if ($this->_parentClassName) { - $classDeclaration .= ' extends ' . $this->_getParentClassName(); + $classDeclaration .= 'class ' . $this->name; + if ($this->parentClassName) { + $classDeclaration .= ' extends ' . $this->getParentClassName(); } - if ($this->_interfaces) { - $classDeclaration .= ' implements ' . implode(', ', $this->_getInterfaces()); + if ($this->interfaces) { + $classDeclaration .= ' implements ' . implode(', ', $this->getInterfaces()); } $lines[] = $classDeclaration; $lines[] = '{'; - return $this->_dumpLines($lines); + return $this->dumpLines($lines); } /** * @return string */ - private function _getParentClassName() + private function getParentClassName() { - return self::_normalizeClassName($this->_parentClassName); + return self::normalizeClassName($this->parentClassName); } /** * @return string[] */ - private function _getInterfaces() + private function getInterfaces() { - return array_map(['\\CodeGenerator\\ClassBlock', '_normalizeClassName'], $this->_interfaces); + return array_map(['\\CodeGenerator\\ClassBlock', 'normalizeClassName'], $this->interfaces); } /** * @return string */ - private function _dumpFooter() + private function dumpFooter() { return '}'; } diff --git a/source/CodeGenerator/ConstantBlock.php b/source/CodeGenerator/ConstantBlock.php index 6bdb6a7..11dab15 100644 --- a/source/CodeGenerator/ConstantBlock.php +++ b/source/CodeGenerator/ConstantBlock.php @@ -5,10 +5,10 @@ class ConstantBlock extends Block { /** @var string */ - private $_name; + private $name; /** @var string|int */ - private $_value; + private $value; /** * @param string $name @@ -16,8 +16,8 @@ class ConstantBlock extends Block */ public function __construct($name, $value) { - $this->_name = (string)$name; - $this->_value = $value; + $this->name = (string)$name; + $this->value = $value; } /** @@ -25,11 +25,11 @@ public function __construct($name, $value) */ public function getName() { - return $this->_name; + return $this->name; } protected function dumpContent() { - return 'const ' . $this->_name . ' = ' . var_export($this->_value, true) . ';'; + return 'const ' . $this->name . ' = ' . var_export($this->value, true) . ';'; } } diff --git a/source/CodeGenerator/DocBlock.php b/source/CodeGenerator/DocBlock.php index e58727c..6445c18 100644 --- a/source/CodeGenerator/DocBlock.php +++ b/source/CodeGenerator/DocBlock.php @@ -4,6 +4,10 @@ class DocBlock extends Block { + const DOC_BLOCK_START = '/**'; + const DOC_BLOCK_END = '*/'; + const DOC_BLOCK_INDENT = '*'; + /** * @var array */ @@ -51,24 +55,74 @@ class DocBlock extends Block protected $tags = []; /** - * @param $tagName - * @param $content + * @param \Reflector $reflection * - * @return $this + * @return DocBlock|null */ - public function addTag($tagName, $content) + public static function createMethodDocBlockFromReflection(\Reflector $reflection) { - if (substr($tagName, 0, 1) === '0') { - $tagName = substr($tagName, 1); + $isClass = $reflection instanceof \ReflectionClass; + $isFunction = $reflection instanceof \ReflectionFunctionAbstract; + $isProperty = $reflection instanceof \ReflectionProperty; + + if (!$isClass && !$isFunction && !$isProperty) { + return null; + } + /** @var \ReflectionClass $reflection */ + $string = $reflection->getDocComment(); + if (!$string) { + return null; } - if (!in_array($tagName, self::$allowedTags, true)) { - return $this; + return self::createDocBlockFromCommentString($string); + } + + /** + * @param $string + * + * @return DocBlock + */ + public static function createDocBlockFromCommentString($string) + { + + // cleanup + $string = trim($string); + if (substr($string, 0, 3) === self::DOC_BLOCK_START) { + $string = substr($string, 3); + } + if (substr($string, -2) === self::DOC_BLOCK_END) { + $string = substr($string, 0, strlen($string) - strlen(self::DOC_BLOCK_END)); + } + $string = rtrim($string); + $string = trim($string, "\n"); + + $block = new self(); + $lines = explode(PHP_EOL, $string); + + foreach ($lines as $line) { + if (self::isTag($line) && preg_match('/(\s?\*\s?)*(@([\w]+))(?=\s|$)/', $line, $matches)) { + $tag = $matches[3]; + $content = trim(str_replace($matches[0], '', $line)); + $block->addText('@' . $tag . ' ' . $content); + } else { + if (!preg_match('/(\s?\*\s?)(.*)/', $line, $matches)) { + continue; + } + $block->addText($matches[2]); + } } - $this->tags[] = ['tag' => '@' . $tagName, 'content' => $content]; + return $block; + } - return $this; + /** + * @param $tagName + * + * @return bool + */ + protected static function isTag($tagName) + { + return 0 !== preg_match('/(^[\\*\s*]*@)/', $tagName); } /** @@ -78,7 +132,7 @@ public function addTag($tagName, $content) */ public function addText($text) { - $splitText = str_split($text, 50); + $splitText = str_split($text, 72); foreach ($splitText as $line) { $this->texts[] = $line; @@ -87,6 +141,26 @@ public function addText($text) return $this; } + /** + * @param $tagName + * @param $content + * + * @return $this + */ + public function addTag($tagName, $content) + { + if (substr($tagName, 0, 1) === '@') { + $tagName = substr($tagName, 1); + } + if (!in_array($tagName, self::$allowedTags, true)) { + return $this; + } + + $this->tags[] = ['tag' => '@' . $tagName, 'content' => $content]; + + return $this; + } + /** * @return string */ @@ -100,22 +174,29 @@ public function dump() */ protected function dumpContent() { + if (!count($this->texts) && !count($this->tags)) { + return ''; + } + $lines = []; - $lines[] = '/**'; + $lines[] = self::DOC_BLOCK_START; if (count($this->texts)) { + $line = ''; foreach ($this->texts as $line) { - $lines[] = ' * ' . $line; + $lines[] = rtrim(' * ' . $line); + } + if ($line !== '' && count($this->tags)) { + // if last line was already empty, do not add another one + $lines[] = ' ' . self::DOC_BLOCK_INDENT; } - $lines[] = ' *'; } foreach ($this->tags as $row) { - $content = $row['tag'] . ' ' . $row['content']; - $lines[] = ' * ' . $content; + $content = ' ' . $row['tag'] . ' ' . $row['content']; + $lines[] = ' ' . self::DOC_BLOCK_INDENT . $content; } - $lines[] = ' */'; - $lines[] = ''; + $lines[] = ' ' . self::DOC_BLOCK_END; - return $this->_dumpLines($lines); + return $this->dumpLines($lines); } } diff --git a/source/CodeGenerator/FileBlock.php b/source/CodeGenerator/FileBlock.php index 17c1428..a69bc18 100644 --- a/source/CodeGenerator/FileBlock.php +++ b/source/CodeGenerator/FileBlock.php @@ -5,26 +5,26 @@ class FileBlock extends Block { /** @var Block[] */ - private $_blocks = []; + private $blocks = []; /** * @param Block $block */ public function addBlock(Block $block) { - $this->_blocks[] = $block; + $this->blocks[] = $block; } protected function dumpContent() { $lines = []; $lines[] = '_blocks as $block) { + foreach ($this->blocks as $block) { $lines[] = ''; $lines[] = $block->dump(); } $lines[] = ''; - return $this->_dumpLines($lines); + return $this->dumpLines($lines); } } diff --git a/source/CodeGenerator/FunctionBlock.php b/source/CodeGenerator/FunctionBlock.php index 7771445..c8e7e72 100644 --- a/source/CodeGenerator/FunctionBlock.php +++ b/source/CodeGenerator/FunctionBlock.php @@ -5,13 +5,11 @@ class FunctionBlock extends Block { /** @var string|null */ - protected $_name; + protected $name; /** @var string */ - protected $_code; - /** @var string|null */ - protected $_docBlock; + protected $code; /** @var ParameterBlock[] */ - private $_parameters = []; + private $parameters = []; /** * @param callable|string|null $body @@ -48,11 +46,11 @@ public function extractFromReflection(\ReflectionFunctionAbstract $reflection) /** * @param \ReflectionFunctionAbstract $reflection */ - public function setBodyFromReflection(\ReflectionFunctionAbstract $reflection) + protected function setBodyFromReflection(\ReflectionFunctionAbstract $reflection) { /** @var $reflection \ReflectionMethod */ if (is_a($reflection, '\\ReflectionMethod') && $reflection->isAbstract()) { - $this->_code = null; + $this->code = null; return; } @@ -86,15 +84,15 @@ public function setBodyFromReflection(\ReflectionFunctionAbstract $reflection) public function setCode($code) { if (null !== $code) { - $code = $this->_outdent((string)$code, true); + $code = $this->outdent((string)$code, true); } - $this->_code = $code; + $this->code = $code; } /** * @param \ReflectionFunctionAbstract $reflection */ - public function setParametersFromReflection(\ReflectionFunctionAbstract $reflection) + protected function setParametersFromReflection(\ReflectionFunctionAbstract $reflection) { foreach ($reflection->getParameters() as $reflectionParameter) { $parameter = ParameterBlock::buildFromReflection($reflectionParameter); @@ -109,33 +107,10 @@ public function setParametersFromReflection(\ReflectionFunctionAbstract $reflect */ public function addParameter(ParameterBlock $parameter) { - if (array_key_exists($parameter->getName(), $this->_parameters)) { + if (array_key_exists($parameter->getName(), $this->parameters)) { throw new \Exception('Parameter `' . $parameter->getName() . '` is already set.'); } - $this->_parameters[$parameter->getName()] = $parameter; - } - - /** - * @param \ReflectionFunctionAbstract $reflection - */ - public function setDocBlockFromReflection(\ReflectionFunctionAbstract $reflection) - { - $docBlock = $reflection->getDocComment(); - if ($docBlock) { - $docBlock = preg_replace('/([\n\r])(' . self::$_indentation . ')+/', '$1', $docBlock); - $this->setDocBlock($docBlock); - } - } - - /** - * @param string|null $docBlock - */ - public function setDocBlock($docBlock) - { - if (null !== $docBlock) { - $docBlock = (string)$docBlock; - } - $this->_docBlock = $docBlock; + $this->parameters[$parameter->getName()] = $parameter; } /** @@ -143,7 +118,7 @@ public function setDocBlock($docBlock) */ public function getName() { - return $this->_name; + return $this->name; } /** @@ -151,36 +126,30 @@ public function getName() */ public function setName($name) { - $this->_name = (string)$name; - } - - protected function dumpContent() - { - return $this->_dumpLine( - $this->_dumpDocBlock(), - $this->_dumpHeader() . $this->_dumpBody() - ); + $this->name = (string)$name; } /** * @return string */ - protected function _dumpDocBlock() + protected function dumpContent() { - return $this->_docBlock; + return $this->dumpLine( + $this->dumpHeader() . $this->dumpBody() + ); } /** * @return string */ - protected function _dumpHeader() + protected function dumpHeader() { $content = 'function'; - if ($this->_name) { - $content .= ' ' . $this->_name; + if ($this->name) { + $content .= ' ' . $this->name; } $content .= '('; - $content .= implode(', ', $this->_parameters); + $content .= implode(', ', $this->parameters); $content .= ')'; return $content; @@ -189,13 +158,13 @@ protected function _dumpHeader() /** * @return string */ - protected function _dumpBody() + protected function dumpBody() { - $code = $this->_code; + $code = $this->code; if ($code) { - $code = $this->_indent($code); + $code = $this->indent($code); } - return $this->_dumpLine('', '{', $code, '}'); + return $this->dumpLine('', '{', $code, '}'); } } diff --git a/source/CodeGenerator/InterfaceBlock.php b/source/CodeGenerator/InterfaceBlock.php index 9c6245d..7e9a10e 100644 --- a/source/CodeGenerator/InterfaceBlock.php +++ b/source/CodeGenerator/InterfaceBlock.php @@ -5,19 +5,19 @@ class InterfaceBlock extends Block { /** @var string */ - private $_name; + private $name; /** @var string */ - private $_namespace; + private $namespace; /** @var array */ - private $_parentInterfaceNames = []; + private $parentInterfaceNames = []; /** @var ConstantBlock[] */ - private $_constants = []; + private $constants = []; /** @var MethodBlock[] */ - private $_methods = []; + private $methods = []; /** * @param $name @@ -25,7 +25,7 @@ class InterfaceBlock extends Block */ public function __construct($name, array $parentInterfaceNames = null) { - $this->_name = (string)$name; + $this->name = (string)$name; if (!is_null($parentInterfaceNames)) { $this->setParentInterfaceNames($parentInterfaceNames); @@ -47,7 +47,7 @@ public function setParentInterfaceNames(array $parentInterfaceNames) */ public function addParentInterfaceName($parentInterfaceName) { - $this->_parentInterfaceNames[] = (string)$parentInterfaceName; + $this->parentInterfaceNames[] = (string)$parentInterfaceName; } /** @@ -94,7 +94,7 @@ public static function buildFromReflection(\ReflectionClass $reflection) */ public function setNamespace($namespace) { - $this->_namespace = (string)$namespace; + $this->namespace = (string)$namespace; } /** @@ -102,7 +102,7 @@ public function setNamespace($namespace) */ public function addMethod(InterfaceMethodBlock $method) { - $this->_methods[$method->getName()] = $method; + $this->methods[$method->getName()] = $method; } /** @@ -128,7 +128,7 @@ protected static function getAllConstantsOfParentInterfaces(\ReflectionClass $re */ public function addConstant(ConstantBlock $constant) { - $this->_constants[$constant->getName()] = $constant; + $this->constants[$constant->getName()] = $constant; } /** @@ -136,7 +136,7 @@ public function addConstant(ConstantBlock $constant) */ public function getName() { - return $this->_name; + return $this->name; } /** @@ -145,53 +145,53 @@ public function getName() protected function dumpContent() { $lines = []; - $lines[] = $this->_dumpHeader(); - foreach ($this->_constants as $constant) { - $lines[] = $this->_indent($constant->dump()); + $lines[] = $this->dumpHeader(); + foreach ($this->constants as $constant) { + $lines[] = $this->indent($constant->dump()); $lines[] = ''; } - foreach ($this->_methods as $method) { - $lines[] = $this->_indent($method->dump()); + foreach ($this->methods as $method) { + $lines[] = $this->indent($method->dump()); $lines[] = ''; } - if (!empty($this->_constants) || !empty($this->_methods)) { + if (!empty($this->constants) || !empty($this->methods)) { array_pop($lines); } - $lines[] = $this->_dumpFooter(); + $lines[] = $this->dumpFooter(); - return $this->_dumpLines($lines); + return $this->dumpLines($lines); } /** * @return string */ - private function _dumpHeader() + private function dumpHeader() { $lines = []; - if ($this->_namespace) { - $lines[] = 'namespace ' . $this->_namespace . ';'; + if ($this->namespace) { + $lines[] = 'namespace ' . $this->namespace . ';'; $lines[] = ''; } - $classDeclaration = 'interface ' . $this->_name; - if ($this->_parentInterfaceNames) { - $classDeclaration .= ' extends ' . $this->_getParentInterfaces(); + $classDeclaration = 'interface ' . $this->name; + if ($this->parentInterfaceNames) { + $classDeclaration .= ' extends ' . $this->getParentInterfaces(); } $lines[] = $classDeclaration; $lines[] = '{'; - return $this->_dumpLines($lines); + return $this->dumpLines($lines); } /** * @return string */ - private function _getParentInterfaces() + private function getParentInterfaces() { $cleaned = []; - foreach ($this->_parentInterfaceNames as $parentInterfaceName) { - $cleaned[] = self::_normalizeClassName($parentInterfaceName); + foreach ($this->parentInterfaceNames as $parentInterfaceName) { + $cleaned[] = self::normalizeClassName($parentInterfaceName); } return implode(', ', $cleaned); @@ -200,7 +200,7 @@ private function _getParentInterfaces() /** * @return string */ - private function _dumpFooter() + private function dumpFooter() { return '}'; } diff --git a/source/CodeGenerator/InterfaceMethodBlock.php b/source/CodeGenerator/InterfaceMethodBlock.php index a6461cf..8807b19 100644 --- a/source/CodeGenerator/InterfaceMethodBlock.php +++ b/source/CodeGenerator/InterfaceMethodBlock.php @@ -29,15 +29,15 @@ public static function buildFromReflection(\ReflectionMethod $reflection) /** * @return string */ - protected function _dumpHeader() + protected function dumpHeader() { - return 'public ' . parent::_dumpHeader(); + return 'public ' . parent::dumpHeader(); } /** * @return string */ - protected function _dumpBody() + protected function dumpBody() { return ';'; } diff --git a/source/CodeGenerator/MethodBlock.php b/source/CodeGenerator/MethodBlock.php index a4038fd..9fd9c12 100644 --- a/source/CodeGenerator/MethodBlock.php +++ b/source/CodeGenerator/MethodBlock.php @@ -5,13 +5,13 @@ class MethodBlock extends FunctionBlock { /** @var string */ - private $_visibility; + private $visibility; /** @var bool */ - private $_static; + private $static; /** @var bool */ - private $_abstract; + private $abstract; /** * @param string $name @@ -31,7 +31,7 @@ public function __construct($name, $body = null) */ public function setVisibility($visibility) { - $this->_visibility = (string)$visibility; + $this->visibility = (string)$visibility; } /** @@ -39,7 +39,7 @@ public function setVisibility($visibility) */ public function setStatic($static) { - $this->_static = (bool)$static; + $this->static = (bool)$static; } /** @@ -47,7 +47,7 @@ public function setStatic($static) */ public function setAbstract($abstract) { - $this->_abstract = (bool)$abstract; + $this->abstract = (bool)$abstract; } /** @@ -108,27 +108,27 @@ public function setAbstractFromReflection(\ReflectionMethod $reflection) $this->setAbstract($reflection->isAbstract()); } - protected function _dumpHeader() + protected function dumpHeader() { $code = ''; - if ($this->_abstract) { + if ($this->abstract) { $code .= 'abstract '; } - $code .= $this->_visibility; - if ($this->_static) { + $code .= $this->visibility; + if ($this->static) { $code .= ' static'; } - $code .= ' ' . parent::_dumpHeader(); + $code .= ' ' . parent::dumpHeader(); return $code; } - protected function _dumpBody() + protected function dumpBody() { - if ($this->_abstract) { + if ($this->abstract) { return ';'; } - return parent::_dumpBody(); + return parent::dumpBody(); } } diff --git a/source/CodeGenerator/ParameterBlock.php b/source/CodeGenerator/ParameterBlock.php index ad01eca..760f088 100644 --- a/source/CodeGenerator/ParameterBlock.php +++ b/source/CodeGenerator/ParameterBlock.php @@ -5,19 +5,19 @@ class ParameterBlock extends Block { /** @var string */ - private $_name; + private $name; /** @var string|null */ - private $_type; + private $type; /** @var mixed */ - private $_defaultValue; + private $defaultValue; /** @var bool */ - private $_optional; + private $optional; /** @var bool */ - private $_passedByReference; + private $passedByReference; /** * @param string $name @@ -32,18 +32,18 @@ class ParameterBlock extends Block */ public function __construct($name, $type = null, $optional = null, $defaultValue = null, $passedByReference = null) { - $this->_name = (string)$name; + $this->name = (string)$name; if (null !== $type) { - $this->_type = (string)$type; + $this->type = (string)$type; } - $this->_optional = (bool)$optional; + $this->optional = (bool)$optional; if (null !== $defaultValue) { - if (!$this->_optional) { + if (!$this->optional) { throw new \Exception('Cannot set default value for non-optional parameter'); } - $this->_defaultValue = $defaultValue; + $this->defaultValue = $defaultValue; } - $this->_passedByReference = (bool)$passedByReference; + $this->passedByReference = (bool)$passedByReference; } /** @@ -82,7 +82,7 @@ public static function buildFromReflection(\ReflectionParameter $reflection) */ public function getName() { - return $this->_name; + return $this->name; } /** @@ -91,14 +91,14 @@ public function getName() protected function dumpContent() { $content = ''; - if ($this->_type) { + if ($this->type) { $content .= $this->_getType() . ' '; } - if ($this->_passedByReference) { + if ($this->passedByReference) { $content .= '&'; } - $content .= '$' . $this->_name; - if ($this->_optional) { + $content .= '$' . $this->name; + if ($this->optional) { $content .= ' = ' . $this->_dumpDefaultValue(); } @@ -110,9 +110,9 @@ protected function dumpContent() */ protected function _getType() { - $type = $this->_type; + $type = $this->type; if (!in_array($type, [null, 'array', 'callable'], true)) { - $type = self::_normalizeClassName($type); + $type = self::normalizeClassName($type); } return $type; @@ -120,10 +120,10 @@ protected function _getType() protected function _dumpDefaultValue() { - if (null === $this->_defaultValue) { + if (null === $this->defaultValue) { return 'null'; } - $value = new ValueBlock($this->_defaultValue); + $value = new ValueBlock($this->defaultValue); return $value->dump(); } diff --git a/source/CodeGenerator/PropertyBlock.php b/source/CodeGenerator/PropertyBlock.php index 16ba2ec..d6d7279 100644 --- a/source/CodeGenerator/PropertyBlock.php +++ b/source/CodeGenerator/PropertyBlock.php @@ -4,25 +4,23 @@ class PropertyBlock extends Block { - /** @var string|null */ - protected $_docBlock; /** @var string */ - private $_name; + private $name; /** @var string */ - private $_visibility; + private $visibility; /** @var bool */ - private $_static; + private $static; /** @var mixed */ - private $_defaultValue; + private $defaultValue; /** * @param string $name */ public function __construct($name) { - $this->_name = (string)$name; + $this->name = (string)$name; $this->setVisibility('public'); $this->setStatic(false); } @@ -32,7 +30,7 @@ public function __construct($name) */ public function setVisibility($visibility) { - $this->_visibility = (string)$visibility; + $this->visibility = (string)$visibility; } /** @@ -40,7 +38,7 @@ public function setVisibility($visibility) */ public function setStatic($static) { - $this->_static = (bool)$static; + $this->static = (bool)$static; } /** @@ -61,13 +59,13 @@ public static function buildFromReflection(\ReflectionProperty $reflection) */ public function extractFromReflection(\ReflectionProperty $reflection) { - $this->_setStaticFromReflection($reflection); - $this->_setVisibilityFromReflection($reflection); - $this->_setDefaultValueFromReflection($reflection); - $this->_setDocBlockFromReflection($reflection); + $this->setStaticFromReflection($reflection); + $this->setVisibilityFromReflection($reflection); + $this->setDefaultValueFromReflection($reflection); + $this->setDocBlockFromReflection($reflection); } - protected function _setStaticFromReflection(\ReflectionProperty $reflection) + protected function setStaticFromReflection(\ReflectionProperty $reflection) { if ($reflection->isStatic()) { $this->setStatic(true); @@ -77,7 +75,7 @@ protected function _setStaticFromReflection(\ReflectionProperty $reflection) /** * @param \ReflectionProperty $reflection */ - protected function _setVisibilityFromReflection(\ReflectionProperty $reflection) + protected function setVisibilityFromReflection(\ReflectionProperty $reflection) { if ($reflection->isPublic()) { $this->setVisibility('public'); @@ -93,7 +91,7 @@ protected function _setVisibilityFromReflection(\ReflectionProperty $reflection) /** * @param \ReflectionProperty $reflection */ - protected function _setDefaultValueFromReflection(\ReflectionProperty $reflection) + protected function setDefaultValueFromReflection(\ReflectionProperty $reflection) { $defaultProperties = $reflection->getDeclaringClass()->getDefaultProperties(); $value = $defaultProperties[$this->getName()]; @@ -107,7 +105,7 @@ protected function _setDefaultValueFromReflection(\ReflectionProperty $reflectio */ public function getName() { - return $this->_name; + return $this->name; } /** @@ -115,55 +113,26 @@ public function getName() */ public function setDefaultValue($value) { - $this->_defaultValue = $value; - } - - protected function _setDocBlockFromReflection(\ReflectionProperty $reflection) - { - $docBlock = $reflection->getDocComment(); - if ($docBlock) { - $docBlock = preg_replace('/([\n\r])(' . self::$_indentation . ')+/', '$1', $docBlock); - $this->setDocBlock($docBlock); - } - } - - /** - * @param string|null $docBlock - */ - public function setDocBlock($docBlock) - { - if (null !== $docBlock) { - $docBlock = (string)$docBlock; - } - $this->_docBlock = $docBlock; + $this->defaultValue = $value; } protected function dumpContent() { - return $this->_dumpLine( - $this->_dumpDocBlock(), + return $this->dumpLine( $this->_dumpValue() ); } - /** - * @return string - */ - protected function _dumpDocBlock() - { - return $this->_docBlock; - } - /** * @return string */ protected function _dumpValue() { - $content = $this->_visibility; - $content .= ($this->_static) ? ' static' : ''; - $content .= ' $' . $this->_name; - if (null !== $this->_defaultValue) { - $value = new ValueBlock($this->_defaultValue); + $content = $this->visibility; + $content .= ($this->static) ? ' static' : ''; + $content .= ' $' . $this->name; + if (null !== $this->defaultValue) { + $value = new ValueBlock($this->defaultValue); $content .= ' = ' . $value->dump(); } $content .= ';'; diff --git a/source/CodeGenerator/TraitBlock.php b/source/CodeGenerator/TraitBlock.php index d2d0662..2180460 100644 --- a/source/CodeGenerator/TraitBlock.php +++ b/source/CodeGenerator/TraitBlock.php @@ -5,26 +5,26 @@ class TraitBlock extends Block { /** @var string */ - private $_name; + private $name; /** @var string */ - private $_namespace; + private $namespace; /** @var string[] */ - private $_uses = []; + private $uses = []; /** @var PropertyBlock[] */ - private $_properties = []; + private $properties = []; /** @var MethodBlock[] */ - private $_methods = []; + private $methods = []; /** * @param $name */ public function __construct($name) { - $this->_name = (string)$name; + $this->name = (string)$name; } /** @@ -59,7 +59,7 @@ public static function buildFromReflection(\ReflectionClass $reflection) */ public function setNamespace($namespace) { - $this->_namespace = (string)$namespace; + $this->namespace = (string)$namespace; } /** @@ -67,7 +67,7 @@ public function setNamespace($namespace) */ public function addMethod(MethodBlock $method) { - $this->_methods[$method->getName()] = $method; + $this->methods[$method->getName()] = $method; } /** @@ -75,7 +75,7 @@ public function addMethod(MethodBlock $method) */ public function addProperty(PropertyBlock $property) { - $this->_properties[$property->getName()] = $property; + $this->properties[$property->getName()] = $property; } /** @@ -83,7 +83,7 @@ public function addProperty(PropertyBlock $property) */ public function getName() { - return $this->_name; + return $this->name; } /** @@ -91,7 +91,7 @@ public function getName() */ public function addUse($name) { - $this->_uses[] = $name; + $this->uses[] = $name; } /** @@ -100,49 +100,49 @@ public function addUse($name) protected function dumpContent() { $lines = []; - $lines[] = $this->_dumpHeader(); - foreach ($this->_uses as $use) { - $lines[] = $this->_indent("use ${use};"); + $lines[] = $this->dumpHeader(); + foreach ($this->uses as $use) { + $lines[] = $this->indent("use ${use};"); $lines[] = ''; } - foreach ($this->_properties as $property) { - $lines[] = $this->_indent($property->dump()); + foreach ($this->properties as $property) { + $lines[] = $this->indent($property->dump()); $lines[] = ''; } - foreach ($this->_methods as $method) { - $lines[] = $this->_indent($method->dump()); + foreach ($this->methods as $method) { + $lines[] = $this->indent($method->dump()); $lines[] = ''; } - if (!empty($this->_uses) || !empty($this->_properties) || !empty($this->_methods)) { + if (!empty($this->uses) || !empty($this->properties) || !empty($this->methods)) { array_pop($lines); } - $lines[] = $this->_dumpFooter(); + $lines[] = $this->dumpFooter(); - return $this->_dumpLines($lines); + return $this->dumpLines($lines); } /** * @return string */ - private function _dumpHeader() + private function dumpHeader() { $lines = []; - if ($this->_namespace) { - $lines[] = 'namespace ' . $this->_namespace . ';'; + if ($this->namespace) { + $lines[] = 'namespace ' . $this->namespace . ';'; $lines[] = ''; } - $classDeclaration = 'trait ' . $this->_name; + $classDeclaration = 'trait ' . $this->name; $lines[] = $classDeclaration; $lines[] = '{'; - return $this->_dumpLines($lines); + return $this->dumpLines($lines); } /** * @return string */ - private function _dumpFooter() + private function dumpFooter() { return '}'; } diff --git a/source/CodeGenerator/ValueBlock.php b/source/CodeGenerator/ValueBlock.php index e8605c9..72c29c0 100644 --- a/source/CodeGenerator/ValueBlock.php +++ b/source/CodeGenerator/ValueBlock.php @@ -5,14 +5,14 @@ class ValueBlock extends Block { /** @var mixed */ - private $_value; + private $value; /** * @param mixed $value */ public function __construct($value) { - $this->_value = $value; + $this->value = $value; } /** @@ -20,12 +20,12 @@ public function __construct($value) */ protected function dumpContent() { - if (is_array($this->_value)) { - $array = new ArrayBlock($this->_value); + if (is_array($this->value)) { + $array = new ArrayBlock($this->value); return $array->dump(); } - return var_export($this->_value, true); + return var_export($this->value, true); } } diff --git a/tests/mocks/MockClass.php b/tests/mocks/MockClass.php index e4f4ac1..2cce18f 100644 --- a/tests/mocks/MockClass.php +++ b/tests/mocks/MockClass.php @@ -6,13 +6,19 @@ class MockClass extends \CodeGeneratormocks\MockAbstractClass { const FOO = 1; - /** @var array */ + /** + * @var array + */ public $foo = [1, 2]; - /** @var array */ + /** + * @var array + */ public static $bar = [1, 2]; - /** @var int */ + /** + * @var int + */ protected $_bar = 1; private $_foo; diff --git a/tests/mocks/MockDocBlock.php b/tests/mocks/MockDocBlock.php index a91cddd..ba19cf0 100644 --- a/tests/mocks/MockDocBlock.php +++ b/tests/mocks/MockDocBlock.php @@ -3,4 +3,4 @@ * * @package CodeGeneratormocks * @author Christian Burgas - */ + */ \ No newline at end of file diff --git a/tests/mocks/MockTrait.php b/tests/mocks/MockTrait.php index 2516976..2ba97ac 100644 --- a/tests/mocks/MockTrait.php +++ b/tests/mocks/MockTrait.php @@ -4,10 +4,14 @@ trait MockTrait { - /** @var array */ + /** + * @var array + */ public $foo = [1, 2]; - /** @var int */ + /** + * @var int + */ protected $_bar = 1; private $_foo; diff --git a/tests/source/CodeGenearator/BlockTest.php b/tests/source/CodeGenearator/BlockTest.php index 8599278..7e55bbe 100644 --- a/tests/source/CodeGenearator/BlockTest.php +++ b/tests/source/CodeGenearator/BlockTest.php @@ -18,7 +18,7 @@ public function testOutdent() ' foo' => ' foo', ]; foreach ($cases as $input => $expected) { - $output = TestHelper::invokeMethod($block, '_outdent', [$input]); + $output = TestHelper::invokeMethod($block, 'outdent', [$input]); $this->assertSame($expected, $output); } } @@ -32,7 +32,7 @@ public function testOutdentUntilSafe() ' foo' => 'foo', ]; foreach ($cases as $input => $expected) { - $output = TestHelper::invokeMethod($block, '_outdent', [$input, true]); + $output = TestHelper::invokeMethod($block, 'outdent', [$input, true]); $this->assertSame($expected, $output); } } @@ -45,7 +45,7 @@ public function testIndent() " foo\n bar" => " foo\n bar", ]; foreach ($cases as $input => $expected) { - $output = TestHelper::invokeMethod($block, '_indent', [$input, true]); + $output = TestHelper::invokeMethod($block, 'indent', [$input, true]); $this->assertSame($expected, $output); } } @@ -55,9 +55,9 @@ public function testSetIndentation() Block::setIndentation(' '); $block = new FileBlock(); - $output = TestHelper::invokeMethod($block, '_indent', ['foo', true]); + $output = TestHelper::invokeMethod($block, 'indent', ['foo', true]); $this->assertSame(' foo', $output); - $output = TestHelper::invokeMethod($block, '_outdent', [" foo\n bar", true]); + $output = TestHelper::invokeMethod($block, 'outdent', [" foo\n bar", true]); $this->assertSame("foo\n bar", $output); Block::setIndentation(' '); diff --git a/tests/source/CodeGenearator/DocBlockTest.php b/tests/source/CodeGenearator/DocBlockTest.php index 91a0a15..1f7afc0 100644 --- a/tests/source/CodeGenearator/DocBlockTest.php +++ b/tests/source/CodeGenearator/DocBlockTest.php @@ -6,6 +6,24 @@ class DocBlockTest extends \PHPUnit_Framework_TestCase { + protected $exampleToParseOne = <<dump()); } + + public function testFromMultiLineString() + { + $docBlock = DocBlock::createDocBlockFromCommentString($this->exampleToParseOne); + + self::assertEquals($this->exampleToParseOne, $docBlock->dump()); + } + + public function testFromString() + { + $docBlock = DocBlock::createDocBlockFromCommentString($this->exampleToParseTwo); + + self::assertEquals($this->exampleToParseTwoExpected, $docBlock->dump()); + } + + } From 62e9fa6af01500bb57e3671050e46b1443176cad Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 22 Oct 2015 10:46:14 +0200 Subject: [PATCH 18/30] Release 0.6.1 --- source/CodeGenerator/Block.php | 2 +- source/CodeGenerator/ClassBlock.php | 2 +- source/CodeGenerator/GeneratorConstants.php | 12 ++++++++++++ source/CodeGenerator/InterfaceMethodBlock.php | 2 +- source/CodeGenerator/MethodBlock.php | 12 ++++++------ source/CodeGenerator/PropertyBlock.php | 10 +++++----- 6 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 source/CodeGenerator/GeneratorConstants.php diff --git a/source/CodeGenerator/Block.php b/source/CodeGenerator/Block.php index ca85764..7f185d1 100644 --- a/source/CodeGenerator/Block.php +++ b/source/CodeGenerator/Block.php @@ -2,7 +2,7 @@ namespace CodeGenerator; -abstract class Block +abstract class Block implements GeneratorConstants { /** @var string */ protected static $indentation = ' '; diff --git a/source/CodeGenerator/ClassBlock.php b/source/CodeGenerator/ClassBlock.php index 548245a..b5b603b 100644 --- a/source/CodeGenerator/ClassBlock.php +++ b/source/CodeGenerator/ClassBlock.php @@ -210,7 +210,7 @@ private function dumpHeader() } $classDeclaration = ''; if ($this->abstract) { - $classDeclaration .= 'abstract '; + $classDeclaration .= self::KEYWORD_ABSTRACT . ' '; } $classDeclaration .= 'class ' . $this->name; if ($this->parentClassName) { diff --git a/source/CodeGenerator/GeneratorConstants.php b/source/CodeGenerator/GeneratorConstants.php new file mode 100644 index 0000000..7524364 --- /dev/null +++ b/source/CodeGenerator/GeneratorConstants.php @@ -0,0 +1,12 @@ +setName($name); - $this->setVisibility('public'); + $this->setVisibility(self::VISIBILITY_PUBLIC); $this->setStatic(false); $this->setAbstract(false); parent::__construct($body); @@ -82,13 +82,13 @@ public function extractFromReflection(\ReflectionFunctionAbstract $reflection) public function setVisibilityFromReflection(\ReflectionMethod $reflection) { if ($reflection->isPublic()) { - $this->setVisibility('public'); + $this->setVisibility(self::VISIBILITY_PUBLIC); } if ($reflection->isProtected()) { - $this->setVisibility('protected'); + $this->setVisibility(self::VISIBILITY_PROTECTED); } if ($reflection->isPrivate()) { - $this->setVisibility('private'); + $this->setVisibility(self::VISIBILITY_PRIVATE); } } @@ -112,11 +112,11 @@ protected function dumpHeader() { $code = ''; if ($this->abstract) { - $code .= 'abstract '; + $code .= self::KEYWORD_ABSTRACT . ' '; } $code .= $this->visibility; if ($this->static) { - $code .= ' static'; + $code .= ' ' . self::KEYWORD_STATIC; } $code .= ' ' . parent::dumpHeader(); diff --git a/source/CodeGenerator/PropertyBlock.php b/source/CodeGenerator/PropertyBlock.php index d6d7279..0cc5bc3 100644 --- a/source/CodeGenerator/PropertyBlock.php +++ b/source/CodeGenerator/PropertyBlock.php @@ -21,7 +21,7 @@ class PropertyBlock extends Block public function __construct($name) { $this->name = (string)$name; - $this->setVisibility('public'); + $this->setVisibility(self::VISIBILITY_PUBLIC); $this->setStatic(false); } @@ -78,13 +78,13 @@ protected function setStaticFromReflection(\ReflectionProperty $reflection) protected function setVisibilityFromReflection(\ReflectionProperty $reflection) { if ($reflection->isPublic()) { - $this->setVisibility('public'); + $this->setVisibility(self::VISIBILITY_PUBLIC); } if ($reflection->isProtected()) { - $this->setVisibility('protected'); + $this->setVisibility(self::VISIBILITY_PROTECTED); } if ($reflection->isPrivate()) { - $this->setVisibility('private'); + $this->setVisibility(self::VISIBILITY_PRIVATE); } } @@ -129,7 +129,7 @@ protected function dumpContent() protected function _dumpValue() { $content = $this->visibility; - $content .= ($this->static) ? ' static' : ''; + $content .= ($this->static) ? ' ' . self::KEYWORD_STATIC : ''; $content .= ' $' . $this->name; if (null !== $this->defaultValue) { $value = new ValueBlock($this->defaultValue); From d497a647b51bcf0e615c14c2270385aa5d0b67a7 Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 22 Oct 2015 12:07:45 +0200 Subject: [PATCH 19/30] Release 0.6.2 --- source/CodeGenerator/ParameterBlock.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/CodeGenerator/ParameterBlock.php b/source/CodeGenerator/ParameterBlock.php index 760f088..6297f01 100644 --- a/source/CodeGenerator/ParameterBlock.php +++ b/source/CodeGenerator/ParameterBlock.php @@ -92,7 +92,7 @@ protected function dumpContent() { $content = ''; if ($this->type) { - $content .= $this->_getType() . ' '; + $content .= $this->getType() . ' '; } if ($this->passedByReference) { $content .= '&'; @@ -108,7 +108,7 @@ protected function dumpContent() /** * @return null|string */ - protected function _getType() + public function getType() { $type = $this->type; if (!in_array($type, [null, 'array', 'callable'], true)) { From 0835e956077a799341dd8edfbcb90302a9e1c8ef Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 22 Oct 2015 12:21:25 +0200 Subject: [PATCH 20/30] Release 0.6.3 --- source/CodeGenerator/ClassBlock.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/source/CodeGenerator/ClassBlock.php b/source/CodeGenerator/ClassBlock.php index b5b603b..28a086d 100644 --- a/source/CodeGenerator/ClassBlock.php +++ b/source/CodeGenerator/ClassBlock.php @@ -118,6 +118,14 @@ public function setNamespace($namespace) $this->namespace = (string)$namespace; } + /** + * @return string + */ + public function getNamespace() + { + return $this->namespace; + } + /** * @param bool $abstract */ From fbfa66694483d14b2fb92749ed44e2322445eb4c Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 22 Oct 2015 14:58:22 +0200 Subject: [PATCH 21/30] - added import uses to classes --- source/CodeGenerator/ClassBlock.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/source/CodeGenerator/ClassBlock.php b/source/CodeGenerator/ClassBlock.php index 28a086d..671bbe5 100644 --- a/source/CodeGenerator/ClassBlock.php +++ b/source/CodeGenerator/ClassBlock.php @@ -19,6 +19,9 @@ class ClassBlock extends Block /** @var string[] */ private $uses = []; + /** @var string[] */ + private $importUses = []; + /** @var ConstantBlock[] */ private $constants = []; @@ -216,6 +219,12 @@ private function dumpHeader() $lines[] = 'namespace ' . $this->namespace . ';'; $lines[] = ''; } + if (count($this->importUses)) { + foreach ($this->importUses as $import) { + $lines[] = 'use ' . $import . ';'; + } + $lines[] = ''; + } $classDeclaration = ''; if ($this->abstract) { $classDeclaration .= self::KEYWORD_ABSTRACT . ' '; From 27eff964a9ad471fdcf699b26f43f2a1877f07ca Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 22 Oct 2015 15:00:43 +0200 Subject: [PATCH 22/30] - added to interface as well --- source/CodeGenerator/ClassBlock.php | 8 ++++++ source/CodeGenerator/InterfaceBlock.php | 33 ++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/source/CodeGenerator/ClassBlock.php b/source/CodeGenerator/ClassBlock.php index 671bbe5..ede0a9a 100644 --- a/source/CodeGenerator/ClassBlock.php +++ b/source/CodeGenerator/ClassBlock.php @@ -177,6 +177,14 @@ public function addUse($name) $this->uses[] = $name; } + /** + * @param string $name + */ + public function addImportUse($name) + { + $this->importUses[] = $name; + } + /** * @return string */ diff --git a/source/CodeGenerator/InterfaceBlock.php b/source/CodeGenerator/InterfaceBlock.php index 7e9a10e..7e40571 100644 --- a/source/CodeGenerator/InterfaceBlock.php +++ b/source/CodeGenerator/InterfaceBlock.php @@ -19,6 +19,12 @@ class InterfaceBlock extends Block /** @var MethodBlock[] */ private $methods = []; + /** @var string[] */ + private $uses = []; + + /** @var string[] */ + private $importUses = []; + /** * @param $name * @param array|null $parentInterfaceNames @@ -32,6 +38,22 @@ public function __construct($name, array $parentInterfaceNames = null) } } + /** + * @param string $name + */ + public function addUse($name) + { + $this->uses[] = $name; + } + + /** + * @param string $name + */ + public function addImportUse($name) + { + $this->importUses[] = $name; + } + /** * @param array $parentInterfaceNames */ @@ -146,6 +168,10 @@ protected function dumpContent() { $lines = []; $lines[] = $this->dumpHeader(); + foreach ($this->uses as $use) { + $lines[] = $this->indent("use ${use};"); + $lines[] = ''; + } foreach ($this->constants as $constant) { $lines[] = $this->indent($constant->dump()); $lines[] = ''; @@ -173,7 +199,12 @@ private function dumpHeader() $lines[] = 'namespace ' . $this->namespace . ';'; $lines[] = ''; } - + if (count($this->importUses)) { + foreach ($this->importUses as $import) { + $lines[] = 'use ' . $import . ';'; + } + $lines[] = ''; + } $classDeclaration = 'interface ' . $this->name; if ($this->parentInterfaceNames) { $classDeclaration .= ' extends ' . $this->getParentInterfaces(); From 47e74a080fcdc6b39108ab890e0417374fa9a0e8 Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 22 Oct 2015 15:40:38 +0200 Subject: [PATCH 23/30] - fixed doc block placement --- source/CodeGenerator/ClassBlock.php | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/source/CodeGenerator/ClassBlock.php b/source/CodeGenerator/ClassBlock.php index ede0a9a..bb839f6 100644 --- a/source/CodeGenerator/ClassBlock.php +++ b/source/CodeGenerator/ClassBlock.php @@ -76,6 +76,11 @@ public function addInterface($interface) $this->interfaces[] = $interface; } + /** + * @param \ReflectionClass $reflection + * + * @return ClassBlock + */ public static function buildFromReflection(\ReflectionClass $reflection) { $class = new self($reflection->getShortName()); @@ -185,6 +190,14 @@ public function addImportUse($name) $this->importUses[] = $name; } + /** + * @return string + */ + public function dump() + { + return $this->dumpContent(); + } + /** * @return string */ @@ -226,6 +239,7 @@ private function dumpHeader() if ($this->namespace) { $lines[] = 'namespace ' . $this->namespace . ';'; $lines[] = ''; + $lines[] = ''; } if (count($this->importUses)) { foreach ($this->importUses as $import) { @@ -233,6 +247,17 @@ private function dumpHeader() } $lines[] = ''; } + + $content = $this->dumpLines($lines); + + if ($this->docBlock) { + $docBlockText = $this->docBlock->dump(); + if ($docBlockText) { + $docBlockText .= PHP_EOL; + } + $content .= $docBlockText; + } + $classDeclaration = ''; if ($this->abstract) { $classDeclaration .= self::KEYWORD_ABSTRACT . ' '; @@ -244,10 +269,12 @@ private function dumpHeader() if ($this->interfaces) { $classDeclaration .= ' implements ' . implode(', ', $this->getInterfaces()); } + $lines = []; $lines[] = $classDeclaration; $lines[] = '{'; + $content .= $this->dumpLines($lines); - return $this->dumpLines($lines); + return $content; } /** From 3ef73ba9e5d521aaf22fc0b476c105a5d33baf9f Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 22 Oct 2015 15:52:15 +0200 Subject: [PATCH 24/30] - formatting --- source/CodeGenerator/ClassBlock.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/source/CodeGenerator/ClassBlock.php b/source/CodeGenerator/ClassBlock.php index bb839f6..06ed016 100644 --- a/source/CodeGenerator/ClassBlock.php +++ b/source/CodeGenerator/ClassBlock.php @@ -110,7 +110,10 @@ public static function buildFromReflection(\ReflectionClass $reflection) } } foreach ($reflection->getConstants() as $name => $value) { - if (!$reflection->getParentClass() || ($reflection->getParentClass() && !$reflection->getParentClass()->hasConstant($name))) { + if (!$reflection->getParentClass() || ($reflection->getParentClass() && !$reflection->getParentClass()->hasConstant( + $name + )) + ) { $class->addConstant(new ConstantBlock($name, $value)); } } @@ -239,7 +242,6 @@ private function dumpHeader() if ($this->namespace) { $lines[] = 'namespace ' . $this->namespace . ';'; $lines[] = ''; - $lines[] = ''; } if (count($this->importUses)) { foreach ($this->importUses as $import) { @@ -255,8 +257,10 @@ private function dumpHeader() if ($docBlockText) { $docBlockText .= PHP_EOL; } - $content .= $docBlockText; + } else { + $docBlockText = "\n"; } + $content .= $docBlockText; $classDeclaration = ''; if ($this->abstract) { From b8d0bd4671f37c7ebc36ae34ef07729d43958852 Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 22 Oct 2015 15:58:09 +0200 Subject: [PATCH 25/30] - fromatting --- source/CodeGenerator/ClassBlock.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/source/CodeGenerator/ClassBlock.php b/source/CodeGenerator/ClassBlock.php index 06ed016..f56906d 100644 --- a/source/CodeGenerator/ClassBlock.php +++ b/source/CodeGenerator/ClassBlock.php @@ -250,17 +250,13 @@ private function dumpHeader() $lines[] = ''; } - $content = $this->dumpLines($lines); - if ($this->docBlock) { $docBlockText = $this->docBlock->dump(); if ($docBlockText) { $docBlockText .= PHP_EOL; } - } else { - $docBlockText = "\n"; + $lines[] = $docBlockText; } - $content .= $docBlockText; $classDeclaration = ''; if ($this->abstract) { @@ -273,12 +269,10 @@ private function dumpHeader() if ($this->interfaces) { $classDeclaration .= ' implements ' . implode(', ', $this->getInterfaces()); } - $lines = []; $lines[] = $classDeclaration; $lines[] = '{'; - $content .= $this->dumpLines($lines); - return $content; + return $this->dumpLines($lines); } /** From ae44c1f0c248f09e0c307727e9524614a7980a35 Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 22 Oct 2015 16:14:15 +0200 Subject: [PATCH 26/30] fixed class doc block --- source/CodeGenerator/ClassBlock.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/source/CodeGenerator/ClassBlock.php b/source/CodeGenerator/ClassBlock.php index f56906d..bcc8035 100644 --- a/source/CodeGenerator/ClassBlock.php +++ b/source/CodeGenerator/ClassBlock.php @@ -251,11 +251,7 @@ private function dumpHeader() } if ($this->docBlock) { - $docBlockText = $this->docBlock->dump(); - if ($docBlockText) { - $docBlockText .= PHP_EOL; - } - $lines[] = $docBlockText; + $lines[] = $this->docBlock->dump(); } $classDeclaration = ''; From f91b38b1ad8934576ebe836e715b1a21a11ed08c Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 22 Oct 2015 16:28:38 +0200 Subject: [PATCH 27/30] -added getter for parameter default value --- source/CodeGenerator/ParameterBlock.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/source/CodeGenerator/ParameterBlock.php b/source/CodeGenerator/ParameterBlock.php index 6297f01..0d22cf2 100644 --- a/source/CodeGenerator/ParameterBlock.php +++ b/source/CodeGenerator/ParameterBlock.php @@ -85,6 +85,14 @@ public function getName() return $this->name; } + /** + * @return mixed + */ + public function getDefaultValue() + { + return $this->defaultValue; + } + /** * @return string */ From c3da7f86d618548624dbc44ed0d5718b494a096f Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 22 Oct 2015 16:34:13 +0200 Subject: [PATCH 28/30] - added getter for optional --- source/CodeGenerator/ParameterBlock.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/source/CodeGenerator/ParameterBlock.php b/source/CodeGenerator/ParameterBlock.php index 0d22cf2..93cf099 100644 --- a/source/CodeGenerator/ParameterBlock.php +++ b/source/CodeGenerator/ParameterBlock.php @@ -93,6 +93,14 @@ public function getDefaultValue() return $this->defaultValue; } + /** + * @return boolean + */ + public function isOptional() + { + return $this->optional; + } + /** * @return string */ From f2490c549a655957f968f8baa4a198fa9ce14192 Mon Sep 17 00:00:00 2001 From: Christian Burgas Date: Thu, 17 Dec 2015 14:45:10 +0100 Subject: [PATCH 29/30] - added wrapper around array keys --- source/CodeGenerator/ArrayBlock.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/CodeGenerator/ArrayBlock.php b/source/CodeGenerator/ArrayBlock.php index c93adc2..3bce1c2 100644 --- a/source/CodeGenerator/ArrayBlock.php +++ b/source/CodeGenerator/ArrayBlock.php @@ -25,7 +25,7 @@ protected function dumpContent() foreach ($this->value as $key => $value) { $line = ''; if ($isAssociative) { - $line .= $key . ' => '; + $line .= '\'' . $key . '\' => '; } $value = new ValueBlock($value); $line .= $value->dump(); From 6501bd8911f7c2c64d1945cdf1223614a37741e6 Mon Sep 17 00:00:00 2001 From: Jan Lukowicz Date: Tue, 14 Nov 2023 11:12:18 +0100 Subject: [PATCH 30/30] BOSS-30433 Update variable in string to php8 --- source/CodeGenerator/ClassBlock.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/CodeGenerator/ClassBlock.php b/source/CodeGenerator/ClassBlock.php index bcc8035..eb0fadb 100644 --- a/source/CodeGenerator/ClassBlock.php +++ b/source/CodeGenerator/ClassBlock.php @@ -209,7 +209,7 @@ protected function dumpContent() $lines = []; $lines[] = $this->dumpHeader(); foreach ($this->uses as $use) { - $lines[] = $this->indent("use ${use};"); + $lines[] = $this->indent("use {$use};"); $lines[] = ''; } foreach ($this->constants as $constant) {