From 8b23f834d987d2ed6c302fdee78b1e7c25410cdb Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 20 May 2015 15:36:52 +0200 Subject: [PATCH 01/57] opened v2.4-dev --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a61bd64a..19361cde 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ "minimum-stability": "dev", "extra": { "branch-alias": { - "dev-master": "2.3-dev" + "dev-master": "2.4-dev" } } } From 1384ea75eb8e8f03fd68311dfd5ba2b6e2d9fbee Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 20 May 2015 16:58:32 +0200 Subject: [PATCH 02/57] requires PHP 5.4.4 --- .travis.yml | 1 - composer.json | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index e686d783..1945e577 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: php php: - - 5.3.3 - 5.4 - 5.5 - 5.6 diff --git a/composer.json b/composer.json index 19361cde..82358a1d 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ } ], "require": { - "php": ">=5.3.1", + "php": ">=5.4.4", "nette/utils": "~2.2" }, "require-dev": { From e63323324ec326d09bbaae2967cce8c3d2e70d42 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 20 May 2015 16:58:33 +0200 Subject: [PATCH 03/57] used PHP 5.4 array syntax --- .travis.yml | 4 ++-- src/PhpGenerator/ClassType.php | 22 +++++++++++----------- src/PhpGenerator/Helpers.php | 4 ++-- src/PhpGenerator/Method.php | 14 +++++++------- src/PhpGenerator/PhpFile.php | 2 +- src/PhpGenerator/PhpNamespace.php | 6 +++--- src/PhpGenerator/Property.php | 4 ++-- tests/PhpGenerator/ClassType.from.phpt | 4 ++-- tests/PhpGenerator/ClassType.phpt | 6 +++--- tests/PhpGenerator/Helpers.dump().phpt | 18 +++++++++--------- tests/PhpGenerator/Helpers.format.phpt | 22 +++++++++++----------- 11 files changed, 53 insertions(+), 53 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1945e577..2cc77a11 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ matrix: script: - vendor/bin/tester tests -s -p php - - php code-checker/src/code-checker.php + - php temp/code-checker/src/code-checker.php --short-arrays after_failure: # Print *.actual content @@ -22,4 +22,4 @@ after_failure: before_script: # Install Nette Tester & Code Checker - composer install --no-interaction --prefer-source - - composer create-project nette/code-checker code-checker ~2.3 --no-interaction --prefer-source + - composer create-project nette/code-checker temp/code-checker ~2.5 --no-interaction --prefer-source diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index 43939a9a..8907384e 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -40,25 +40,25 @@ class ClassType extends Nette\Object private $abstract = FALSE; /** @var strings|string[] */ - private $extends = array(); + private $extends = []; /** @var string[] */ - private $implements = array(); + private $implements = []; /** @var string[] */ - private $traits = array(); + private $traits = []; /** @var string[] */ - private $documents = array(); + private $documents = []; /** @var array name => value */ - private $consts = array(); + private $consts = []; /** @var Property[] name => Property */ - private $properties = array(); + private $properties = []; /** @var Method[] name => Method */ - private $methods = array(); + private $methods = []; /** @@ -113,12 +113,12 @@ public function __construct($name = NULL, PhpNamespace $namespace = NULL) */ public function __toString() { - $consts = array(); + $consts = []; foreach ($this->consts as $name => $value) { $consts[] = "const $name = " . Helpers::dump($value) . ";\n"; } - $properties = array(); + $properties = []; foreach ($this->properties as $property) { $doc = str_replace("\n", "\n * ", implode("\n", (array) $property->getDocuments())); $properties[] = ($property->getDocuments() ? (strpos($doc, "\n") === FALSE ? "/** $doc */\n" : "/**\n * $doc\n */\n") : '') @@ -127,7 +127,7 @@ public function __toString() . ";\n"; } - $extends = $implements = $traits = array(); + $extends = $implements = $traits = []; if ($this->namespace) { foreach ((array) $this->extends as $name) { $extends[] = $this->namespace->unresolveName($name); @@ -205,7 +205,7 @@ public function getName() */ public function setType($type) { - if (!in_array($type, array('class', 'interface', 'trait'), TRUE)) { + if (!in_array($type, ['class', 'interface', 'trait'], TRUE)) { throw new Nette\InvalidArgumentException('Argument must be class|interface|trait.'); } $this->type = $type; diff --git a/src/PhpGenerator/Helpers.php b/src/PhpGenerator/Helpers.php index eb4e5798..4ed348d3 100644 --- a/src/PhpGenerator/Helpers.php +++ b/src/PhpGenerator/Helpers.php @@ -100,7 +100,7 @@ private static function _dump(& $var, $level = 0) $space = str_repeat("\t", $level); $class = get_class($var); - static $list = array(); + static $list = []; if ($level > self::MAX_DEPTH || in_array($var, $list, TRUE)) { throw new Nette\InvalidArgumentException('Nesting level too deep or recursive dependency.'); @@ -170,7 +170,7 @@ public static function formatArgs($statement, array $args) $a = strlen($s); } else { - $arg = substr($statement, $a - 1, 1) === '$' || in_array(substr($statement, $a - 2, 2), array('->', '::'), TRUE) + $arg = substr($statement, $a - 1, 1) === '$' || in_array(substr($statement, $a - 2, 2), ['->', '::'], TRUE) ? self::formatMember($arg) : self::_dump($arg); $statement = substr_replace($statement, $arg, $a, 1); $a += strlen($arg); diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index 3823391a..0847b554 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -21,10 +21,10 @@ class Method extends Nette\Object private $name; /** @var array of name => Parameter */ - private $parameters = array(); + private $parameters = []; /** @var array of name => bool */ - private $uses = array(); + private $uses = []; /** @var string|FALSE */ private $body; @@ -48,7 +48,7 @@ class Method extends Nette\Object private $variadic = FALSE; /** @var array of string */ - private $documents = array(); + private $documents = []; /** @var PhpNamespace */ private $namespace; @@ -82,10 +82,10 @@ public static function from($from) */ public function __toString() { - $parameters = array(); + $parameters = []; foreach ($this->parameters as $param) { $variadic = $this->variadic && $param === end($this->parameters); - $hint = in_array($param->getTypeHint(), array('array', '')) + $hint = in_array($param->getTypeHint(), ['array', '']) ? $param->getTypeHint() : ($this->namespace ? $this->namespace->unresolveName($param->getTypeHint()) : $param->getTypeHint()); @@ -95,7 +95,7 @@ public function __toString() . '$' . $param->getName() . ($param->isOptional() && !$variadic ? ' = ' . Helpers::dump($param->defaultValue) : ''); } - $uses = array(); + $uses = []; foreach ($this->uses as $param) { $uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName(); } @@ -257,7 +257,7 @@ public function isStatic() */ public function setVisibility($val) { - if (!in_array($val, array('public', 'protected', 'private', NULL), TRUE)) { + if (!in_array($val, ['public', 'protected', 'private', NULL], TRUE)) { throw new Nette\InvalidArgumentException('Argument must be public|protected|private|NULL.'); } $this->visibility = (string) $val; diff --git a/src/PhpGenerator/PhpFile.php b/src/PhpGenerator/PhpFile.php index a210eb67..8922b089 100644 --- a/src/PhpGenerator/PhpFile.php +++ b/src/PhpGenerator/PhpFile.php @@ -27,7 +27,7 @@ class PhpFile extends Object private $documents; /** @var PhpNamespace[] */ - private $namespaces = array(); + private $namespaces = []; /** diff --git a/src/PhpGenerator/PhpNamespace.php b/src/PhpGenerator/PhpNamespace.php index b6dfd4e1..9f86dc43 100644 --- a/src/PhpGenerator/PhpNamespace.php +++ b/src/PhpGenerator/PhpNamespace.php @@ -31,10 +31,10 @@ class PhpNamespace extends Object private $bracketedSyntax = FALSE; /** @var string[] */ - private $uses = array(); + private $uses = []; /** @var ClassType[] */ - private $classes = array(); + private $classes = []; public function __construct($name = NULL) @@ -204,7 +204,7 @@ public function addTrait($name) */ public function __toString() { - $uses = array(); + $uses = []; asort($this->uses); foreach ($this->uses as $alias => $name) { $useNamespace = Helpers::extractNamespace($name); diff --git a/src/PhpGenerator/Property.php b/src/PhpGenerator/Property.php index 558eb6b8..efc03ad8 100644 --- a/src/PhpGenerator/Property.php +++ b/src/PhpGenerator/Property.php @@ -30,7 +30,7 @@ class Property extends Nette\Object private $visibility = 'public'; /** @var array of string */ - private $documents = array(); + private $documents = []; /** @@ -114,7 +114,7 @@ public function isStatic() */ public function setVisibility($val) { - if (!in_array($val, array('public', 'protected', 'private'), TRUE)) { + if (!in_array($val, ['public', 'protected', 'private'], TRUE)) { throw new Nette\InvalidArgumentException('Argument must be public|protected|private.'); } $this->visibility = (string) $val; diff --git a/tests/PhpGenerator/ClassType.from.phpt b/tests/PhpGenerator/ClassType.from.phpt index bd440d3f..9b606a8f 100644 --- a/tests/PhpGenerator/ClassType.from.phpt +++ b/tests/PhpGenerator/ClassType.from.phpt @@ -47,7 +47,7 @@ class Class2 extends Class1 implements Interface2 /** @var int */ protected $protected = 10; - private $private = array(); + private $private = []; static public $static; @@ -55,7 +55,7 @@ class Class2 extends Class1 implements Interface2 * Func3 * @return Class1 */ - private function & func3(array $a = array(), Class2 $b = NULL, \Abc\Unknown $c, \Xyz\Unknown $d, $e) + private function & func3(array $a = [], Class2 $b = NULL, \Abc\Unknown $c, \Xyz\Unknown $d, $e) {} final function func2() diff --git a/tests/PhpGenerator/ClassType.phpt b/tests/PhpGenerator/ClassType.phpt index 49569e8e..305bd810 100644 --- a/tests/PhpGenerator/ClassType.phpt +++ b/tests/PhpGenerator/ClassType.phpt @@ -34,7 +34,7 @@ $class->addProperty('handle') $class->addProperty('order') ->setValue(new PhpLiteral('RecursiveIteratorIterator::SELF_FIRST')); -$p = $class->addProperty('sections', array('first' => TRUE)) +$p = $class->addProperty('sections', ['first' => TRUE]) ->setStatic(TRUE); Assert::same($p, $class->getProperty('sections')); @@ -43,7 +43,7 @@ $m = $class->addMethod('getHandle') ->addDocument('Returns file handle.') ->addDocument('@return resource') ->setFinal(TRUE) - ->setBody('return $this->?;', array('handle')); + ->setBody('return $this->?;', ['handle']); Assert::same($m, $class->getMethod('getHandle')); @@ -51,7 +51,7 @@ $class->addMethod('getSections') ->setStatic(TRUE) ->setVisibility('protected') ->setReturnReference(TRUE) - ->addBody('$mode = ?;', array(123)) + ->addBody('$mode = ?;', [123]) ->addBody('return self::$sections;') ->addParameter('mode', new PhpLiteral('self::ORDER')); diff --git a/tests/PhpGenerator/Helpers.dump().phpt b/tests/PhpGenerator/Helpers.dump().phpt index b5af5e78..e540aa43 100644 --- a/tests/PhpGenerator/Helpers.dump().phpt +++ b/tests/PhpGenerator/Helpers.dump().phpt @@ -24,17 +24,17 @@ Assert::same( "'I\xc3\xb1t\xc3\xabrn\xc3\xa2ti\xc3\xb4n\xc3\xa0liz\xc3\xa6ti\xc3 Assert::same( '"\rHello \$"', Helpers::dump("\rHello $") ); Assert::same( "'He\\llo'", Helpers::dump('He\llo') ); Assert::same( '\'He\ll\\\\\o \\\'wor\\\\\\\'ld\\\\\'', Helpers::dump('He\ll\\\o \'wor\\\'ld\\') ); -Assert::same( 'array()', Helpers::dump(array()) ); +Assert::same( 'array()', Helpers::dump([]) ); -Assert::same( "array(\$s)", Helpers::dump(array(new PhpLiteral('$s'))) ); +Assert::same( "array(\$s)", Helpers::dump([new PhpLiteral('$s')]) ); -Assert::same( "array(1, 2, 3)", Helpers::dump(array(1,2,3)) ); -Assert::same( "array('a', 7 => 'b', 'c', '9a' => 'd', 'e')", Helpers::dump(array('a', 7 => 'b', 'c', '9a' => 'd', 9 => 'e')) ); -Assert::same( "array(\n\tarray(\n\t\t'a',\n\t\t'loooooooooooooooooooooooooooooooooong',\n\t),\n)", Helpers::dump(array(array('a', 'loooooooooooooooooooooooooooooooooong'))) ); -Assert::same( "array('a' => 1, array(\"\\r\" => \"\\r\", 2), 3)", Helpers::dump(array('a' => 1, array("\r" => "\r", 2), 3)) ); +Assert::same( "array(1, 2, 3)", Helpers::dump([1,2,3]) ); +Assert::same( "array('a', 7 => 'b', 'c', '9a' => 'd', 'e')", Helpers::dump(['a', 7 => 'b', 'c', '9a' => 'd', 9 => 'e']) ); +Assert::same( "array(\n\tarray(\n\t\t'a',\n\t\t'loooooooooooooooooooooooooooooooooong',\n\t),\n)", Helpers::dump([['a', 'loooooooooooooooooooooooooooooooooong']]) ); +Assert::same( "array('a' => 1, array(\"\\r\" => \"\\r\", 2), 3)", Helpers::dump(['a' => 1, ["\r" => "\r", 2], 3]) ); -Assert::same( "(object) array(\n\t'a' => 1,\n\t'b' => 2,\n)", Helpers::dump((object) array('a' => 1, 'b' => 2)) ); -Assert::same( "(object) array(\n\t'a' => (object) array(\n\t\t'b' => 2,\n\t),\n)" , Helpers::dump((object) array('a' => (object) array('b' => 2))) ); +Assert::same( "(object) array(\n\t'a' => 1,\n\t'b' => 2,\n)", Helpers::dump((object) ['a' => 1, 'b' => 2]) ); +Assert::same( "(object) array(\n\t'a' => (object) array(\n\t\t'b' => 2,\n\t),\n)" , Helpers::dump((object) ['a' => (object) ['b' => 2]]) ); class Test @@ -55,7 +55,7 @@ class Test2 extends Test function __sleep() { - return array('c', 'b', 'a'); + return ['c', 'b', 'a']; } function __wakeup() diff --git a/tests/PhpGenerator/Helpers.format.phpt b/tests/PhpGenerator/Helpers.format.phpt index a08a8a2f..bf2927da 100644 --- a/tests/PhpGenerator/Helpers.format.phpt +++ b/tests/PhpGenerator/Helpers.format.phpt @@ -14,27 +14,27 @@ require __DIR__ . '/../bootstrap.php'; Assert::same( 'func', Helpers::format('func') ); Assert::same( 'func(1)', Helpers::format('func(?)', 1, 2) ); -Assert::same( 'func', Helpers::formatArgs('func', array(1, 2)) ); -Assert::same( 'func(1)', Helpers::formatArgs('func(?)', array(1, 2)) ); -Assert::same( "func(array(1, 2))", Helpers::formatArgs('func(?)', array(array(1, 2))) ); -Assert::same( 'func(1, 2)', Helpers::formatArgs('func(?*)', array(array(1, 2))) ); +Assert::same( 'func', Helpers::formatArgs('func', [1, 2]) ); +Assert::same( 'func(1)', Helpers::formatArgs('func(?)', [1, 2]) ); +Assert::same( "func(array(1, 2))", Helpers::formatArgs('func(?)', [[1, 2]]) ); +Assert::same( 'func(1, 2)', Helpers::formatArgs('func(?*)', [[1, 2]]) ); Assert::same( "func(10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,\n\t35, 36, 37, 38, 39, 40)", - Helpers::formatArgs('func(?*)', array(range(10, 40))) + Helpers::formatArgs('func(?*)', [range(10, 40)]) ); Assert::exception(function() { - Helpers::formatArgs('func(?*)', array(1, 2)); + Helpers::formatArgs('func(?*)', [1, 2]); }, 'Nette\InvalidArgumentException', 'Argument must be an array.'); Assert::exception(function() { - Helpers::formatArgs('func(?, ?, ?)', array(1, 2)); + Helpers::formatArgs('func(?, ?, ?)', [1, 2]); }, 'Nette\InvalidArgumentException', 'Insufficient number of arguments.'); -Assert::same( '$a = 2', Helpers::formatArgs('$? = ?', array('a', 2)) ); -Assert::same( '$obj->a = 2', Helpers::formatArgs('$obj->? = ?', array('a', 2)) ); -Assert::same( '$obj->{1} = 2', Helpers::formatArgs('$obj->? = ?', array(1, 2)) ); -Assert::same( '$obj->{\' \'} = 2', Helpers::formatArgs('$obj->? = ?', array(' ', 2)) ); +Assert::same( '$a = 2', Helpers::formatArgs('$? = ?', ['a', 2]) ); +Assert::same( '$obj->a = 2', Helpers::formatArgs('$obj->? = ?', ['a', 2]) ); +Assert::same( '$obj->{1} = 2', Helpers::formatArgs('$obj->? = ?', [1, 2]) ); +Assert::same( '$obj->{\' \'} = 2', Helpers::formatArgs('$obj->? = ?', [' ', 2]) ); Assert::same( "Item", Helpers::formatMember('Item') ); Assert::same( "{'0Item'}", Helpers::formatMember('0Item') ); From fe6f2ff0019fdcae0aab907258b06647c2078514 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 20 May 2015 18:14:09 +0200 Subject: [PATCH 04/57] removed support for PHP 5.3 --- src/PhpGenerator/ClassType.php | 2 +- src/PhpGenerator/Parameter.php | 2 +- tests/PhpGenerator/ClassType.from.trait.phpt | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index 8907384e..123bb3cb 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -69,7 +69,7 @@ public static function from($from) { $from = $from instanceof \ReflectionClass ? $from : new \ReflectionClass($from); $class = new static($from->getShortName()); - $class->type = $from->isInterface() ? 'interface' : (PHP_VERSION_ID >= 50400 && $from->isTrait() ? 'trait' : 'class'); + $class->type = $from->isInterface() ? 'interface' : ($from->isTrait() ? 'trait' : 'class'); $class->final = $from->isFinal() && $class->type === 'class'; $class->abstract = $from->isAbstract() && $class->type === 'class'; $class->implements = $from->getInterfaceNames(); diff --git a/src/PhpGenerator/Parameter.php b/src/PhpGenerator/Parameter.php index a70d72e7..d61b30b0 100644 --- a/src/PhpGenerator/Parameter.php +++ b/src/PhpGenerator/Parameter.php @@ -51,7 +51,7 @@ public static function from(\ReflectionParameter $from) } } $param->optional = PHP_VERSION_ID < 50407 ? $from->isOptional() || ($param->typeHint && $from->allowsNull()) : $from->isDefaultValueAvailable(); - $param->defaultValue = (PHP_VERSION_ID === 50316 ? $from->isOptional() : $from->isDefaultValueAvailable()) ? $from->getDefaultValue() : NULL; + $param->defaultValue = $from->isDefaultValueAvailable() ? $from->getDefaultValue() : NULL; $namespace = $from->getDeclaringClass()->getNamespaceName(); $namespace = $namespace ? "\\$namespace\\" : "\\"; diff --git a/tests/PhpGenerator/ClassType.from.trait.phpt b/tests/PhpGenerator/ClassType.from.trait.phpt index 1c2f1f75..2908eb68 100644 --- a/tests/PhpGenerator/ClassType.from.trait.phpt +++ b/tests/PhpGenerator/ClassType.from.trait.phpt @@ -2,7 +2,6 @@ /** * Test: Nette\PhpGenerator generator. - * @phpversion 5.4 */ use Nette\PhpGenerator\ClassType, From 4db6c3ce9902030bad23a23178042f858a220514 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 20 May 2015 20:36:36 +0200 Subject: [PATCH 05/57] Helpers::dump() generates PHP 5.4 short array syntax --- src/PhpGenerator/Helpers.php | 6 +++--- tests/PhpGenerator/ClassType.expect | 2 +- tests/PhpGenerator/ClassType.from.expect | 2 +- tests/PhpGenerator/Helpers.dump().phpt | 20 ++++++++++---------- tests/PhpGenerator/Helpers.format.phpt | 2 +- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/PhpGenerator/Helpers.php b/src/PhpGenerator/Helpers.php index 4ed348d3..257557d2 100644 --- a/src/PhpGenerator/Helpers.php +++ b/src/PhpGenerator/Helpers.php @@ -89,7 +89,7 @@ private static function _dump(& $var, $level = 0) } unset($var[$marker]); } - return 'array(' . (strpos($out, "\n") === FALSE && strlen($out) < 40 ? $out : $outAlt) . ')'; + return '[' . (strpos($out, "\n") === FALSE && strlen($out) < 40 ? $out : $outAlt) . ']'; } elseif ($var instanceof \Serializable) { $var = serialize($var); @@ -121,8 +121,8 @@ private static function _dump(& $var, $level = 0) $out .= $space; } return $class === 'stdClass' - ? "(object) array($out)" - : __CLASS__ . "::createObject('$class', array($out))"; + ? "(object) [$out]" + : __CLASS__ . "::createObject('$class', [$out])"; } elseif (is_resource($var)) { throw new Nette\InvalidArgumentException('Cannot dump resource.'); diff --git a/tests/PhpGenerator/ClassType.expect b/tests/PhpGenerator/ClassType.expect index 317f9a4f..e8476f6e 100644 --- a/tests/PhpGenerator/ClassType.expect +++ b/tests/PhpGenerator/ClassType.expect @@ -18,7 +18,7 @@ abstract final class Example extends ParentClass implements IExample, IOne public $order = RecursiveIteratorIterator::SELF_FIRST; - public static $sections = array('first' => TRUE); + public static $sections = ['first' => TRUE]; /** diff --git a/tests/PhpGenerator/ClassType.from.expect b/tests/PhpGenerator/ClassType.from.expect index 697c52cb..c6eef384 100644 --- a/tests/PhpGenerator/ClassType.from.expect +++ b/tests/PhpGenerator/ClassType.from.expect @@ -44,7 +44,7 @@ class Class2 extends Class1 implements Interface2 /** @var int */ protected $protected = 10; - private $private = array(); + private $private = []; public static $static; diff --git a/tests/PhpGenerator/Helpers.dump().phpt b/tests/PhpGenerator/Helpers.dump().phpt index e540aa43..9e2d67c3 100644 --- a/tests/PhpGenerator/Helpers.dump().phpt +++ b/tests/PhpGenerator/Helpers.dump().phpt @@ -24,17 +24,17 @@ Assert::same( "'I\xc3\xb1t\xc3\xabrn\xc3\xa2ti\xc3\xb4n\xc3\xa0liz\xc3\xa6ti\xc3 Assert::same( '"\rHello \$"', Helpers::dump("\rHello $") ); Assert::same( "'He\\llo'", Helpers::dump('He\llo') ); Assert::same( '\'He\ll\\\\\o \\\'wor\\\\\\\'ld\\\\\'', Helpers::dump('He\ll\\\o \'wor\\\'ld\\') ); -Assert::same( 'array()', Helpers::dump([]) ); +Assert::same( '[]', Helpers::dump([]) ); -Assert::same( "array(\$s)", Helpers::dump([new PhpLiteral('$s')]) ); +Assert::same( "[\$s]", Helpers::dump([new PhpLiteral('$s')]) ); -Assert::same( "array(1, 2, 3)", Helpers::dump([1,2,3]) ); -Assert::same( "array('a', 7 => 'b', 'c', '9a' => 'd', 'e')", Helpers::dump(['a', 7 => 'b', 'c', '9a' => 'd', 9 => 'e']) ); -Assert::same( "array(\n\tarray(\n\t\t'a',\n\t\t'loooooooooooooooooooooooooooooooooong',\n\t),\n)", Helpers::dump([['a', 'loooooooooooooooooooooooooooooooooong']]) ); -Assert::same( "array('a' => 1, array(\"\\r\" => \"\\r\", 2), 3)", Helpers::dump(['a' => 1, ["\r" => "\r", 2], 3]) ); +Assert::same( "[1, 2, 3]", Helpers::dump([1,2,3]) ); +Assert::same( "['a', 7 => 'b', 'c', '9a' => 'd', 'e']", Helpers::dump(['a', 7 => 'b', 'c', '9a' => 'd', 9 => 'e']) ); +Assert::same( "[\n\t[\n\t\t'a',\n\t\t'loooooooooooooooooooooooooooooooooong',\n\t],\n]", Helpers::dump([['a', 'loooooooooooooooooooooooooooooooooong']]) ); +Assert::same( "['a' => 1, [\"\\r\" => \"\\r\", 2], 3]", Helpers::dump(['a' => 1, ["\r" => "\r", 2], 3]) ); -Assert::same( "(object) array(\n\t'a' => 1,\n\t'b' => 2,\n)", Helpers::dump((object) ['a' => 1, 'b' => 2]) ); -Assert::same( "(object) array(\n\t'a' => (object) array(\n\t\t'b' => 2,\n\t),\n)" , Helpers::dump((object) ['a' => (object) ['b' => 2]]) ); +Assert::same( "(object) [\n\t'a' => 1,\n\t'b' => 2,\n]", Helpers::dump((object) ['a' => 1, 'b' => 2]) ); +Assert::same( "(object) [\n\t'a' => (object) [\n\t\t'b' => 2,\n\t],\n]" , Helpers::dump((object) ['a' => (object) ['b' => 2]]) ); class Test @@ -44,7 +44,7 @@ class Test private $c = 3; } -Assert::same( "Nette\\PhpGenerator\\Helpers::createObject('Test', array(\n\t'a' => 1,\n\t\"\\x00*\\x00b\" => 2,\n\t\"\\x00Test\\x00c\" => 3,\n))", Helpers::dump(new Test) ); +Assert::same( "Nette\\PhpGenerator\\Helpers::createObject('Test', [\n\t'a' => 1,\n\t\"\\x00*\\x00b\" => 2,\n\t\"\\x00Test\\x00c\" => 3,\n])", Helpers::dump(new Test) ); Assert::equal( new Test, eval('return ' . Helpers::dump(new Test) . ';') ); @@ -63,7 +63,7 @@ class Test2 extends Test } } -Assert::same( "Nette\\PhpGenerator\\Helpers::createObject('Test2', array(\n\t\"\\x00Test2\\x00c\" => 4,\n\t'a' => 1,\n\t\"\\x00*\\x00b\" => 2,\n))", Helpers::dump(new Test2) ); +Assert::same( "Nette\\PhpGenerator\\Helpers::createObject('Test2', [\n\t\"\\x00Test2\\x00c\" => 4,\n\t'a' => 1,\n\t\"\\x00*\\x00b\" => 2,\n])", Helpers::dump(new Test2) ); Assert::equal( new Test2, eval('return ' . Helpers::dump(new Test2) . ';') ); diff --git a/tests/PhpGenerator/Helpers.format.phpt b/tests/PhpGenerator/Helpers.format.phpt index bf2927da..57928c15 100644 --- a/tests/PhpGenerator/Helpers.format.phpt +++ b/tests/PhpGenerator/Helpers.format.phpt @@ -16,7 +16,7 @@ Assert::same( 'func(1)', Helpers::format('func(?)', 1, 2) ); Assert::same( 'func', Helpers::formatArgs('func', [1, 2]) ); Assert::same( 'func(1)', Helpers::formatArgs('func(?)', [1, 2]) ); -Assert::same( "func(array(1, 2))", Helpers::formatArgs('func(?)', [[1, 2]]) ); +Assert::same( "func([1, 2])", Helpers::formatArgs('func(?)', [[1, 2]]) ); Assert::same( 'func(1, 2)', Helpers::formatArgs('func(?*)', [[1, 2]]) ); Assert::same( "func(10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,\n\t35, 36, 37, 38, 39, 40)", From d113bfceb5d98254ebc08d3c5ead23c8b909cd47 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 18 Jun 2015 16:45:36 +0200 Subject: [PATCH 06/57] improved coding style --- src/PhpGenerator/ClassType.php | 4 +- src/PhpGenerator/Helpers.php | 4 +- src/PhpGenerator/Parameter.php | 2 +- tests/PhpGenerator/ClassType.from.phpt | 6 +- tests/PhpGenerator/ClassType.from.trait.phpt | 4 +- tests/PhpGenerator/ClassType.inheritance.phpt | 15 +++-- tests/PhpGenerator/ClassType.interface.phpt | 4 +- tests/PhpGenerator/ClassType.phpt | 6 +- tests/PhpGenerator/Helpers.dump().phpt | 56 +++++++++---------- tests/PhpGenerator/Helpers.format.phpt | 36 ++++++------ tests/PhpGenerator/Method.closure.phpt | 4 +- tests/PhpGenerator/Method.variadics.phpt | 6 +- tests/PhpGenerator/PhpNamespace.phpt | 2 +- 13 files changed, 77 insertions(+), 72 deletions(-) diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index 123bb3cb..6639c64a 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -7,8 +7,8 @@ namespace Nette\PhpGenerator; -use Nette, - Nette\Utils\Strings; +use Nette; +use Nette\Utils\Strings; /** diff --git a/src/PhpGenerator/Helpers.php b/src/PhpGenerator/Helpers.php index 257557d2..d62b4c4c 100644 --- a/src/PhpGenerator/Helpers.php +++ b/src/PhpGenerator/Helpers.php @@ -114,7 +114,7 @@ private static function _dump(& $var, $level = 0) } foreach ($arr as $k => & $v) { if (!isset($props) || isset($props[$k])) { - $out .= "$space\t" . self::_dump($k, $level + 1) . " => " . self::_dump($v, $level + 1) . ",\n"; + $out .= "$space\t" . self::_dump($k, $level + 1) . ' => ' . self::_dump($v, $level + 1) . ",\n"; } } array_pop($list); @@ -189,7 +189,7 @@ public static function formatMember($name) { return $name instanceof PhpLiteral || !self::isIdentifier($name) ? '{' . self::_dump($name) . '}' - : $name ; + : $name; } diff --git a/src/PhpGenerator/Parameter.php b/src/PhpGenerator/Parameter.php index d61b30b0..d38c21e5 100644 --- a/src/PhpGenerator/Parameter.php +++ b/src/PhpGenerator/Parameter.php @@ -54,7 +54,7 @@ public static function from(\ReflectionParameter $from) $param->defaultValue = $from->isDefaultValueAvailable() ? $from->getDefaultValue() : NULL; $namespace = $from->getDeclaringClass()->getNamespaceName(); - $namespace = $namespace ? "\\$namespace\\" : "\\"; + $namespace = $namespace ? "\\$namespace\\" : '\\'; if (Nette\Utils\Strings::startsWith($param->typeHint, $namespace)) { $param->typeHint = substr($param->typeHint, strlen($namespace)); } diff --git a/tests/PhpGenerator/ClassType.from.phpt b/tests/PhpGenerator/ClassType.from.phpt index 9b606a8f..38cbf8a4 100644 --- a/tests/PhpGenerator/ClassType.from.phpt +++ b/tests/PhpGenerator/ClassType.from.phpt @@ -6,9 +6,9 @@ namespace Abc; -use Nette\PhpGenerator\ClassType, - ReflectionClass, - Tester\Assert; +use Nette\PhpGenerator\ClassType; +use ReflectionClass; +use Tester\Assert; require __DIR__ . '/../bootstrap.php'; diff --git a/tests/PhpGenerator/ClassType.from.trait.phpt b/tests/PhpGenerator/ClassType.from.trait.phpt index 2908eb68..cfb62ed7 100644 --- a/tests/PhpGenerator/ClassType.from.trait.phpt +++ b/tests/PhpGenerator/ClassType.from.trait.phpt @@ -4,8 +4,8 @@ * Test: Nette\PhpGenerator generator. */ -use Nette\PhpGenerator\ClassType, - Tester\Assert; +use Nette\PhpGenerator\ClassType; +use Tester\Assert; require __DIR__ . '/../bootstrap.php'; diff --git a/tests/PhpGenerator/ClassType.inheritance.phpt b/tests/PhpGenerator/ClassType.inheritance.phpt index 93441050..3aad1bfc 100644 --- a/tests/PhpGenerator/ClassType.inheritance.phpt +++ b/tests/PhpGenerator/ClassType.inheritance.phpt @@ -1,8 +1,8 @@ 'b', 'c', '9a' => 'd', 'e']", Helpers::dump(['a', 7 => 'b', 'c', '9a' => 'd', 9 => 'e']) ); -Assert::same( "[\n\t[\n\t\t'a',\n\t\t'loooooooooooooooooooooooooooooooooong',\n\t],\n]", Helpers::dump([['a', 'loooooooooooooooooooooooooooooooooong']]) ); -Assert::same( "['a' => 1, [\"\\r\" => \"\\r\", 2], 3]", Helpers::dump(['a' => 1, ["\r" => "\r", 2], 3]) ); +Assert::same('[1, 2, 3]', Helpers::dump([1, 2, 3])); +Assert::same("['a', 7 => 'b', 'c', '9a' => 'd', 'e']", Helpers::dump(['a', 7 => 'b', 'c', '9a' => 'd', 9 => 'e'])); +Assert::same("[\n\t[\n\t\t'a',\n\t\t'loooooooooooooooooooooooooooooooooong',\n\t],\n]", Helpers::dump([['a', 'loooooooooooooooooooooooooooooooooong']])); +Assert::same("['a' => 1, [\"\\r\" => \"\\r\", 2], 3]", Helpers::dump(['a' => 1, ["\r" => "\r", 2], 3])); -Assert::same( "(object) [\n\t'a' => 1,\n\t'b' => 2,\n]", Helpers::dump((object) ['a' => 1, 'b' => 2]) ); -Assert::same( "(object) [\n\t'a' => (object) [\n\t\t'b' => 2,\n\t],\n]" , Helpers::dump((object) ['a' => (object) ['b' => 2]]) ); +Assert::same("(object) [\n\t'a' => 1,\n\t'b' => 2,\n]", Helpers::dump((object) ['a' => 1, 'b' => 2])); +Assert::same("(object) [\n\t'a' => (object) [\n\t\t'b' => 2,\n\t],\n]", Helpers::dump((object) ['a' => (object) ['b' => 2]])); class Test @@ -44,8 +44,8 @@ class Test private $c = 3; } -Assert::same( "Nette\\PhpGenerator\\Helpers::createObject('Test', [\n\t'a' => 1,\n\t\"\\x00*\\x00b\" => 2,\n\t\"\\x00Test\\x00c\" => 3,\n])", Helpers::dump(new Test) ); -Assert::equal( new Test, eval('return ' . Helpers::dump(new Test) . ';') ); +Assert::same("Nette\\PhpGenerator\\Helpers::createObject('Test', [\n\t'a' => 1,\n\t\"\\x00*\\x00b\" => 2,\n\t\"\\x00Test\\x00c\" => 3,\n])", Helpers::dump(new Test)); +Assert::equal(new Test, eval('return ' . Helpers::dump(new Test) . ';')); class Test2 extends Test @@ -63,8 +63,8 @@ class Test2 extends Test } } -Assert::same( "Nette\\PhpGenerator\\Helpers::createObject('Test2', [\n\t\"\\x00Test2\\x00c\" => 4,\n\t'a' => 1,\n\t\"\\x00*\\x00b\" => 2,\n])", Helpers::dump(new Test2) ); -Assert::equal( new Test2, eval('return ' . Helpers::dump(new Test2) . ';') ); +Assert::same("Nette\\PhpGenerator\\Helpers::createObject('Test2', [\n\t\"\\x00Test2\\x00c\" => 4,\n\t'a' => 1,\n\t\"\\x00*\\x00b\" => 2,\n])", Helpers::dump(new Test2)); +Assert::equal(new Test2, eval('return ' . Helpers::dump(new Test2) . ';')); class Test3 implements Serializable @@ -81,5 +81,5 @@ class Test3 implements Serializable } } -Assert::same( 'unserialize(\'C:5:"Test3":0:{}\')', Helpers::dump(new Test3) ); -Assert::equal( new Test3, eval('return ' . Helpers::dump(new Test3) . ';') ); +Assert::same('unserialize(\'C:5:"Test3":0:{}\')', Helpers::dump(new Test3)); +Assert::equal(new Test3, eval('return ' . Helpers::dump(new Test3) . ';')); diff --git a/tests/PhpGenerator/Helpers.format.phpt b/tests/PhpGenerator/Helpers.format.phpt index 57928c15..dff61eef 100644 --- a/tests/PhpGenerator/Helpers.format.phpt +++ b/tests/PhpGenerator/Helpers.format.phpt @@ -4,40 +4,40 @@ * Test: Nette\PhpGenerator\Helpers::format() & formatArgs() */ -use Nette\PhpGenerator\Helpers, - Tester\Assert; +use Nette\PhpGenerator\Helpers; +use Tester\Assert; require __DIR__ . '/../bootstrap.php'; -Assert::same( 'func', Helpers::format('func') ); -Assert::same( 'func(1)', Helpers::format('func(?)', 1, 2) ); +Assert::same('func', Helpers::format('func')); +Assert::same('func(1)', Helpers::format('func(?)', 1, 2)); -Assert::same( 'func', Helpers::formatArgs('func', [1, 2]) ); -Assert::same( 'func(1)', Helpers::formatArgs('func(?)', [1, 2]) ); -Assert::same( "func([1, 2])", Helpers::formatArgs('func(?)', [[1, 2]]) ); -Assert::same( 'func(1, 2)', Helpers::formatArgs('func(?*)', [[1, 2]]) ); +Assert::same('func', Helpers::formatArgs('func', [1, 2])); +Assert::same('func(1)', Helpers::formatArgs('func(?)', [1, 2])); +Assert::same('func([1, 2])', Helpers::formatArgs('func(?)', [[1, 2]])); +Assert::same('func(1, 2)', Helpers::formatArgs('func(?*)', [[1, 2]])); Assert::same( "func(10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,\n\t35, 36, 37, 38, 39, 40)", Helpers::formatArgs('func(?*)', [range(10, 40)]) ); -Assert::exception(function() { +Assert::exception(function () { Helpers::formatArgs('func(?*)', [1, 2]); }, 'Nette\InvalidArgumentException', 'Argument must be an array.'); -Assert::exception(function() { +Assert::exception(function () { Helpers::formatArgs('func(?, ?, ?)', [1, 2]); }, 'Nette\InvalidArgumentException', 'Insufficient number of arguments.'); -Assert::same( '$a = 2', Helpers::formatArgs('$? = ?', ['a', 2]) ); -Assert::same( '$obj->a = 2', Helpers::formatArgs('$obj->? = ?', ['a', 2]) ); -Assert::same( '$obj->{1} = 2', Helpers::formatArgs('$obj->? = ?', [1, 2]) ); -Assert::same( '$obj->{\' \'} = 2', Helpers::formatArgs('$obj->? = ?', [' ', 2]) ); +Assert::same('$a = 2', Helpers::formatArgs('$? = ?', ['a', 2])); +Assert::same('$obj->a = 2', Helpers::formatArgs('$obj->? = ?', ['a', 2])); +Assert::same('$obj->{1} = 2', Helpers::formatArgs('$obj->? = ?', [1, 2])); +Assert::same('$obj->{\' \'} = 2', Helpers::formatArgs('$obj->? = ?', [' ', 2])); -Assert::same( "Item", Helpers::formatMember('Item') ); -Assert::same( "{'0Item'}", Helpers::formatMember('0Item') ); +Assert::same('Item', Helpers::formatMember('Item')); +Assert::same("{'0Item'}", Helpers::formatMember('0Item')); -Assert::true( Helpers::isIdentifier('Item') ); -Assert::false( Helpers::isIdentifier('0Item') ); +Assert::true(Helpers::isIdentifier('Item')); +Assert::false(Helpers::isIdentifier('0Item')); diff --git a/tests/PhpGenerator/Method.closure.phpt b/tests/PhpGenerator/Method.closure.phpt index 35310426..32989bb1 100644 --- a/tests/PhpGenerator/Method.closure.phpt +++ b/tests/PhpGenerator/Method.closure.phpt @@ -4,8 +4,8 @@ * Test: Nette\PhpGenerator & closure. */ -use Nette\PhpGenerator\Method, - Tester\Assert; +use Nette\PhpGenerator\Method; +use Tester\Assert; require __DIR__ . '/../bootstrap.php'; diff --git a/tests/PhpGenerator/Method.variadics.phpt b/tests/PhpGenerator/Method.variadics.phpt index 147e81a4..2d6450c7 100644 --- a/tests/PhpGenerator/Method.variadics.phpt +++ b/tests/PhpGenerator/Method.variadics.phpt @@ -5,9 +5,9 @@ * @phpversion 5.6 */ -use Nette\PhpGenerator\Method, - Nette\PhpGenerator\Parameter, - Tester\Assert; +use Nette\PhpGenerator\Method; +use Nette\PhpGenerator\Parameter; +use Tester\Assert; require __DIR__ . '/../bootstrap.php'; diff --git a/tests/PhpGenerator/PhpNamespace.phpt b/tests/PhpGenerator/PhpNamespace.phpt index 51c36eb0..781cafd9 100644 --- a/tests/PhpGenerator/PhpNamespace.phpt +++ b/tests/PhpGenerator/PhpNamespace.phpt @@ -28,7 +28,7 @@ Assert::same($namespace, $classA->getNamespace()); $interfaceB = $namespace->addInterface('B'); Assert::same($namespace, $interfaceB->getNamespace()); -Assert::exception(function() use ($namespace) { +Assert::exception(function () use ($namespace) { $traitC = $namespace->addTrait('C'); Assert::same($namespace, $traitC->getNamespace()); }, 'Nette\InvalidStateException', "Alias 'C' used already for 'Bar\\C', cannot use for 'Foo\\C'."); From cdba4d8c36c6d69ccc3eba5305511d33bea93ef7 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sat, 20 Jun 2015 16:13:15 +0200 Subject: [PATCH 07/57] readme.md: added badges --- readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/readme.md b/readme.md index a34ac641..48e360ad 100644 --- a/readme.md +++ b/readme.md @@ -3,6 +3,8 @@ Nette PHP Generator [![Downloads this Month](https://img.shields.io/packagist/dm/nette/php-generator.svg)](https://packagist.org/packages/nette/php-generator) [![Build Status](https://travis-ci.org/nette/php-generator.svg?branch=master)](https://travis-ci.org/nette/php-generator) +[![Latest Stable Version](https://poser.pugx.org/nette/php-generator/v/stable)](https://github.com/nette/php-generator/releases) +[![License](https://img.shields.io/badge/license-New%20BSD-blue.svg)](https://github.com/nette/php-generator/blob/master/license.md) Generate PHP code with a simple programmatical API. From 9b4b63b8c8d7feb69c07c341bbd12d50a4ef863e Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sun, 21 Jun 2015 17:24:27 +0200 Subject: [PATCH 08/57] travis: testing with lowest dependencies --- .travis.yml | 6 +++++- composer.json | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2cc77a11..87920592 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,10 @@ matrix: - php: 7.0 - php: hhvm + include: + - php: 5.6 + env: dependencies="--prefer-lowest --prefer-stable" + script: - vendor/bin/tester tests -s -p php - php temp/code-checker/src/code-checker.php --short-arrays @@ -21,5 +25,5 @@ after_failure: before_script: # Install Nette Tester & Code Checker - - composer install --no-interaction --prefer-source + - composer update --no-interaction --prefer-source $dependencies - composer create-project nette/code-checker temp/code-checker ~2.5 --no-interaction --prefer-source diff --git a/composer.json b/composer.json index 82358a1d..0dcc3367 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "nette/utils": "~2.2" }, "require-dev": { - "nette/tester": "~1.0" + "nette/tester": "~1.4" }, "conflict": { "nette/nette": "<2.2" From 866bde0223160519ca6351d7c5adcd4aabf9e4d3 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 29 Jun 2015 14:45:18 +0200 Subject: [PATCH 09/57] removed @author --- src/PhpGenerator/ClassType.php | 2 -- src/PhpGenerator/Helpers.php | 2 -- src/PhpGenerator/Method.php | 2 -- src/PhpGenerator/Parameter.php | 2 -- src/PhpGenerator/PhpFile.php | 2 -- src/PhpGenerator/PhpLiteral.php | 2 -- src/PhpGenerator/PhpNamespace.php | 2 -- src/PhpGenerator/Property.php | 2 -- 8 files changed, 16 deletions(-) diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index 6639c64a..0ef96932 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -13,8 +13,6 @@ /** * Class/Interface/Trait description. - * - * @author David Grudl */ class ClassType extends Nette\Object { diff --git a/src/PhpGenerator/Helpers.php b/src/PhpGenerator/Helpers.php index d62b4c4c..3e69f037 100644 --- a/src/PhpGenerator/Helpers.php +++ b/src/PhpGenerator/Helpers.php @@ -12,8 +12,6 @@ /** * PHP code generator utils. - * - * @author David Grudl */ class Helpers { diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index 0847b554..1f021802 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -12,8 +12,6 @@ /** * Class method description. - * - * @author David Grudl */ class Method extends Nette\Object { diff --git a/src/PhpGenerator/Parameter.php b/src/PhpGenerator/Parameter.php index d38c21e5..fcd97226 100644 --- a/src/PhpGenerator/Parameter.php +++ b/src/PhpGenerator/Parameter.php @@ -12,8 +12,6 @@ /** * Method parameter description. - * - * @author David Grudl */ class Parameter extends Nette\Object { diff --git a/src/PhpGenerator/PhpFile.php b/src/PhpGenerator/PhpFile.php index 8922b089..47fa1eec 100644 --- a/src/PhpGenerator/PhpFile.php +++ b/src/PhpGenerator/PhpFile.php @@ -18,8 +18,6 @@ * - opening tag ( */ class PhpFile extends Object { diff --git a/src/PhpGenerator/PhpLiteral.php b/src/PhpGenerator/PhpLiteral.php index 19f23c92..e07233ca 100644 --- a/src/PhpGenerator/PhpLiteral.php +++ b/src/PhpGenerator/PhpLiteral.php @@ -12,8 +12,6 @@ /** * PHP literal value. - * - * @author David Grudl */ class PhpLiteral { diff --git a/src/PhpGenerator/PhpNamespace.php b/src/PhpGenerator/PhpNamespace.php index 9f86dc43..173c5554 100644 --- a/src/PhpGenerator/PhpNamespace.php +++ b/src/PhpGenerator/PhpNamespace.php @@ -19,8 +19,6 @@ * - namespace statement * - variable amount of use statements * - one or more class declarations - * - * @author Jakub Kulhan */ class PhpNamespace extends Object { diff --git a/src/PhpGenerator/Property.php b/src/PhpGenerator/Property.php index efc03ad8..5440e642 100644 --- a/src/PhpGenerator/Property.php +++ b/src/PhpGenerator/Property.php @@ -12,8 +12,6 @@ /** * Class property description. - * - * @author David Grudl */ class Property extends Nette\Object { From 386f46c11817a0e5e287a1a9b6ec68876a6bac72 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 29 Jun 2015 15:59:06 +0200 Subject: [PATCH 10/57] typo: removed unused use --- src/PhpGenerator/PhpLiteral.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/PhpGenerator/PhpLiteral.php b/src/PhpGenerator/PhpLiteral.php index e07233ca..be8a2463 100644 --- a/src/PhpGenerator/PhpLiteral.php +++ b/src/PhpGenerator/PhpLiteral.php @@ -7,8 +7,6 @@ namespace Nette\PhpGenerator; -use Nette; - /** * PHP literal value. From 37f5128659a77b32b983a6efeaa9519e31586a4f Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sat, 11 Jul 2015 22:58:41 +0200 Subject: [PATCH 11/57] travis: migrating to container-based infrastructure --- .travis.yml | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 87920592..83564d57 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,5 +25,11 @@ after_failure: before_script: # Install Nette Tester & Code Checker - - composer update --no-interaction --prefer-source $dependencies - - composer create-project nette/code-checker temp/code-checker ~2.5 --no-interaction --prefer-source + - travis_retry composer update --no-interaction --prefer-dist $dependencies + - travis_retry composer create-project nette/code-checker temp/code-checker ~2.5 --no-interaction + +sudo: false + +cache: + directories: + - $HOME/.composer/cache From 7eb7569c2a760731c3907dead07dd0733bd64318 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 19 Aug 2015 16:33:06 +0200 Subject: [PATCH 12/57] Method, ClassType: better whitespace usage --- src/PhpGenerator/ClassType.php | 10 +++--- src/PhpGenerator/Method.php | 2 +- tests/PhpGenerator/ClassType.expect | 2 -- tests/PhpGenerator/ClassType.from.expect | 7 ---- .../PhpGenerator/ClassType.from.trait.expect | 7 ---- .../PhpGenerator/ClassType.inheritance.expect | 2 -- tests/PhpGenerator/PhpFile.bracketed.expect | 33 ------------------- tests/PhpGenerator/PhpFile.regular.expect | 27 --------------- tests/PhpGenerator/PhpNamespace.expect | 6 +--- 9 files changed, 7 insertions(+), 89 deletions(-) diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index 0ef96932..d48483f6 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -157,13 +157,13 @@ public function __toString() . $this->name . ' ' . ($this->extends ? 'extends ' . implode(', ', $extends) . ' ' : '') . ($this->implements ? 'implements ' . implode(', ', $implements) . ' ' : '') - . "\n{\n\n" + . "\n{\n" . Strings::indent( ($this->traits ? 'use ' . implode(', ', $traits) . ";\n\n" : '') - . ($this->consts ? implode('', $consts) . "\n\n" : '') - . ($this->properties ? implode("\n", $properties) . "\n\n" : '') - . implode("\n\n\n", $this->methods), 1) - . "\n\n}" + . ($this->consts ? implode('', $consts) . "\n" : '') + . ($this->properties ? implode("\n", $properties) . "\n" : '') + . ($this->methods ? "\n" . implode("\n\n\n", $this->methods) . "\n\n" : ''), 1) + . '}' ) . "\n"; } diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index 1f021802..23bc9c91 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -108,7 +108,7 @@ public function __toString() . '(' . implode(', ', $parameters) . ')' . ($this->uses ? ' use (' . implode(', ', $uses) . ')' : '') . ($this->abstract || $this->body === FALSE ? ';' - : ($this->name ? "\n" : ' ') . "{\n" . Nette\Utils\Strings::indent(trim($this->body), 1) . "\n}"); + : ($this->name ? "\n" : ' ') . "{\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) . '}'); } diff --git a/tests/PhpGenerator/ClassType.expect b/tests/PhpGenerator/ClassType.expect index e8476f6e..6e20fd0e 100644 --- a/tests/PhpGenerator/ClassType.expect +++ b/tests/PhpGenerator/ClassType.expect @@ -6,13 +6,11 @@ */ abstract final class Example extends ParentClass implements IExample, IOne { - use ObjectTrait; const ROLE = 'admin'; const FORCE_ARRAY = Nette\Utils\Json::FORCE_ARRAY; - /** @var resource orignal file handle */ private $handle; diff --git a/tests/PhpGenerator/ClassType.from.expect b/tests/PhpGenerator/ClassType.from.expect index c6eef384..b417f6e8 100644 --- a/tests/PhpGenerator/ClassType.from.expect +++ b/tests/PhpGenerator/ClassType.from.expect @@ -11,9 +11,6 @@ interface Interface1 interface Interface2 { - - - } abstract class Class1 implements Interface1 @@ -24,7 +21,6 @@ abstract class Class1 implements Interface1 */ function func1() { - } @@ -34,7 +30,6 @@ abstract class Class1 implements Interface1 class Class2 extends Class1 implements Interface2 { - /** * Public * @var int @@ -55,13 +50,11 @@ class Class2 extends Class1 implements Interface2 */ private function & func3(array $a%a?%, Class2 $b = NULL, Unknown $c, \Xyz\Unknown $d, $e) { - } final function func2() { - } } \ No newline at end of file diff --git a/tests/PhpGenerator/ClassType.from.trait.expect b/tests/PhpGenerator/ClassType.from.trait.expect index 19be7237..2aebe46f 100644 --- a/tests/PhpGenerator/ClassType.from.trait.expect +++ b/tests/PhpGenerator/ClassType.from.trait.expect @@ -6,7 +6,6 @@ trait Trait1 function func1() { - } } @@ -16,7 +15,6 @@ trait Trait2 protected function func2() { - } } @@ -26,20 +24,15 @@ abstract class Class1 function func1() { - } protected function func2() { - } } class Class2 extends Class1 { - - - } \ No newline at end of file diff --git a/tests/PhpGenerator/ClassType.inheritance.expect b/tests/PhpGenerator/ClassType.inheritance.expect index b35bc9e3..934924d3 100644 --- a/tests/PhpGenerator/ClassType.inheritance.expect +++ b/tests/PhpGenerator/ClassType.inheritance.expect @@ -1,6 +1,5 @@ class B extends A { - public $d; protected $e; @@ -10,7 +9,6 @@ class B extends A function bar() { - } } diff --git a/tests/PhpGenerator/PhpFile.bracketed.expect b/tests/PhpGenerator/PhpFile.bracketed.expect index 671e07b7..bab4953b 100644 --- a/tests/PhpGenerator/PhpFile.bracketed.expect +++ b/tests/PhpGenerator/PhpFile.bracketed.expect @@ -9,25 +9,16 @@ namespace Foo { class A implements A, \Bar\C { - use C, \Bar\D; - - } interface B { - - - } trait C { - - - } } @@ -37,25 +28,16 @@ namespace Bar { class B extends \Foo\A implements \Foo\B { - use \Foo\C; - - } interface C { - - - } trait D { - - - } } @@ -65,23 +47,14 @@ namespace Baz { class E { - - - } interface F extends \Foo\B, \Bar\C { - - - } trait G { - - - } } @@ -91,9 +64,6 @@ namespace { class H { - - - } } @@ -103,9 +73,6 @@ namespace FooBar { class I { - - - } } diff --git a/tests/PhpGenerator/PhpFile.regular.expect b/tests/PhpGenerator/PhpFile.regular.expect index 614b269d..07476b02 100644 --- a/tests/PhpGenerator/PhpFile.regular.expect +++ b/tests/PhpGenerator/PhpFile.regular.expect @@ -9,25 +9,16 @@ namespace Foo; class A implements A, \Bar\C { - use C, \Bar\D; - - } interface B { - - - } trait C { - - - } @@ -35,25 +26,16 @@ namespace Bar; class B extends \Foo\A implements \Foo\B { - use \Foo\C; - - } interface C { - - - } trait D { - - - } @@ -61,21 +43,12 @@ namespace Baz; class E { - - - } interface F extends \Foo\B, \Bar\C { - - - } trait G { - - - } diff --git a/tests/PhpGenerator/PhpNamespace.expect b/tests/PhpGenerator/PhpNamespace.expect index 8ae51347..12e43871 100644 --- a/tests/PhpGenerator/PhpNamespace.expect +++ b/tests/PhpGenerator/PhpNamespace.expect @@ -4,19 +4,15 @@ use Bar\C; class A implements A, C { - use \Bar\D; + public function test(C $test) { - } } interface B { - - - } From 0b6ea54a7f90498cd5224c0944a0fbf2f2231b46 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 19 Aug 2015 17:11:56 +0200 Subject: [PATCH 13/57] Method, Parameter: added support for build-in types callable, self, parent --- src/PhpGenerator/Method.php | 5 +++-- src/PhpGenerator/Parameter.php | 18 +++++++++++------- tests/PhpGenerator/ClassType.from.expect | 2 +- tests/PhpGenerator/ClassType.from.phpt | 2 +- tests/PhpGenerator/PhpNamespace.expect | 2 +- tests/PhpGenerator/PhpNamespace.phpt | 13 ++++++++----- 6 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index 23bc9c91..bf5c62ae 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -80,12 +80,13 @@ public static function from($from) */ public function __toString() { + static $builtinTypes = ['array', 'self', 'parent', 'callable', '']; $parameters = []; foreach ($this->parameters as $param) { $variadic = $this->variadic && $param === end($this->parameters); - $hint = in_array($param->getTypeHint(), ['array', '']) + $hint = !$this->namespace || in_array($param->getTypeHint(), $builtinTypes, TRUE) ? $param->getTypeHint() - : ($this->namespace ? $this->namespace->unresolveName($param->getTypeHint()) : $param->getTypeHint()); + : $this->namespace->unresolveName($param->getTypeHint()); $parameters[] = ($hint ? $hint . ' ' : '') . ($param->isReference() ? '&' : '') diff --git a/src/PhpGenerator/Parameter.php b/src/PhpGenerator/Parameter.php index fcd97226..3239e5b3 100644 --- a/src/PhpGenerator/Parameter.php +++ b/src/PhpGenerator/Parameter.php @@ -39,13 +39,17 @@ public static function from(\ReflectionParameter $from) $param = new static; $param->name = $from->getName(); $param->reference = $from->isPassedByReference(); - try { - $param->typeHint = $from->isArray() ? 'array' : ($from->getClass() ? '\\' . $from->getClass()->getName() : ''); - } catch (\ReflectionException $e) { - if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) { - $param->typeHint = '\\' . $m[1]; - } else { - throw $e; + if ($from->isArray() || $from->isCallable()) { + $param->typeHint = $from->isArray() ? 'array' : 'callable'; + } else { + try { + $param->typeHint = $from->getClass() ? '\\' . $from->getClass()->getName() : ''; + } catch (\ReflectionException $e) { + if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) { + $param->typeHint = '\\' . $m[1]; + } else { + throw $e; + } } } $param->optional = PHP_VERSION_ID < 50407 ? $from->isOptional() || ($param->typeHint && $from->allowsNull()) : $from->isDefaultValueAvailable(); diff --git a/tests/PhpGenerator/ClassType.from.expect b/tests/PhpGenerator/ClassType.from.expect index b417f6e8..7c527d15 100644 --- a/tests/PhpGenerator/ClassType.from.expect +++ b/tests/PhpGenerator/ClassType.from.expect @@ -48,7 +48,7 @@ class Class2 extends Class1 implements Interface2 * Func3 * @return Class1 */ - private function & func3(array $a%a?%, Class2 $b = NULL, Unknown $c, \Xyz\Unknown $d, $e) + private function & func3(array $a%a?%, Class2 $b = NULL, Unknown $c, \Xyz\Unknown $d, callable $e, $f) { } diff --git a/tests/PhpGenerator/ClassType.from.phpt b/tests/PhpGenerator/ClassType.from.phpt index 38cbf8a4..f5f49870 100644 --- a/tests/PhpGenerator/ClassType.from.phpt +++ b/tests/PhpGenerator/ClassType.from.phpt @@ -55,7 +55,7 @@ class Class2 extends Class1 implements Interface2 * Func3 * @return Class1 */ - private function & func3(array $a = [], Class2 $b = NULL, \Abc\Unknown $c, \Xyz\Unknown $d, $e) + private function & func3(array $a = [], Class2 $b = NULL, \Abc\Unknown $c, \Xyz\Unknown $d, callable $e, $f) {} final function func2() diff --git a/tests/PhpGenerator/PhpNamespace.expect b/tests/PhpGenerator/PhpNamespace.expect index 12e43871..742da2ba 100644 --- a/tests/PhpGenerator/PhpNamespace.expect +++ b/tests/PhpGenerator/PhpNamespace.expect @@ -7,7 +7,7 @@ class A implements A, C use \Bar\D; - public function test(C $test) + public function test(C $a, self $b, parent $c, array $d, callable $e) { } diff --git a/tests/PhpGenerator/PhpNamespace.phpt b/tests/PhpGenerator/PhpNamespace.phpt index 781cafd9..03479bc6 100644 --- a/tests/PhpGenerator/PhpNamespace.phpt +++ b/tests/PhpGenerator/PhpNamespace.phpt @@ -36,10 +36,13 @@ Assert::exception(function () use ($namespace) { $classA ->addImplement('Foo\\A') ->addImplement('Bar\\C') - ->addTrait('Bar\\D') - ->addMethod('test') - ->addParameter('test') - ->setTypeHint('Bar\C'); - + ->addTrait('Bar\\D'); + +$method = $classA->addMethod('test'); +$method->addParameter('a')->setTypeHint('Bar\C'); +$method->addParameter('b')->setTypeHint('self'); +$method->addParameter('c')->setTypeHint('parent'); +$method->addParameter('d')->setTypeHint('array'); +$method->addParameter('e')->setTypeHint('callable'); Assert::matchFile(__DIR__ . '/PhpNamespace.expect', (string) $namespace); From 34cee0b6261b4c64034c8e3d111305331637dc67 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 19 Aug 2015 17:44:43 +0200 Subject: [PATCH 14/57] ClassType::__toString() refactoring --- src/PhpGenerator/ClassType.php | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index d48483f6..fba2b7a2 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -125,24 +125,13 @@ public function __toString() . ";\n"; } - $extends = $implements = $traits = []; + $extends = (array) $this->extends; + $implements = $this->implements; + $traits = $this->traits; if ($this->namespace) { - foreach ((array) $this->extends as $name) { - $extends[] = $this->namespace->unresolveName($name); - } - - foreach ((array) $this->implements as $name) { - $implements[] = $this->namespace->unresolveName($name); - } - - foreach ((array) $this->traits as $name) { - $traits[] = $this->namespace->unresolveName($name); - } - - } else { - $extends = (array) $this->extends; - $implements = (array) $this->implements; - $traits = (array) $this->traits; + $extends = array_map([$this->namespace, 'unresolveName'], $extends); + $implements = array_map([$this->namespace, 'unresolveName'], $implements); + $traits = array_map([$this->namespace, 'unresolveName'], $traits); } foreach ($this->methods as $method) { @@ -155,11 +144,11 @@ public function __toString() . ($this->final ? 'final ' : '') . $this->type . ' ' . $this->name . ' ' - . ($this->extends ? 'extends ' . implode(', ', $extends) . ' ' : '') - . ($this->implements ? 'implements ' . implode(', ', $implements) . ' ' : '') + . ($extends ? 'extends ' . implode(', ', $extends) . ' ' : '') + . ($implements ? 'implements ' . implode(', ', $implements) . ' ' : '') . "\n{\n" . Strings::indent( - ($this->traits ? 'use ' . implode(', ', $traits) . ";\n\n" : '') + ($traits ? 'use ' . implode(', ', $traits) . ";\n\n" : '') . ($this->consts ? implode('', $consts) . "\n" : '') . ($this->properties ? implode("\n", $properties) . "\n" : '') . ($this->methods ? "\n" . implode("\n\n\n", $this->methods) . "\n\n" : ''), 1) From aeafb386f2a946eb873d6275d92b4819d22fedb0 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 19 Aug 2015 17:41:23 +0200 Subject: [PATCH 15/57] fixed content for properties $documents --- src/PhpGenerator/ClassType.php | 6 +++--- src/PhpGenerator/Method.php | 4 ++-- src/PhpGenerator/PhpFile.php | 4 ++-- src/PhpGenerator/Property.php | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index fba2b7a2..1e6122de 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -71,7 +71,7 @@ public static function from($from) $class->final = $from->isFinal() && $class->type === 'class'; $class->abstract = $from->isAbstract() && $class->type === 'class'; $class->implements = $from->getInterfaceNames(); - $class->documents = preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")); + $class->documents = $from->getDocComment() ? [preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t"))] : []; $namespace = $from->getNamespaceName(); if ($from->getParentClass()) { $class->extends = $from->getParentClass()->getName(); @@ -118,7 +118,7 @@ public function __toString() $properties = []; foreach ($this->properties as $property) { - $doc = str_replace("\n", "\n * ", implode("\n", (array) $property->getDocuments())); + $doc = str_replace("\n", "\n * ", implode("\n", $property->getDocuments())); $properties[] = ($property->getDocuments() ? (strpos($doc, "\n") === FALSE ? "/** $doc */\n" : "/**\n * $doc\n */\n") : '') . $property->getVisibility() . ($property->isStatic() ? ' static' : '') . ' $' . $property->getName() . ($property->value === NULL ? '' : ' = ' . Helpers::dump($property->value)) @@ -139,7 +139,7 @@ public function __toString() } return Strings::normalize( - ($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $this->documents)) . "\n */\n" : '') + ($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", $this->documents)) . "\n */\n" : '') . ($this->abstract ? 'abstract ' : '') . ($this->final ? 'final ' : '') . $this->type . ' ' diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index bf5c62ae..084c2777 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -70,7 +70,7 @@ public static function from($from) $method->body = $from->isAbstract() ? FALSE : ''; $method->returnReference = $from->returnsReference(); $method->variadic = PHP_VERSION_ID >= 50600 && $from->isVariadic(); - $method->documents = preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")); + $method->documents = $from->getDocComment() ? [preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t"))] : []; return $method; } @@ -98,7 +98,7 @@ public function __toString() foreach ($this->uses as $param) { $uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName(); } - return ($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $this->documents)) . "\n */\n" : '') + return ($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", $this->documents)) . "\n */\n" : '') . ($this->abstract ? 'abstract ' : '') . ($this->final ? 'final ' : '') . ($this->visibility ? $this->visibility . ' ' : '') diff --git a/src/PhpGenerator/PhpFile.php b/src/PhpGenerator/PhpFile.php index 47fa1eec..02a342f2 100644 --- a/src/PhpGenerator/PhpFile.php +++ b/src/PhpGenerator/PhpFile.php @@ -22,7 +22,7 @@ class PhpFile extends Object { /** @var string[] */ - private $documents; + private $documents = []; /** @var PhpNamespace[] */ private $namespaces = []; @@ -119,7 +119,7 @@ public function __toString() return Strings::normalize( "documents ? "\n" . str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $this->documents)) . "\n */\n\n" : '') + . ($this->documents ? "\n" . str_replace("\n", "\n * ", "/**\n" . implode("\n", $this->documents)) . "\n */\n\n" : '') . implode("\n\n", $this->namespaces) ) . "\n"; } diff --git a/src/PhpGenerator/Property.php b/src/PhpGenerator/Property.php index 5440e642..6a74d169 100644 --- a/src/PhpGenerator/Property.php +++ b/src/PhpGenerator/Property.php @@ -42,7 +42,7 @@ public static function from(\ReflectionProperty $from) $prop->value = isset($defaults[$prop->name]) ? $defaults[$prop->name] : NULL; $prop->static = $from->isStatic(); $prop->visibility = $from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : 'public'); - $prop->documents = preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")); + $prop->documents = $from->getDocComment() ? [preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t"))] : []; return $prop; } From 845fc5f4e4ccfe56e997bb485fcf041ed392988a Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 19 Aug 2015 09:43:27 +0200 Subject: [PATCH 16/57] typos --- src/PhpGenerator/ClassType.php | 8 ++++---- src/PhpGenerator/Method.php | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index 1e6122de..98b2cf99 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -22,7 +22,7 @@ class ClassType extends Nette\Object const TYPE_TRAIT = 'trait'; - /** @var PhpNamespace */ + /** @var PhpNamespace|NULL */ private $namespace; /** @var string */ @@ -37,7 +37,7 @@ class ClassType extends Nette\Object /** @var bool */ private $abstract = FALSE; - /** @var strings|string[] */ + /** @var string|string[] */ private $extends = []; /** @var string[] */ @@ -250,7 +250,7 @@ public function isAbstract() /** - * @param strings|string[] + * @param string|string[] * @return self */ public function setExtends($types) @@ -264,7 +264,7 @@ public function setExtends($types) /** - * @return strings|string[] + * @return string|string[] */ public function getExtends() { diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index 084c2777..939b865c 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -48,7 +48,7 @@ class Method extends Nette\Object /** @var array of string */ private $documents = []; - /** @var PhpNamespace */ + /** @var PhpNamespace|NULL */ private $namespace; From 89d5a344250f8a348e6f7ac5c94d424b4e2c868e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Kou=C5=99il?= Date: Tue, 18 Aug 2015 18:50:21 +0200 Subject: [PATCH 17/57] Method, Parameter: added support for php 7 type hints --- src/PhpGenerator/Method.php | 32 +++++++++++- src/PhpGenerator/Parameter.php | 5 +- tests/PhpGenerator/Method.returnTypes.phpt | 49 +++++++++++++++++++ .../PhpGenerator/Method.scalarParameters.phpt | 49 +++++++++++++++++++ 4 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 tests/PhpGenerator/Method.returnTypes.phpt create mode 100644 tests/PhpGenerator/Method.scalarParameters.phpt diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index 939b865c..9d0ea0c7 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -51,6 +51,9 @@ class Method extends Nette\Object /** @var PhpNamespace|NULL */ private $namespace; + /** @var string */ + private $returnType; + /** * @return self @@ -71,6 +74,10 @@ public static function from($from) $method->returnReference = $from->returnsReference(); $method->variadic = PHP_VERSION_ID >= 50600 && $from->isVariadic(); $method->documents = $from->getDocComment() ? [preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t"))] : []; + if (PHP_VERSION_ID >= 70000 && $from->hasReturnType()) { + $returnType = $from->getReturnType(); + $method->returnType = $returnType->isBuiltin() ? (string) $returnType : '\\' . $returnType; + } return $method; } @@ -80,7 +87,7 @@ public static function from($from) */ public function __toString() { - static $builtinTypes = ['array', 'self', 'parent', 'callable', '']; + static $builtinTypes = ['array', 'self', 'parent', 'callable', 'string', 'bool', 'float', 'int', '']; $parameters = []; foreach ($this->parameters as $param) { $variadic = $this->variadic && $param === end($this->parameters); @@ -98,6 +105,9 @@ public function __toString() foreach ($this->uses as $param) { $uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName(); } + $returnType = !$this->namespace || in_array($this->returnType, $builtinTypes) + ? $this->returnType + : $this->namespace->unresolveName($this->returnType); return ($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", $this->documents)) . "\n */\n" : '') . ($this->abstract ? 'abstract ' : '') . ($this->final ? 'final ' : '') @@ -108,6 +118,7 @@ public function __toString() . ($this->name ? ' ' . $this->name : '') . '(' . implode(', ', $parameters) . ')' . ($this->uses ? ' use (' . implode(', ', $uses) . ')' : '') + . ($returnType ? ': ' . $returnType : '') . ($this->abstract || $this->body === FALSE ? ';' : ($this->name ? "\n" : ' ') . "{\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) . '}'); } @@ -393,4 +404,23 @@ public function setNamespace(PhpNamespace $val = NULL) return $this; } + /** + * @param string + * @return self + */ + public function setReturnType($val) + { + $this->returnType = (string) $val; + return $this; + } + + + /** + * @return string + */ + public function getReturnType() + { + return $this->returnType; + } + } diff --git a/src/PhpGenerator/Parameter.php b/src/PhpGenerator/Parameter.php index 3239e5b3..f999496f 100644 --- a/src/PhpGenerator/Parameter.php +++ b/src/PhpGenerator/Parameter.php @@ -39,7 +39,10 @@ public static function from(\ReflectionParameter $from) $param = new static; $param->name = $from->getName(); $param->reference = $from->isPassedByReference(); - if ($from->isArray() || $from->isCallable()) { + if (PHP_VERSION_ID >= 70000) { + $type = $from->getType(); + $param->typeHint = !$type || $type->isBuiltin() ? (string) $type : '\\' . $type; + } elseif ($from->isArray() || $from->isCallable()) { $param->typeHint = $from->isArray() ? 'array' : 'callable'; } else { try { diff --git a/tests/PhpGenerator/Method.returnTypes.phpt b/tests/PhpGenerator/Method.returnTypes.phpt new file mode 100644 index 00000000..22842263 --- /dev/null +++ b/tests/PhpGenerator/Method.returnTypes.phpt @@ -0,0 +1,49 @@ +getReturnType()); + + $method = Method::from(A::class .'::testScalar'); + Assert::same('string', $method->getReturnType()); + + // generating methods with return type declarations + + $method = (new Method) + ->setName('create') + ->setReturnType('Foo') + ->setBody('return new Foo();'); + + Assert::match( + 'function create(): Foo +{ + return new Foo(); +} +', (string) $method); + +} diff --git a/tests/PhpGenerator/Method.scalarParameters.phpt b/tests/PhpGenerator/Method.scalarParameters.phpt new file mode 100644 index 00000000..e44e3088 --- /dev/null +++ b/tests/PhpGenerator/Method.scalarParameters.phpt @@ -0,0 +1,49 @@ +getParameters()['a']->getTypeHint()); + +$method = Method::from(Foo::class . '::scalars'); +Assert::same('bool', $method->getParameters()['b']->getTypeHint()); + +$method = Method::from(Foo::class . '::scalars'); +Assert::same('int', $method->getParameters()['c']->getTypeHint()); + +$method = Method::from(Foo::class . '::scalars'); +Assert::same('float', $method->getParameters()['d']->getTypeHint()); + + +// generating methods with scalar type hints + +$method = (new Method) + ->setName('create') + ->setBody('return null;'); +$method->addParameter('a')->setTypeHint('string'); +$method->addParameter('b')->setTypeHint('bool'); + +Assert::match( +'function create(string $a, bool $b) +{ + return null; +} +', (string) $method); From 20a094c0815bf58ed53625e9428bf5c4dbb7c7b3 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 19 Aug 2015 17:54:42 +0200 Subject: [PATCH 18/57] fixed inconsistencies between 'nullable string' and 'string' properties --- src/PhpGenerator/Method.php | 28 ++++++++++++++-------------- src/PhpGenerator/Parameter.php | 14 +++++++------- src/PhpGenerator/Property.php | 4 ++-- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index 9d0ea0c7..d5331a04 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -15,7 +15,7 @@ */ class Method extends Nette\Object { - /** @var string */ + /** @var string|NULL */ private $name; /** @var array of name => Parameter */ @@ -25,7 +25,7 @@ class Method extends Nette\Object private $uses = []; /** @var string|FALSE */ - private $body; + private $body = ''; /** @var bool */ private $static = FALSE; @@ -45,13 +45,13 @@ class Method extends Nette\Object /** @var bool */ private $variadic = FALSE; - /** @var array of string */ + /** @var string[] */ private $documents = []; /** @var PhpNamespace|NULL */ private $namespace; - /** @var string */ + /** @var string|NULL */ private $returnType; @@ -67,7 +67,7 @@ public static function from($from) $method->parameters[$param->getName()] = Parameter::from($param); } $method->static = $from->isStatic(); - $method->visibility = $from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : ''); + $method->visibility = $from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : NULL); $method->final = $from->isFinal(); $method->abstract = $from->isAbstract() && !$from->getDeclaringClass()->isInterface(); $method->body = $from->isAbstract() ? FALSE : ''; @@ -87,7 +87,7 @@ public static function from($from) */ public function __toString() { - static $builtinTypes = ['array', 'self', 'parent', 'callable', 'string', 'bool', 'float', 'int', '']; + static $builtinTypes = ['array', 'self', 'parent', 'callable', 'string', 'bool', 'float', 'int', NULL]; $parameters = []; foreach ($this->parameters as $param) { $variadic = $this->variadic && $param === end($this->parameters); @@ -105,7 +105,7 @@ public function __toString() foreach ($this->uses as $param) { $uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName(); } - $returnType = !$this->namespace || in_array($this->returnType, $builtinTypes) + $returnType = !$this->namespace || in_array($this->returnType, $builtinTypes, TRUE) ? $this->returnType : $this->namespace->unresolveName($this->returnType); return ($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", $this->documents)) . "\n */\n" : '') @@ -125,18 +125,18 @@ public function __toString() /** - * @param string + * @param string|NULL * @return self */ public function setName($name) { - $this->name = (string) $name; + $this->name = $name ? (string) $name : NULL; return $this; } /** - * @return string + * @return string|NULL */ public function getName() { @@ -270,7 +270,7 @@ public function setVisibility($val) if (!in_array($val, ['public', 'protected', 'private', NULL], TRUE)) { throw new Nette\InvalidArgumentException('Argument must be public|protected|private|NULL.'); } - $this->visibility = (string) $val; + $this->visibility = $val ? (string) $val : NULL; return $this; } @@ -405,18 +405,18 @@ public function setNamespace(PhpNamespace $val = NULL) } /** - * @param string + * @param string|NULL * @return self */ public function setReturnType($val) { - $this->returnType = (string) $val; + $this->returnType = $val ? (string) $val : NULL; return $this; } /** - * @return string + * @return string|NULL */ public function getReturnType() { diff --git a/src/PhpGenerator/Parameter.php b/src/PhpGenerator/Parameter.php index f999496f..685c391b 100644 --- a/src/PhpGenerator/Parameter.php +++ b/src/PhpGenerator/Parameter.php @@ -16,12 +16,12 @@ class Parameter extends Nette\Object { /** @var string */ - private $name; + private $name = ''; /** @var bool */ private $reference = FALSE; - /** @var string */ + /** @var string|NULL */ private $typeHint; /** @var bool */ @@ -41,12 +41,12 @@ public static function from(\ReflectionParameter $from) $param->reference = $from->isPassedByReference(); if (PHP_VERSION_ID >= 70000) { $type = $from->getType(); - $param->typeHint = !$type || $type->isBuiltin() ? (string) $type : '\\' . $type; + $param->typeHint = $type ? ($type->isBuiltin() ? '' : '\\') . $type : NULL; } elseif ($from->isArray() || $from->isCallable()) { $param->typeHint = $from->isArray() ? 'array' : 'callable'; } else { try { - $param->typeHint = $from->getClass() ? '\\' . $from->getClass()->getName() : ''; + $param->typeHint = $from->getClass() ? '\\' . $from->getClass()->getName() : NULL; } catch (\ReflectionException $e) { if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) { $param->typeHint = '\\' . $m[1]; @@ -108,18 +108,18 @@ public function isReference() /** - * @param string + * @param string|NULL * @return self */ public function setTypeHint($hint) { - $this->typeHint = (string) $hint; + $this->typeHint = $hint ? (string) $hint : NULL; return $this; } /** - * @return string + * @return string|NULL */ public function getTypeHint() { diff --git a/src/PhpGenerator/Property.php b/src/PhpGenerator/Property.php index 6a74d169..2f833b87 100644 --- a/src/PhpGenerator/Property.php +++ b/src/PhpGenerator/Property.php @@ -16,7 +16,7 @@ class Property extends Nette\Object { /** @var string */ - private $name; + private $name = ''; /** @var mixed */ public $value; @@ -27,7 +27,7 @@ class Property extends Nette\Object /** @var string public|protected|private */ private $visibility = 'public'; - /** @var array of string */ + /** @var string[] */ private $documents = []; From e747943d4e2ebacb12333716a98f207533304f8e Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 21 Aug 2015 14:24:17 +0200 Subject: [PATCH 19/57] Method::from() added support for closures & global functions --- src/PhpGenerator/Method.php | 28 +++++++++---- src/PhpGenerator/Parameter.php | 2 +- tests/PhpGenerator/Method.closure.expect | 3 -- tests/PhpGenerator/Method.closure.phpt | 25 ----------- tests/PhpGenerator/Method.function.phpt | 53 ++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 38 deletions(-) delete mode 100644 tests/PhpGenerator/Method.closure.expect delete mode 100644 tests/PhpGenerator/Method.closure.phpt create mode 100644 tests/PhpGenerator/Method.function.phpt diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index d5331a04..8325a1d9 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -11,7 +11,7 @@ /** - * Class method description. + * Method or function description. */ class Method extends Nette\Object { @@ -60,17 +60,26 @@ class Method extends Nette\Object */ public static function from($from) { - $from = $from instanceof \ReflectionMethod ? $from : new \ReflectionMethod($from); + if (is_string($from) && strpos($from, '::')) { + $from = new \ReflectionMethod($from); + } elseif (is_array($from)) { + $from = new \ReflectionMethod($from[0], $from[1]); + } elseif (!$from instanceof \ReflectionFunctionAbstract) { + $from = new \ReflectionFunction($from); + } + $method = new static; - $method->name = $from->getName(); + $method->name = $from->isClosure() ? NULL : $from->getName(); foreach ($from->getParameters() as $param) { $method->parameters[$param->getName()] = Parameter::from($param); } - $method->static = $from->isStatic(); - $method->visibility = $from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : NULL); - $method->final = $from->isFinal(); - $method->abstract = $from->isAbstract() && !$from->getDeclaringClass()->isInterface(); - $method->body = $from->isAbstract() ? FALSE : ''; + if ($from instanceof \ReflectionMethod) { + $method->static = $from->isStatic(); + $method->visibility = $from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : NULL); + $method->final = $from->isFinal(); + $method->abstract = $from->isAbstract() && !$from->getDeclaringClass()->isInterface(); + $method->body = $from->isAbstract() ? FALSE : ''; + } $method->returnReference = $from->returnsReference(); $method->variadic = PHP_VERSION_ID >= 50600 && $from->isVariadic(); $method->documents = $from->getDocComment() ? [preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t"))] : []; @@ -108,6 +117,7 @@ public function __toString() $returnType = !$this->namespace || in_array($this->returnType, $builtinTypes, TRUE) ? $this->returnType : $this->namespace->unresolveName($this->returnType); + return ($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", $this->documents)) . "\n */\n" : '') . ($this->abstract ? 'abstract ' : '') . ($this->final ? 'final ' : '') @@ -115,7 +125,7 @@ public function __toString() . ($this->static ? 'static ' : '') . 'function' . ($this->returnReference ? ' &' : '') - . ($this->name ? ' ' . $this->name : '') + . ' ' . $this->name . '(' . implode(', ', $parameters) . ')' . ($this->uses ? ' use (' . implode(', ', $uses) . ')' : '') . ($returnType ? ': ' . $returnType : '') diff --git a/src/PhpGenerator/Parameter.php b/src/PhpGenerator/Parameter.php index 685c391b..479379b2 100644 --- a/src/PhpGenerator/Parameter.php +++ b/src/PhpGenerator/Parameter.php @@ -58,7 +58,7 @@ public static function from(\ReflectionParameter $from) $param->optional = PHP_VERSION_ID < 50407 ? $from->isOptional() || ($param->typeHint && $from->allowsNull()) : $from->isDefaultValueAvailable(); $param->defaultValue = $from->isDefaultValueAvailable() ? $from->getDefaultValue() : NULL; - $namespace = $from->getDeclaringClass()->getNamespaceName(); + $namespace = $from->getDeclaringClass() ? $from->getDeclaringClass()->getNamespaceName() : NULL; $namespace = $namespace ? "\\$namespace\\" : '\\'; if (Nette\Utils\Strings::startsWith($param->typeHint, $namespace)) { $param->typeHint = substr($param->typeHint, strlen($namespace)); diff --git a/tests/PhpGenerator/Method.closure.expect b/tests/PhpGenerator/Method.closure.expect deleted file mode 100644 index 65be62f5..00000000 --- a/tests/PhpGenerator/Method.closure.expect +++ /dev/null @@ -1,3 +0,0 @@ -function &($a, $b) use ($this, &$vars) { - return $a + $b; -} \ No newline at end of file diff --git a/tests/PhpGenerator/Method.closure.phpt b/tests/PhpGenerator/Method.closure.phpt deleted file mode 100644 index 32989bb1..00000000 --- a/tests/PhpGenerator/Method.closure.phpt +++ /dev/null @@ -1,25 +0,0 @@ -setReturnReference(TRUE) - ->setBody('return $a + $b;'); - -$function->addParameter('a'); -$function->addParameter('b'); -$function->addUse('this'); -$function->addUse('vars') - ->setReference(TRUE); - -Assert::matchFile(__DIR__ . '/Method.closure.expect', (string) $function); diff --git a/tests/PhpGenerator/Method.function.phpt b/tests/PhpGenerator/Method.function.phpt new file mode 100644 index 00000000..622764c8 --- /dev/null +++ b/tests/PhpGenerator/Method.function.phpt @@ -0,0 +1,53 @@ +setReturnReference(TRUE) + ->setBody('return $a + $b;'); + +$function->addParameter('a'); +$function->addParameter('b'); +$function->addUse('this'); +$function->addUse('vars') + ->setReference(TRUE); + +Assert::match( +'function & ($a, $b) use ($this, &$vars) { + return $a + $b; +}', (string) $function); + + +/** closure */ +$closure = function (stdClass $a, $b = NULL) {}; +$function = Method::from($closure); +Assert::match( +'/** + * closure + */ +function (stdClass $a, $b = NULL) { +}', (string) $function); + + +/** global */ +function func(stdClass $a, $b = NULL) { +}; + +$function = Method::from('func'); +Assert::match( +'/** + * global + */ +function func(stdClass $a, $b = NULL) +{ +}', (string) $function); From 9d3d8d411aea4de31f0c39e1ff235e28a05bd19d Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 19 Aug 2015 18:10:17 +0200 Subject: [PATCH 20/57] deprecated addDocument(), setDocuments() and getDocuments() and replaced with addComment(), setComment() and getComment() --- src/PhpGenerator/ClassType.php | 49 +++++++++++++++------ src/PhpGenerator/Method.php | 47 +++++++++++++++----- src/PhpGenerator/PhpFile.php | 49 +++++++++++++++------ src/PhpGenerator/Property.php | 45 ++++++++++++++----- tests/PhpGenerator/ClassType.interface.phpt | 2 +- tests/PhpGenerator/ClassType.phpt | 10 ++--- tests/PhpGenerator/PhpFile.phpt | 4 +- 7 files changed, 148 insertions(+), 58 deletions(-) diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index 98b2cf99..64c72adb 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -46,8 +46,8 @@ class ClassType extends Nette\Object /** @var string[] */ private $traits = []; - /** @var string[] */ - private $documents = []; + /** @var string|NULL */ + private $comment; /** @var array name => value */ private $consts = []; @@ -71,7 +71,7 @@ public static function from($from) $class->final = $from->isFinal() && $class->type === 'class'; $class->abstract = $from->isAbstract() && $class->type === 'class'; $class->implements = $from->getInterfaceNames(); - $class->documents = $from->getDocComment() ? [preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t"))] : []; + $class->comment = $from->getDocComment() ? preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")) : NULL; $namespace = $from->getNamespaceName(); if ($from->getParentClass()) { $class->extends = $from->getParentClass()->getName(); @@ -118,8 +118,8 @@ public function __toString() $properties = []; foreach ($this->properties as $property) { - $doc = str_replace("\n", "\n * ", implode("\n", $property->getDocuments())); - $properties[] = ($property->getDocuments() ? (strpos($doc, "\n") === FALSE ? "/** $doc */\n" : "/**\n * $doc\n */\n") : '') + $doc = str_replace("\n", "\n * ", $property->getComment()); + $properties[] = ($doc ? (strpos($doc, "\n") === FALSE ? "/** $doc */\n" : "/**\n * $doc\n */\n") : '') . $property->getVisibility() . ($property->isStatic() ? ' static' : '') . ' $' . $property->getName() . ($property->value === NULL ? '' : ' = ' . Helpers::dump($property->value)) . ";\n"; @@ -139,7 +139,7 @@ public function __toString() } return Strings::normalize( - ($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", $this->documents)) . "\n */\n" : '') + ($this->comment ? str_replace("\n", "\n * ", "/**\n" . $this->comment) . "\n */\n" : '') . ($this->abstract ? 'abstract ' : '') . ($this->final ? 'final ' : '') . $this->type . ' ' @@ -347,22 +347,22 @@ public function addTrait($trait) /** - * @param string[] + * @param string|NULL * @return self */ - public function setDocuments(array $s) + public function setComment($val) { - $this->documents = $s; + $this->comment = $val ? (string) $val : NULL; return $this; } /** - * @return string[] + * @return string|NULL */ - public function getDocuments() + public function getComment() { - return $this->documents; + return $this->comment; } @@ -370,13 +370,34 @@ public function getDocuments() * @param string * @return self */ - public function addDocument($s) + public function addComment($val) { - $this->documents[] = (string) $s; + $this->comment .= $this->comment ? "\n$val" : $val; return $this; } + /** @deprecated */ + public function setDocuments(array $s) + { + return $this->setComment(implode("\n", $s)); + } + + + /** @deprecated */ + public function getDocuments() + { + return $this->comment ? [$this->comment] : []; + } + + + /** @deprecated */ + public function addDocument($s) + { + return $this->addComment($s); + } + + /** * @return self */ diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index 8325a1d9..99283bb1 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -45,8 +45,8 @@ class Method extends Nette\Object /** @var bool */ private $variadic = FALSE; - /** @var string[] */ - private $documents = []; + /** @var string|NULL */ + private $comment; /** @var PhpNamespace|NULL */ private $namespace; @@ -82,7 +82,7 @@ public static function from($from) } $method->returnReference = $from->returnsReference(); $method->variadic = PHP_VERSION_ID >= 50600 && $from->isVariadic(); - $method->documents = $from->getDocComment() ? [preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t"))] : []; + $method->comment = $from->getDocComment() ? preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")) : NULL; if (PHP_VERSION_ID >= 70000 && $from->hasReturnType()) { $returnType = $from->getReturnType(); $method->returnType = $returnType->isBuiltin() ? (string) $returnType : '\\' . $returnType; @@ -118,7 +118,7 @@ public function __toString() ? $this->returnType : $this->namespace->unresolveName($this->returnType); - return ($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", $this->documents)) . "\n */\n" : '') + return ($this->comment ? str_replace("\n", "\n * ", "/**\n" . $this->comment) . "\n */\n" : '') . ($this->abstract ? 'abstract ' : '') . ($this->final ? 'final ' : '') . ($this->visibility ? $this->visibility . ' ' : '') @@ -374,23 +374,25 @@ public function isVariadic() } + + /** - * @param string[] + * @param string|NULL * @return self */ - public function setDocuments(array $val) + public function setComment($val) { - $this->documents = $val; + $this->comment = $val ? (string) $val : NULL; return $this; } /** - * @return string[] + * @return string|NULL */ - public function getDocuments() + public function getComment() { - return $this->documents; + return $this->comment; } @@ -398,13 +400,34 @@ public function getDocuments() * @param string * @return self */ - public function addDocument($val) + public function addComment($val) { - $this->documents[] = (string) $val; + $this->comment .= $this->comment ? "\n$val" : $val; return $this; } + /** @deprecated */ + public function setDocuments(array $s) + { + return $this->setComment(implode("\n", $s)); + } + + + /** @deprecated */ + public function getDocuments() + { + return $this->comment ? [$this->comment] : []; + } + + + /** @deprecated */ + public function addDocument($s) + { + return $this->addComment($s); + } + + /** * @return self */ diff --git a/src/PhpGenerator/PhpFile.php b/src/PhpGenerator/PhpFile.php index 02a342f2..d0bfd8f1 100644 --- a/src/PhpGenerator/PhpFile.php +++ b/src/PhpGenerator/PhpFile.php @@ -21,30 +21,32 @@ */ class PhpFile extends Object { - /** @var string[] */ - private $documents = []; + /** @var string|NULL */ + private $comment; /** @var PhpNamespace[] */ private $namespaces = []; + + /** - * @return string[] + * @param string|NULL + * @return self */ - public function getDocuments() + public function setComment($val) { - return $this->documents; + $this->comment = $val ? (string) $val : NULL; + return $this; } /** - * @param string[] - * @return self + * @return string|NULL */ - public function setDocuments(array $documents) + public function getComment() { - $this->documents = $documents; - return $this; + return $this->comment; } @@ -52,13 +54,34 @@ public function setDocuments(array $documents) * @param string * @return self */ - public function addDocument($document) + public function addComment($val) { - $this->documents[] = $document; + $this->comment .= $this->comment ? "\n$val" : $val; return $this; } + /** @deprecated */ + public function setDocuments(array $s) + { + return $this->setComment(implode("\n", $s)); + } + + + /** @deprecated */ + public function getDocuments() + { + return $this->comment ? [$this->comment] : []; + } + + + /** @deprecated */ + public function addDocument($s) + { + return $this->addComment($s); + } + + /** * @param string * @return ClassType @@ -119,7 +142,7 @@ public function __toString() return Strings::normalize( "documents ? "\n" . str_replace("\n", "\n * ", "/**\n" . implode("\n", $this->documents)) . "\n */\n\n" : '') + . ($this->comment ? "\n" . str_replace("\n", "\n * ", "/**\n" . $this->comment) . "\n */\n\n" : '') . implode("\n\n", $this->namespaces) ) . "\n"; } diff --git a/src/PhpGenerator/Property.php b/src/PhpGenerator/Property.php index 2f833b87..5bd427a7 100644 --- a/src/PhpGenerator/Property.php +++ b/src/PhpGenerator/Property.php @@ -27,8 +27,8 @@ class Property extends Nette\Object /** @var string public|protected|private */ private $visibility = 'public'; - /** @var string[] */ - private $documents = []; + /** @var string|NULL */ + private $comment; /** @@ -42,7 +42,7 @@ public static function from(\ReflectionProperty $from) $prop->value = isset($defaults[$prop->name]) ? $defaults[$prop->name] : NULL; $prop->static = $from->isStatic(); $prop->visibility = $from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : 'public'); - $prop->documents = $from->getDocComment() ? [preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t"))] : []; + $prop->comment = $from->getDocComment() ? preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")) : NULL; return $prop; } @@ -129,23 +129,25 @@ public function getVisibility() } + + /** - * @param string[] + * @param string|NULL * @return self */ - public function setDocuments(array $s) + public function setComment($val) { - $this->documents = $s; + $this->comment = $val ? (string) $val : NULL; return $this; } /** - * @return string[] + * @return string|NULL */ - public function getDocuments() + public function getComment() { - return $this->documents; + return $this->comment; } @@ -153,10 +155,31 @@ public function getDocuments() * @param string * @return self */ - public function addDocument($s) + public function addComment($val) { - $this->documents[] = (string) $s; + $this->comment .= $this->comment ? "\n$val" : $val; return $this; } + + /** @deprecated */ + public function setDocuments(array $s) + { + return $this->setComment(implode("\n", $s)); +} + + + /** @deprecated */ + public function getDocuments() + { + return $this->comment ? [$this->comment] : []; + } + + + /** @deprecated */ + public function addDocument($s) + { + return $this->addComment($s); + } + } diff --git a/tests/PhpGenerator/ClassType.interface.phpt b/tests/PhpGenerator/ClassType.interface.phpt index 75dd4905..f69b634f 100644 --- a/tests/PhpGenerator/ClassType.interface.phpt +++ b/tests/PhpGenerator/ClassType.interface.phpt @@ -16,7 +16,7 @@ $interface ->setType('interface') ->addExtend('IOne') ->addExtend('ITwo') - ->addDocument('Description of interface'); + ->addComment('Description of interface'); $interface->addMethod('getForm'); diff --git a/tests/PhpGenerator/ClassType.phpt b/tests/PhpGenerator/ClassType.phpt index bc6575ee..6ac11b5e 100644 --- a/tests/PhpGenerator/ClassType.phpt +++ b/tests/PhpGenerator/ClassType.phpt @@ -20,8 +20,8 @@ $class ->addImplement('IExample') ->addImplement('IOne') ->addTrait('ObjectTrait') - ->addDocument("Description of class.\nThis is example\n") - ->addDocument('@property-read Nette\Forms\Form $form'); + ->addComment("Description of class.\nThis is example\n") + ->addComment('@property-read Nette\Forms\Form $form'); $class ->addConst('ROLE', 'admin') @@ -29,7 +29,7 @@ $class $class->addProperty('handle') ->setVisibility('private') - ->addDocument('@var resource orignal file handle'); + ->addComment('@var resource orignal file handle'); $class->addProperty('order') ->setValue(new PhpLiteral('RecursiveIteratorIterator::SELF_FIRST')); @@ -40,8 +40,8 @@ $p = $class->addProperty('sections', ['first' => TRUE]) Assert::same($p, $class->getProperty('sections')); $m = $class->addMethod('getHandle') - ->addDocument('Returns file handle.') - ->addDocument('@return resource') + ->addComment('Returns file handle.') + ->addComment('@return resource') ->setFinal(TRUE) ->setBody('return $this->?;', ['handle']); diff --git a/tests/PhpGenerator/PhpFile.phpt b/tests/PhpGenerator/PhpFile.phpt index ef95b967..d7fe0607 100644 --- a/tests/PhpGenerator/PhpFile.phpt +++ b/tests/PhpGenerator/PhpFile.phpt @@ -13,8 +13,8 @@ require __DIR__ . '/../bootstrap.php'; $file = new PhpFile; -$file->addDocument('This file is auto-generated. DO NOT EDIT!'); -$file->addDocument('Hey there, I\'m here to document things.'); +$file->addComment('This file is auto-generated. DO NOT EDIT!'); +$file->addComment('Hey there, I\'m here to document things.'); $namespaceFoo = $file->addNamespace('Foo'); From c08df14036601711129bf118503ee516f51ad240 Mon Sep 17 00:00:00 2001 From: Jan Tvrdik Date: Sun, 23 Aug 2015 23:56:44 +0200 Subject: [PATCH 21/57] requires PHP 5.6.0 --- .travis.yml | 2 -- composer.json | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 83564d57..4d2d58e4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,5 @@ language: php php: - - 5.4 - - 5.5 - 5.6 - 7.0 - hhvm diff --git a/composer.json b/composer.json index 0dcc3367..b83a0d90 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ } ], "require": { - "php": ">=5.4.4", + "php": ">=5.6.0", "nette/utils": "~2.2" }, "require-dev": { From acdfd974a9c15960ea67d3afebf9ef0a480899ed Mon Sep 17 00:00:00 2001 From: Jan Tvrdik Date: Sun, 23 Aug 2015 23:59:18 +0200 Subject: [PATCH 22/57] use ClassName::class syntax --- tests/PhpGenerator/Helpers.format.phpt | 4 ++-- tests/PhpGenerator/PhpNamespace.phpt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/PhpGenerator/Helpers.format.phpt b/tests/PhpGenerator/Helpers.format.phpt index dff61eef..0b662e21 100644 --- a/tests/PhpGenerator/Helpers.format.phpt +++ b/tests/PhpGenerator/Helpers.format.phpt @@ -25,11 +25,11 @@ Assert::same( Assert::exception(function () { Helpers::formatArgs('func(?*)', [1, 2]); -}, 'Nette\InvalidArgumentException', 'Argument must be an array.'); +}, Nette\InvalidArgumentException::class, 'Argument must be an array.'); Assert::exception(function () { Helpers::formatArgs('func(?, ?, ?)', [1, 2]); -}, 'Nette\InvalidArgumentException', 'Insufficient number of arguments.'); +}, Nette\InvalidArgumentException::class, 'Insufficient number of arguments.'); Assert::same('$a = 2', Helpers::formatArgs('$? = ?', ['a', 2])); Assert::same('$obj->a = 2', Helpers::formatArgs('$obj->? = ?', ['a', 2])); diff --git a/tests/PhpGenerator/PhpNamespace.phpt b/tests/PhpGenerator/PhpNamespace.phpt index 03479bc6..20fb536c 100644 --- a/tests/PhpGenerator/PhpNamespace.phpt +++ b/tests/PhpGenerator/PhpNamespace.phpt @@ -31,7 +31,7 @@ Assert::same($namespace, $interfaceB->getNamespace()); Assert::exception(function () use ($namespace) { $traitC = $namespace->addTrait('C'); Assert::same($namespace, $traitC->getNamespace()); -}, 'Nette\InvalidStateException', "Alias 'C' used already for 'Bar\\C', cannot use for 'Foo\\C'."); +}, Nette\InvalidStateException::class, "Alias 'C' used already for 'Bar\\C', cannot use for 'Foo\\C'."); $classA ->addImplement('Foo\\A') From b14ce9e7c37bfcf9438a18d5f6a6c5c93549ba25 Mon Sep 17 00:00:00 2001 From: Michael Moravec Date: Mon, 24 Aug 2015 19:47:37 +0200 Subject: [PATCH 23/57] Remove PHP <=5.6 compatibility checks --- src/PhpGenerator/Parameter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpGenerator/Parameter.php b/src/PhpGenerator/Parameter.php index 479379b2..9173b201 100644 --- a/src/PhpGenerator/Parameter.php +++ b/src/PhpGenerator/Parameter.php @@ -55,7 +55,7 @@ public static function from(\ReflectionParameter $from) } } } - $param->optional = PHP_VERSION_ID < 50407 ? $from->isOptional() || ($param->typeHint && $from->allowsNull()) : $from->isDefaultValueAvailable(); + $param->optional = $from->isDefaultValueAvailable(); $param->defaultValue = $from->isDefaultValueAvailable() ? $from->getDefaultValue() : NULL; $namespace = $from->getDeclaringClass() ? $from->getDeclaringClass()->getNamespaceName() : NULL; From 5f5524d50da91083f378348a92338cf9575cb7c9 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 31 Aug 2015 20:16:39 +0200 Subject: [PATCH 24/57] PhpNamespace::unresolveName() supports for build-in types --- src/PhpGenerator/Method.php | 13 ++++++------- src/PhpGenerator/PhpNamespace.php | 3 +++ tests/PhpGenerator/PhpNamespace.phpt | 4 ++++ 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index 99283bb1..8fc33355 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -96,13 +96,12 @@ public static function from($from) */ public function __toString() { - static $builtinTypes = ['array', 'self', 'parent', 'callable', 'string', 'bool', 'float', 'int', NULL]; $parameters = []; foreach ($this->parameters as $param) { $variadic = $this->variadic && $param === end($this->parameters); - $hint = !$this->namespace || in_array($param->getTypeHint(), $builtinTypes, TRUE) - ? $param->getTypeHint() - : $this->namespace->unresolveName($param->getTypeHint()); + $hint = $this->namespace + ? $this->namespace->unresolveName((string) $param->getTypeHint()) + : $param->getTypeHint(); $parameters[] = ($hint ? $hint . ' ' : '') . ($param->isReference() ? '&' : '') @@ -114,9 +113,9 @@ public function __toString() foreach ($this->uses as $param) { $uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName(); } - $returnType = !$this->namespace || in_array($this->returnType, $builtinTypes, TRUE) - ? $this->returnType - : $this->namespace->unresolveName($this->returnType); + $returnType = $this->namespace + ? $this->namespace->unresolveName((string) $this->returnType) + : $this->returnType; return ($this->comment ? str_replace("\n", "\n * ", "/**\n" . $this->comment) . "\n */\n" : '') . ($this->abstract ? 'abstract ' : '') diff --git a/src/PhpGenerator/PhpNamespace.php b/src/PhpGenerator/PhpNamespace.php index 173c5554..308db324 100644 --- a/src/PhpGenerator/PhpNamespace.php +++ b/src/PhpGenerator/PhpNamespace.php @@ -134,6 +134,9 @@ public function addUse($name, $alias = NULL, &$aliasOut = NULL) */ public function unresolveName($name) { + if (in_array(strtolower($name), ['self', 'parent', 'array', 'callable', 'string', 'bool', 'float', 'int', ''], TRUE)) { + return $name; + } $name = ltrim($name, '\\'); $res = NULL; $lower = strtolower($name); diff --git a/tests/PhpGenerator/PhpNamespace.phpt b/tests/PhpGenerator/PhpNamespace.phpt index 20fb536c..9feff15c 100644 --- a/tests/PhpGenerator/PhpNamespace.phpt +++ b/tests/PhpGenerator/PhpNamespace.phpt @@ -21,6 +21,10 @@ Assert::same('\Bar', $namespace->unresolveName('Bar')); Assert::same('C', $namespace->unresolveName('bar\C')); Assert::same('C\D', $namespace->unresolveName('Bar\C\D')); +foreach (['String', 'string', 'int', 'float', 'bool', 'array', 'callable', 'self', 'parent', ''] as $type) { + Assert::same($type, $namespace->unresolveName($type)); +} + $classA = $namespace->addClass('A'); Assert::same($namespace, $classA->getNamespace()); From f759fba5a2515e10894c5572ba181a7cfd5912ca Mon Sep 17 00:00:00 2001 From: Jaroslav Hanslik Date: Wed, 26 Aug 2015 20:58:17 +0200 Subject: [PATCH 25/57] Minor fixes because of PHP7 strict mode --- src/PhpGenerator/ClassType.php | 2 +- src/PhpGenerator/Method.php | 2 +- src/PhpGenerator/Parameter.php | 2 +- src/PhpGenerator/PhpNamespace.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index 64c72adb..62deaa03 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -101,7 +101,7 @@ public static function from($from) public function __construct($name = NULL, PhpNamespace $namespace = NULL) { - $this->setName($name); + $this->setName((string) $name); $this->namespace = $namespace; } diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index 8fc33355..7f89fd16 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -285,7 +285,7 @@ public function setVisibility($val) /** - * @return string + * @return string|NULL */ public function getVisibility() { diff --git a/src/PhpGenerator/Parameter.php b/src/PhpGenerator/Parameter.php index 9173b201..076ad77e 100644 --- a/src/PhpGenerator/Parameter.php +++ b/src/PhpGenerator/Parameter.php @@ -60,7 +60,7 @@ public static function from(\ReflectionParameter $from) $namespace = $from->getDeclaringClass() ? $from->getDeclaringClass()->getNamespaceName() : NULL; $namespace = $namespace ? "\\$namespace\\" : '\\'; - if (Nette\Utils\Strings::startsWith($param->typeHint, $namespace)) { + if ($param->typeHint !== NULL && Nette\Utils\Strings::startsWith($param->typeHint, $namespace)) { $param->typeHint = substr($param->typeHint, strlen($namespace)); } return $param; diff --git a/src/PhpGenerator/PhpNamespace.php b/src/PhpGenerator/PhpNamespace.php index 308db324..589f2311 100644 --- a/src/PhpGenerator/PhpNamespace.php +++ b/src/PhpGenerator/PhpNamespace.php @@ -37,7 +37,7 @@ class PhpNamespace extends Object public function __construct($name = NULL) { - $this->setName($name); + $this->setName((string) $name); } From 2f15be43d3af08f4b93c88900b92f969bb871761 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 31 Aug 2015 22:22:17 +0200 Subject: [PATCH 26/57] Method, Parameter, Property: added and used constructors --- src/PhpGenerator/ClassType.php | 7 +++---- src/PhpGenerator/Method.php | 19 +++++++++++++------ src/PhpGenerator/Parameter.php | 12 ++++++++++-- src/PhpGenerator/PhpFile.php | 2 -- src/PhpGenerator/Property.php | 12 ++++++++++-- 5 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index 62deaa03..eb8f39d7 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -473,8 +473,7 @@ public function getProperty($name) */ public function addProperty($name, $value = NULL) { - $property = new Property; - return $this->properties[$name] = $property->setName($name)->setValue($value); + return $this->properties[$name] = (new Property($name))->setValue($value); } @@ -521,13 +520,13 @@ public function getMethod($name) */ public function addMethod($name) { - $method = new Method; + $method = new Method($name); if ($this->type === 'interface') { $method->setVisibility(NULL)->setBody(FALSE); } else { $method->setVisibility('public'); } - return $this->methods[$name] = $method->setName($name); + return $this->methods[$name] = $method; } } diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index 7f89fd16..c2883857 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -68,8 +68,7 @@ public static function from($from) $from = new \ReflectionFunction($from); } - $method = new static; - $method->name = $from->isClosure() ? NULL : $from->getName(); + $method = new static($from->isClosure() ? NULL : $from->getName()); foreach ($from->getParameters() as $param) { $method->parameters[$param->getName()] = Parameter::from($param); } @@ -91,6 +90,15 @@ public static function from($from) } + /** + * @param string|NULL + */ + public function __construct($name = NULL) + { + $this->setName($name); + } + + /** * @return string PHP code */ @@ -184,11 +192,11 @@ public function getParameters() */ public function addParameter($name, $defaultValue = NULL) { - $param = new Parameter; + $param = new Parameter($name); if (func_num_args() > 1) { $param->setOptional(TRUE)->setDefaultValue($defaultValue); } - return $this->parameters[$name] = $param->setName($name); + return $this->parameters[$name] = $param; } @@ -216,8 +224,7 @@ public function getUses() */ public function addUse($name) { - $param = new Parameter; - return $this->uses[] = $param->setName($name); + return $this->uses[] = new Parameter($name); } diff --git a/src/PhpGenerator/Parameter.php b/src/PhpGenerator/Parameter.php index 076ad77e..0c998bc7 100644 --- a/src/PhpGenerator/Parameter.php +++ b/src/PhpGenerator/Parameter.php @@ -36,8 +36,7 @@ class Parameter extends Nette\Object */ public static function from(\ReflectionParameter $from) { - $param = new static; - $param->name = $from->getName(); + $param = new static($from->getName()); $param->reference = $from->isPassedByReference(); if (PHP_VERSION_ID >= 70000) { $type = $from->getType(); @@ -67,6 +66,15 @@ public static function from(\ReflectionParameter $from) } + /** + * @param string without $ + */ + public function __construct($name = '') + { + $this->setName($name); + } + + /** * @param string without $ * @return self diff --git a/src/PhpGenerator/PhpFile.php b/src/PhpGenerator/PhpFile.php index d0bfd8f1..4149823f 100644 --- a/src/PhpGenerator/PhpFile.php +++ b/src/PhpGenerator/PhpFile.php @@ -28,8 +28,6 @@ class PhpFile extends Object private $namespaces = []; - - /** * @param string|NULL * @return self diff --git a/src/PhpGenerator/Property.php b/src/PhpGenerator/Property.php index 5bd427a7..049cb105 100644 --- a/src/PhpGenerator/Property.php +++ b/src/PhpGenerator/Property.php @@ -36,8 +36,7 @@ class Property extends Nette\Object */ public static function from(\ReflectionProperty $from) { - $prop = new static; - $prop->name = $from->getName(); + $prop = new static($from->getName()); $defaults = $from->getDeclaringClass()->getDefaultProperties(); $prop->value = isset($defaults[$prop->name]) ? $defaults[$prop->name] : NULL; $prop->static = $from->isStatic(); @@ -47,6 +46,15 @@ public static function from(\ReflectionProperty $from) } + /** + * @param string without $ + */ + public function __construct($name = '') + { + $this->setName($name); + } + + /** * @param string without $ * @return self From 5dddff990e9f18de3bd5f81e54c986f91146ccfb Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 1 Sep 2015 00:11:23 +0200 Subject: [PATCH 27/57] PhpNamespace: changed order of methods --- src/PhpGenerator/PhpNamespace.php | 48 +++++++++++++++---------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/PhpGenerator/PhpNamespace.php b/src/PhpGenerator/PhpNamespace.php index 589f2311..71a4e621 100644 --- a/src/PhpGenerator/PhpNamespace.php +++ b/src/PhpGenerator/PhpNamespace.php @@ -41,15 +41,6 @@ public function __construct($name = NULL) } - /** - * @return string - */ - public function getName() - { - return $this->name; - } - - /** * @param string * @return self @@ -62,11 +53,11 @@ public function setName($name) /** - * @return bool + * @return string */ - public function getBracketedSyntax() + public function getName() { - return $this->bracketedSyntax; + return $this->name; } @@ -83,11 +74,11 @@ public function setBracketedSyntax($state = TRUE) /** - * @return string[] + * @return bool */ - public function getUses() + public function getBracketedSyntax() { - return $this->uses; + return $this->bracketedSyntax; } @@ -128,6 +119,15 @@ public function addUse($name, $alias = NULL, &$aliasOut = NULL) } + /** + * @return string[] + */ + public function getUses() + { + return $this->uses; + } + + /** * @param string * @return string @@ -157,15 +157,6 @@ public function unresolveName($name) } - /** - * @return ClassType[] - */ - public function getClasses() - { - return $this->classes; - } - - /** * @param string * @return ClassType @@ -200,6 +191,15 @@ public function addTrait($name) } + /** + * @return ClassType[] + */ + public function getClasses() + { + return $this->classes; + } + + /** * @return string PHP code */ From 83f7679d370866721524de2eef09177430c298d8 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 1 Sep 2015 00:12:43 +0200 Subject: [PATCH 28/57] PhpNamespace: name can be NULL --- src/PhpGenerator/PhpNamespace.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/PhpGenerator/PhpNamespace.php b/src/PhpGenerator/PhpNamespace.php index 71a4e621..0da60bd5 100644 --- a/src/PhpGenerator/PhpNamespace.php +++ b/src/PhpGenerator/PhpNamespace.php @@ -37,12 +37,12 @@ class PhpNamespace extends Object public function __construct($name = NULL) { - $this->setName((string) $name); + $this->setName($name); } /** - * @param string + * @param string|NULL * @return self */ public function setName($name) @@ -53,11 +53,11 @@ public function setName($name) /** - * @return string + * @return string|NULL */ public function getName() { - return $this->name; + return $this->name ?: NULL; } From 6e1b2935dae0b505aa56d1c31b725f0f69e42d83 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 1 Sep 2015 00:53:28 +0200 Subject: [PATCH 29/57] PhpNamespace::unresolveName() removed leading \ for empty namespace --- src/PhpGenerator/PhpNamespace.php | 2 +- tests/PhpGenerator/PhpNamespace.phpt | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/PhpGenerator/PhpNamespace.php b/src/PhpGenerator/PhpNamespace.php index 0da60bd5..847e8bf5 100644 --- a/src/PhpGenerator/PhpNamespace.php +++ b/src/PhpGenerator/PhpNamespace.php @@ -152,7 +152,7 @@ public function unresolveName($name) if (!$res && Strings::startsWith($lower, strtolower($this->name) . '\\')) { return substr($name, strlen($this->name) + 1); } else { - return $res ?: '\\' . $name; + return $res ?: ($this->name ? '\\' : '') . $name; } } diff --git a/tests/PhpGenerator/PhpNamespace.phpt b/tests/PhpGenerator/PhpNamespace.phpt index 9feff15c..34c2e9ed 100644 --- a/tests/PhpGenerator/PhpNamespace.phpt +++ b/tests/PhpGenerator/PhpNamespace.phpt @@ -10,6 +10,22 @@ use Tester\Assert; require __DIR__ . '/../bootstrap.php'; +$namespace = new PhpNamespace; + +Assert::same('A', $namespace->unresolveName('A')); +Assert::same('foo\A', $namespace->unresolveName('foo\A')); + +$namespace->addUse('Bar\C'); + +Assert::same('Bar', $namespace->unresolveName('Bar')); +Assert::same('C', $namespace->unresolveName('bar\C')); +Assert::same('C\D', $namespace->unresolveName('Bar\C\D')); + +foreach (['String', 'string', 'int', 'float', 'bool', 'array', 'callable', 'self', 'parent', ''] as $type) { + Assert::same($type, $namespace->unresolveName($type)); +} + + $namespace = new PhpNamespace('Foo'); Assert::same('\A', $namespace->unresolveName('A')); From 2c08cae350f254036562a0f19e825667af2979fa Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 31 Aug 2015 22:22:17 +0200 Subject: [PATCH 30/57] setName() is deprecated in favor of constructors --- src/PhpGenerator/ClassType.php | 11 ++++------- src/PhpGenerator/Method.php | 5 +---- src/PhpGenerator/Parameter.php | 5 +---- src/PhpGenerator/PhpNamespace.php | 5 +---- src/PhpGenerator/Property.php | 5 +---- tests/PhpGenerator/Method.returnTypes.phpt | 3 +-- tests/PhpGenerator/Method.scalarParameters.phpt | 3 +-- tests/PhpGenerator/Method.variadics.phpt | 15 +++++---------- 8 files changed, 15 insertions(+), 37 deletions(-) diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index eb8f39d7..bc4e632b 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -99,9 +99,9 @@ public static function from($from) } - public function __construct($name = NULL, PhpNamespace $namespace = NULL) + public function __construct($name = '', PhpNamespace $namespace = NULL) { - $this->setName((string) $name); + $this->setName($name); $this->namespace = $namespace; } @@ -158,7 +158,7 @@ public function __toString() /** - * @return PhpNamespace + * @return PhpNamespace|NULL */ public function getNamespace() { @@ -166,10 +166,7 @@ public function getNamespace() } - /** - * @param string - * @return self - */ + /** @deprecated */ public function setName($name) { $this->name = (string) $name; diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index c2883857..faae9c97 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -141,10 +141,7 @@ public function __toString() } - /** - * @param string|NULL - * @return self - */ + /** @deprecated */ public function setName($name) { $this->name = $name ? (string) $name : NULL; diff --git a/src/PhpGenerator/Parameter.php b/src/PhpGenerator/Parameter.php index 0c998bc7..4a440bd5 100644 --- a/src/PhpGenerator/Parameter.php +++ b/src/PhpGenerator/Parameter.php @@ -75,10 +75,7 @@ public function __construct($name = '') } - /** - * @param string without $ - * @return self - */ + /** @deprecated */ public function setName($name) { $this->name = (string) $name; diff --git a/src/PhpGenerator/PhpNamespace.php b/src/PhpGenerator/PhpNamespace.php index 847e8bf5..c2fa8d15 100644 --- a/src/PhpGenerator/PhpNamespace.php +++ b/src/PhpGenerator/PhpNamespace.php @@ -41,10 +41,7 @@ public function __construct($name = NULL) } - /** - * @param string|NULL - * @return self - */ + /** @deprecated */ public function setName($name) { $this->name = (string) $name; diff --git a/src/PhpGenerator/Property.php b/src/PhpGenerator/Property.php index 049cb105..4fc8b563 100644 --- a/src/PhpGenerator/Property.php +++ b/src/PhpGenerator/Property.php @@ -55,10 +55,7 @@ public function __construct($name = '') } - /** - * @param string without $ - * @return self - */ + /** @deprecated */ public function setName($name) { $this->name = (string) $name; diff --git a/tests/PhpGenerator/Method.returnTypes.phpt b/tests/PhpGenerator/Method.returnTypes.phpt index 22842263..018db71e 100644 --- a/tests/PhpGenerator/Method.returnTypes.phpt +++ b/tests/PhpGenerator/Method.returnTypes.phpt @@ -34,8 +34,7 @@ namespace // generating methods with return type declarations - $method = (new Method) - ->setName('create') + $method = (new Method('create')) ->setReturnType('Foo') ->setBody('return new Foo();'); diff --git a/tests/PhpGenerator/Method.scalarParameters.phpt b/tests/PhpGenerator/Method.scalarParameters.phpt index e44e3088..8093c458 100644 --- a/tests/PhpGenerator/Method.scalarParameters.phpt +++ b/tests/PhpGenerator/Method.scalarParameters.phpt @@ -35,8 +35,7 @@ Assert::same('float', $method->getParameters()['d']->getTypeHint()); // generating methods with scalar type hints -$method = (new Method) - ->setName('create') +$method = (new Method('create')) ->setBody('return null;'); $method->addParameter('a')->setTypeHint('string'); $method->addParameter('b')->setTypeHint('bool'); diff --git a/tests/PhpGenerator/Method.variadics.phpt b/tests/PhpGenerator/Method.variadics.phpt index 2d6450c7..21dcbb15 100644 --- a/tests/PhpGenerator/Method.variadics.phpt +++ b/tests/PhpGenerator/Method.variadics.phpt @@ -34,8 +34,7 @@ Assert::same('array', $method->getParameters()['bar']->getTypeHint()); // test generating // parameterless variadic method -$method = (new Method) - ->setName('variadic') +$method = (new Method('variadic')) ->setVariadic(TRUE) ->setBody('return 42;'); @@ -48,8 +47,7 @@ Assert::match( // variadic method with one parameter -$method = (new Method) - ->setName('variadic') +$method = (new Method('variadic')) ->setVariadic(TRUE) ->setBody('return 42;'); $method->addParameter('foo'); @@ -63,8 +61,7 @@ Assert::match( // variadic method with multiple parameters -$method = (new Method) - ->setName('variadic') +$method = (new Method('variadic')) ->setVariadic(TRUE) ->setBody('return 42;'); $method->addParameter('foo'); @@ -80,8 +77,7 @@ Assert::match( // method with typehinted variadic param -$method = (new Method) - ->setName('variadic') +$method = (new Method('variadic')) ->setVariadic(TRUE) ->setBody('return 42;'); $method->addParameter('foo')->setTypeHint('array'); @@ -95,8 +91,7 @@ Assert::match( // method with typrhinted by-value variadic param -$method = (new Method) - ->setName('variadic') +$method = (new Method('variadic')) ->setVariadic(TRUE) ->setBody('return 42;'); $method->addParameter('foo')->setTypeHint('array')->setReference(TRUE); From 2f64aa306d2005d672c650a36534938640b97a18 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 1 Sep 2015 00:33:37 +0200 Subject: [PATCH 31/57] Method::setParameters() & ClassType::setProperties() & setMethods() retain names in keys --- src/PhpGenerator/ClassType.php | 6 ++++-- src/PhpGenerator/Method.php | 3 ++- tests/PhpGenerator/ClassType.phpt | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index bc4e632b..28b989dd 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -432,12 +432,13 @@ public function addConst($name, $value) */ public function setProperties(array $props) { + $this->properties = []; foreach ($props as $v) { if (!$v instanceof Property) { throw new Nette\InvalidArgumentException('Argument must be Nette\PhpGenerator\Property[].'); } + $this->properties[$v->getName()] = $v; } - $this->properties = $props; return $this; } @@ -480,12 +481,13 @@ public function addProperty($name, $value = NULL) */ public function setMethods(array $methods) { + $this->methods = []; foreach ($methods as $v) { if (!$v instanceof Method) { throw new Nette\InvalidArgumentException('Argument must be Nette\PhpGenerator\Method[].'); } + $this->methods[$v->getName()] = $v; } - $this->methods = $methods; return $this; } diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index faae9c97..d3254972 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -164,12 +164,13 @@ public function getName() */ public function setParameters(array $val) { + $this->parameters = []; foreach ($val as $v) { if (!$v instanceof Parameter) { throw new Nette\InvalidArgumentException('Argument must be Nette\PhpGenerator\Parameter[].'); } + $this->parameters[$v->getName()] = $v; } - $this->parameters = $val; return $this; } diff --git a/tests/PhpGenerator/ClassType.phpt b/tests/PhpGenerator/ClassType.phpt index 6ac11b5e..13a7f5ff 100644 --- a/tests/PhpGenerator/ClassType.phpt +++ b/tests/PhpGenerator/ClassType.phpt @@ -65,3 +65,20 @@ $method->addParameter('res', NULL) ->setTypeHint('array'); Assert::matchFile(__DIR__ . '/ClassType.expect', (string) $class); + + +// global setters & getters +$methods = $class->getMethods(); +Assert::count(3, $methods); +$class->setMethods(array_values($methods)); +Assert::same($methods, $class->getMethods()); + +$properties = $class->getProperties(); +Assert::count(3, $properties); +$class->setProperties(array_values($properties)); +Assert::same($properties, $class->getProperties()); + +$parameters = $method->getParameters(); +Assert::count(2, $parameters); +$method->setParameters(array_values($parameters)); +Assert::same($parameters, $method->getParameters()); From c00b49b3ca0939eabf0eaebda3b47a49ef60aea3 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 1 Sep 2015 00:52:30 +0200 Subject: [PATCH 32/57] Factory methods from() are using PhpNamespace --- src/PhpGenerator/ClassType.php | 11 +---------- src/PhpGenerator/Method.php | 3 +-- src/PhpGenerator/Parameter.php | 13 +++---------- tests/PhpGenerator/Method.returnTypes.phpt | 6 +++--- 4 files changed, 8 insertions(+), 25 deletions(-) diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index 28b989dd..0dfb7150 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -66,25 +66,16 @@ class ClassType extends Nette\Object public static function from($from) { $from = $from instanceof \ReflectionClass ? $from : new \ReflectionClass($from); - $class = new static($from->getShortName()); + $class = new static($from->getShortName(), new PhpNamespace($from->getNamespaceName())); $class->type = $from->isInterface() ? 'interface' : ($from->isTrait() ? 'trait' : 'class'); $class->final = $from->isFinal() && $class->type === 'class'; $class->abstract = $from->isAbstract() && $class->type === 'class'; $class->implements = $from->getInterfaceNames(); $class->comment = $from->getDocComment() ? preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")) : NULL; - $namespace = $from->getNamespaceName(); if ($from->getParentClass()) { $class->extends = $from->getParentClass()->getName(); - if ($namespace) { - $class->extends = Strings::startsWith($class->extends, "$namespace\\") ? substr($class->extends, strlen($namespace) + 1) : '\\' . $class->extends; - } $class->implements = array_diff($class->implements, $from->getParentClass()->getInterfaceNames()); } - if ($namespace) { - foreach ($class->implements as & $interface) { - $interface = Strings::startsWith($interface, "$namespace\\") ? substr($interface, strlen($namespace) + 1) : '\\' . $interface; - } - } foreach ($from->getProperties() as $prop) { if ($prop->getDeclaringClass() == $from) { // intentionally == $class->properties[$prop->getName()] = Property::from($prop); diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index d3254972..d9cd2281 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -83,8 +83,7 @@ public static function from($from) $method->variadic = PHP_VERSION_ID >= 50600 && $from->isVariadic(); $method->comment = $from->getDocComment() ? preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")) : NULL; if (PHP_VERSION_ID >= 70000 && $from->hasReturnType()) { - $returnType = $from->getReturnType(); - $method->returnType = $returnType->isBuiltin() ? (string) $returnType : '\\' . $returnType; + $method->returnType = (string) $from->getReturnType(); } return $method; } diff --git a/src/PhpGenerator/Parameter.php b/src/PhpGenerator/Parameter.php index 4a440bd5..827ac6fb 100644 --- a/src/PhpGenerator/Parameter.php +++ b/src/PhpGenerator/Parameter.php @@ -39,16 +39,15 @@ public static function from(\ReflectionParameter $from) $param = new static($from->getName()); $param->reference = $from->isPassedByReference(); if (PHP_VERSION_ID >= 70000) { - $type = $from->getType(); - $param->typeHint = $type ? ($type->isBuiltin() ? '' : '\\') . $type : NULL; + $param->typeHint = $from->hasType() ? (string) $from->getType() : NULL; } elseif ($from->isArray() || $from->isCallable()) { $param->typeHint = $from->isArray() ? 'array' : 'callable'; } else { try { - $param->typeHint = $from->getClass() ? '\\' . $from->getClass()->getName() : NULL; + $param->typeHint = $from->getClass() ? $from->getClass()->getName() : NULL; } catch (\ReflectionException $e) { if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) { - $param->typeHint = '\\' . $m[1]; + $param->typeHint = $m[1]; } else { throw $e; } @@ -56,12 +55,6 @@ public static function from(\ReflectionParameter $from) } $param->optional = $from->isDefaultValueAvailable(); $param->defaultValue = $from->isDefaultValueAvailable() ? $from->getDefaultValue() : NULL; - - $namespace = $from->getDeclaringClass() ? $from->getDeclaringClass()->getNamespaceName() : NULL; - $namespace = $namespace ? "\\$namespace\\" : '\\'; - if ($param->typeHint !== NULL && Nette\Utils\Strings::startsWith($param->typeHint, $namespace)) { - $param->typeHint = substr($param->typeHint, strlen($namespace)); - } return $param; } diff --git a/tests/PhpGenerator/Method.returnTypes.phpt b/tests/PhpGenerator/Method.returnTypes.phpt index 018db71e..0b31194f 100644 --- a/tests/PhpGenerator/Method.returnTypes.phpt +++ b/tests/PhpGenerator/Method.returnTypes.phpt @@ -22,12 +22,12 @@ namespace interface A { - function testClass() : \A\Foo; - function testScalar() : string; + function testClass(): \A\Foo; + function testScalar(): string; } $method = Method::from(A::class .'::testClass'); - Assert::same('\A\Foo', $method->getReturnType()); + Assert::same('A\Foo', $method->getReturnType()); $method = Method::from(A::class .'::testScalar'); Assert::same('string', $method->getReturnType()); From 77c0c9d0f208bb1f9ed635216c4021c8534181b2 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 1 Sep 2015 15:31:05 +0200 Subject: [PATCH 33/57] ClassType: prints one 'use' keyword per trait --- src/PhpGenerator/ClassType.php | 2 +- tests/PhpGenerator/PhpFile.bracketed.expect | 3 ++- tests/PhpGenerator/PhpFile.regular.expect | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index 0dfb7150..7e54ec5e 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -139,7 +139,7 @@ public function __toString() . ($implements ? 'implements ' . implode(', ', $implements) . ' ' : '') . "\n{\n" . Strings::indent( - ($traits ? 'use ' . implode(', ', $traits) . ";\n\n" : '') + ($traits ? 'use ' . implode(";\nuse ", $traits) . ";\n\n" : '') . ($this->consts ? implode('', $consts) . "\n" : '') . ($this->properties ? implode("\n", $properties) . "\n" : '') . ($this->methods ? "\n" . implode("\n\n\n", $this->methods) . "\n\n" : ''), 1) diff --git a/tests/PhpGenerator/PhpFile.bracketed.expect b/tests/PhpGenerator/PhpFile.bracketed.expect index bab4953b..02eb1b30 100644 --- a/tests/PhpGenerator/PhpFile.bracketed.expect +++ b/tests/PhpGenerator/PhpFile.bracketed.expect @@ -9,7 +9,8 @@ namespace Foo { class A implements A, \Bar\C { - use C, \Bar\D; + use C; + use \Bar\D; } diff --git a/tests/PhpGenerator/PhpFile.regular.expect b/tests/PhpGenerator/PhpFile.regular.expect index 07476b02..f8e75ed9 100644 --- a/tests/PhpGenerator/PhpFile.regular.expect +++ b/tests/PhpGenerator/PhpFile.regular.expect @@ -9,7 +9,8 @@ namespace Foo; class A implements A, \Bar\C { - use C, \Bar\D; + use C; + use \Bar\D; } From 42e818c25d6231bcbe4d124464469ee9b03e1114 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 1 Sep 2015 10:20:33 +0200 Subject: [PATCH 34/57] refactoring --- src/PhpGenerator/ClassType.php | 17 ++++++----------- src/PhpGenerator/Method.php | 11 +++-------- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index 7e54ec5e..dc32bd22 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -116,14 +116,9 @@ public function __toString() . ";\n"; } - $extends = (array) $this->extends; - $implements = $this->implements; - $traits = $this->traits; - if ($this->namespace) { - $extends = array_map([$this->namespace, 'unresolveName'], $extends); - $implements = array_map([$this->namespace, 'unresolveName'], $implements); - $traits = array_map([$this->namespace, 'unresolveName'], $traits); - } + $mapper = function (array $arr) { + return array_map([$this->namespace ?: new PhpNamespace, 'unresolveName'], $arr); + }; foreach ($this->methods as $method) { $method->setNamespace($this->namespace); @@ -135,11 +130,11 @@ public function __toString() . ($this->final ? 'final ' : '') . $this->type . ' ' . $this->name . ' ' - . ($extends ? 'extends ' . implode(', ', $extends) . ' ' : '') - . ($implements ? 'implements ' . implode(', ', $implements) . ' ' : '') + . ($this->extends ? 'extends ' . implode(', ', $mapper((array) $this->extends)) . ' ' : '') + . ($this->implements ? 'implements ' . implode(', ', $mapper($this->implements)) . ' ' : '') . "\n{\n" . Strings::indent( - ($traits ? 'use ' . implode(";\nuse ", $traits) . ";\n\n" : '') + ($this->traits ? 'use ' . implode(";\nuse ", $mapper($this->traits)) . ";\n\n" : '') . ($this->consts ? implode('', $consts) . "\n" : '') . ($this->properties ? implode("\n", $properties) . "\n" : '') . ($this->methods ? "\n" . implode("\n\n\n", $this->methods) . "\n\n" : ''), 1) diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index d9cd2281..d3253e5f 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -103,14 +103,12 @@ public function __construct($name = NULL) */ public function __toString() { + $namespace = $this->namespace ?: new PhpNamespace; $parameters = []; foreach ($this->parameters as $param) { $variadic = $this->variadic && $param === end($this->parameters); - $hint = $this->namespace - ? $this->namespace->unresolveName((string) $param->getTypeHint()) - : $param->getTypeHint(); - $parameters[] = ($hint ? $hint . ' ' : '') + $parameters[] = ($param->getTypeHint() ? $namespace->unresolveName($param->getTypeHint()) . ' ' : '') . ($param->isReference() ? '&' : '') . ($variadic ? '...' : '') . '$' . $param->getName() @@ -120,9 +118,6 @@ public function __toString() foreach ($this->uses as $param) { $uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName(); } - $returnType = $this->namespace - ? $this->namespace->unresolveName((string) $this->returnType) - : $this->returnType; return ($this->comment ? str_replace("\n", "\n * ", "/**\n" . $this->comment) . "\n */\n" : '') . ($this->abstract ? 'abstract ' : '') @@ -134,7 +129,7 @@ public function __toString() . ' ' . $this->name . '(' . implode(', ', $parameters) . ')' . ($this->uses ? ' use (' . implode(', ', $uses) . ')' : '') - . ($returnType ? ': ' . $returnType : '') + . ($this->returnType ? ': ' . $namespace->unresolveName($this->returnType) : '') . ($this->abstract || $this->body === FALSE ? ';' : ($this->name ? "\n" : ' ') . "{\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) . '}'); } From ed06e308076be36e56f20e312c0b67bc4a336674 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 1 Sep 2015 00:24:04 +0200 Subject: [PATCH 35/57] ClassType: namespace is passed to methods immediately --- src/PhpGenerator/ClassType.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index dc32bd22..5aa3be68 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -83,7 +83,7 @@ public static function from($from) } foreach ($from->getMethods() as $method) { if ($method->getDeclaringClass() == $from) { // intentionally == - $class->methods[$method->getName()] = Method::from($method); + $class->methods[$method->getName()] = Method::from($method)->setNamespace($class->namespace); } } return $class; @@ -120,10 +120,6 @@ public function __toString() return array_map([$this->namespace ?: new PhpNamespace, 'unresolveName'], $arr); }; - foreach ($this->methods as $method) { - $method->setNamespace($this->namespace); - } - return Strings::normalize( ($this->comment ? str_replace("\n", "\n * ", "/**\n" . $this->comment) . "\n */\n" : '') . ($this->abstract ? 'abstract ' : '') @@ -472,7 +468,7 @@ public function setMethods(array $methods) if (!$v instanceof Method) { throw new Nette\InvalidArgumentException('Argument must be Nette\PhpGenerator\Method[].'); } - $this->methods[$v->getName()] = $v; + $this->methods[$v->getName()] = $v->setNamespace($this->namespace); } return $this; } @@ -505,7 +501,7 @@ public function getMethod($name) */ public function addMethod($name) { - $method = new Method($name); + $method = (new Method($name))->setNamespace($this->namespace); if ($this->type === 'interface') { $method->setVisibility(NULL)->setBody(FALSE); } else { From cd522690387e0e2c5f8544cfa58e58e3f47256cb Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 7 Sep 2015 12:59:59 +0200 Subject: [PATCH 36/57] typos --- src/PhpGenerator/Method.php | 3 +-- src/PhpGenerator/Property.php | 2 -- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index d3253e5f..02b5fdc1 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -372,8 +372,6 @@ public function isVariadic() } - - /** * @param string|NULL * @return self @@ -435,6 +433,7 @@ public function setNamespace(PhpNamespace $val = NULL) return $this; } + /** * @param string|NULL * @return self diff --git a/src/PhpGenerator/Property.php b/src/PhpGenerator/Property.php index 4fc8b563..626e6aac 100644 --- a/src/PhpGenerator/Property.php +++ b/src/PhpGenerator/Property.php @@ -134,8 +134,6 @@ public function getVisibility() } - - /** * @param string|NULL * @return self From d7a37f1a76537bb586c4096f7e013d7963abb4e0 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 21 Sep 2015 19:16:12 +0200 Subject: [PATCH 37/57] PHP 5.6 syntax --- src/PhpGenerator/Helpers.php | 5 ++--- src/PhpGenerator/Method.php | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/PhpGenerator/Helpers.php b/src/PhpGenerator/Helpers.php index 3e69f037..4b36fe34 100644 --- a/src/PhpGenerator/Helpers.php +++ b/src/PhpGenerator/Helpers.php @@ -135,10 +135,9 @@ private static function _dump(& $var, $level = 0) * Generates PHP statement. * @return string */ - public static function format($statement) + public static function format($statement, ...$args) { - $args = func_get_args(); - return self::formatArgs(array_shift($args), $args); + return self::formatArgs($statement, $args); } diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index 02b5fdc1..ee48cd3c 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -80,7 +80,7 @@ public static function from($from) $method->body = $from->isAbstract() ? FALSE : ''; } $method->returnReference = $from->returnsReference(); - $method->variadic = PHP_VERSION_ID >= 50600 && $from->isVariadic(); + $method->variadic = $from->isVariadic(); $method->comment = $from->getDocComment() ? preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")) : NULL; if (PHP_VERSION_ID >= 70000 && $from->hasReturnType()) { $method->returnType = (string) $from->getReturnType(); From e76d8d1fa7c2c30e96cad93ea0cdbcaa1769b4a3 Mon Sep 17 00:00:00 2001 From: castamir Date: Thu, 24 Sep 2015 09:32:58 +0200 Subject: [PATCH 38/57] PhpFile::__toString(): brackets are no longer set when generating classes from global namespace only --- src/PhpGenerator/PhpFile.php | 2 +- tests/PhpGenerator/PhpFile.globalNamespace.expect | 4 ++++ tests/PhpGenerator/PhpFile.phpt | 5 +++++ 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 tests/PhpGenerator/PhpFile.globalNamespace.expect diff --git a/src/PhpGenerator/PhpFile.php b/src/PhpGenerator/PhpFile.php index 4149823f..a3f8cdba 100644 --- a/src/PhpGenerator/PhpFile.php +++ b/src/PhpGenerator/PhpFile.php @@ -135,7 +135,7 @@ public function addNamespace($name) public function __toString() { foreach ($this->namespaces as $namespace) { - $namespace->setBracketedSyntax(isset($this->namespaces[NULL])); + $namespace->setBracketedSyntax(count($this->namespaces) > 1 && isset($this->namespaces[NULL])); } return Strings::normalize( diff --git a/tests/PhpGenerator/PhpFile.globalNamespace.expect b/tests/PhpGenerator/PhpFile.globalNamespace.expect new file mode 100644 index 00000000..4c39eada --- /dev/null +++ b/tests/PhpGenerator/PhpFile.globalNamespace.expect @@ -0,0 +1,4 @@ +addClass('H'); $file->addClass('FooBar\\I'); Assert::matchFile(__DIR__ . '/PhpFile.bracketed.expect', (string) $file); + +$file = new PhpFile; +$file->addClass('A'); + +Assert::matchFile(__DIR__ . '/PhpFile.globalNamespace.expect', (string) $file); From c22b521b2e333c3f134ed646848e9f4a7ccf1014 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 1 Oct 2015 17:48:26 +0200 Subject: [PATCH 39/57] Helpers::dump() throws exception for closures --- src/PhpGenerator/Helpers.php | 3 +++ tests/PhpGenerator/Helpers.dump().phpt | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/src/PhpGenerator/Helpers.php b/src/PhpGenerator/Helpers.php index 4b36fe34..b3ac2e73 100644 --- a/src/PhpGenerator/Helpers.php +++ b/src/PhpGenerator/Helpers.php @@ -93,6 +93,9 @@ private static function _dump(& $var, $level = 0) $var = serialize($var); return 'unserialize(' . self::_dump($var, $level) . ')'; + } elseif ($var instanceof \Closure) { + throw new Nette\InvalidArgumentException('Cannot dump closure.'); + } elseif (is_object($var)) { $arr = (array) $var; $space = str_repeat("\t", $level); diff --git a/tests/PhpGenerator/Helpers.dump().phpt b/tests/PhpGenerator/Helpers.dump().phpt index 29c2042f..bdab67d1 100644 --- a/tests/PhpGenerator/Helpers.dump().phpt +++ b/tests/PhpGenerator/Helpers.dump().phpt @@ -83,3 +83,7 @@ class Test3 implements Serializable Assert::same('unserialize(\'C:5:"Test3":0:{}\')', Helpers::dump(new Test3)); Assert::equal(new Test3, eval('return ' . Helpers::dump(new Test3) . ';')); + +Assert::exception(function () { + Helpers::dump(function () {}); +}, Nette\InvalidArgumentException::class, 'Cannot dump closure.'); From cfd9fba8c3ee53a8a9b160c6507c428f1e3d09d4 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 1 Oct 2015 17:47:56 +0200 Subject: [PATCH 40/57] added support for anonymous classes --- src/PhpGenerator/ClassType.php | 6 +++- src/PhpGenerator/Helpers.php | 3 ++ tests/PhpGenerator/ClassType.from.php7.expect | 26 +++++++++++++++ tests/PhpGenerator/ClassType.from.php7.phpt | 32 +++++++++++++++++++ tests/PhpGenerator/Helpers.dump().php7.phpt | 17 ++++++++++ 5 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 tests/PhpGenerator/ClassType.from.php7.expect create mode 100644 tests/PhpGenerator/ClassType.from.php7.phpt create mode 100644 tests/PhpGenerator/Helpers.dump().php7.phpt diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index 5aa3be68..501229db 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -66,7 +66,11 @@ class ClassType extends Nette\Object public static function from($from) { $from = $from instanceof \ReflectionClass ? $from : new \ReflectionClass($from); - $class = new static($from->getShortName(), new PhpNamespace($from->getNamespaceName())); + if (PHP_VERSION_ID >= 70000 && $from->isAnonymous()) { + $class = new static('anonymous'); + } else { + $class = new static($from->getShortName(), new PhpNamespace($from->getNamespaceName())); + } $class->type = $from->isInterface() ? 'interface' : ($from->isTrait() ? 'trait' : 'class'); $class->final = $from->isFinal() && $class->type === 'class'; $class->abstract = $from->isAbstract() && $class->type === 'class'; diff --git a/src/PhpGenerator/Helpers.php b/src/PhpGenerator/Helpers.php index b3ac2e73..6fc35022 100644 --- a/src/PhpGenerator/Helpers.php +++ b/src/PhpGenerator/Helpers.php @@ -97,6 +97,9 @@ private static function _dump(& $var, $level = 0) throw new Nette\InvalidArgumentException('Cannot dump closure.'); } elseif (is_object($var)) { + if (PHP_VERSION_ID >= 70000 && (new \ReflectionObject($var))->isAnonymous()) { + throw new Nette\InvalidArgumentException('Cannot dump anonymous class.'); + } $arr = (array) $var; $space = str_repeat("\t", $level); $class = get_class($var); diff --git a/tests/PhpGenerator/ClassType.from.php7.expect b/tests/PhpGenerator/ClassType.from.php7.expect new file mode 100644 index 00000000..c23a67bd --- /dev/null +++ b/tests/PhpGenerator/ClassType.from.php7.expect @@ -0,0 +1,26 @@ +class anonymous +{ + public $a; + + private $b; + + + function a() + { + } + + + private function b() + { + } + +} + +class anonymous extends Class1 +{ + + function a() + { + } + +} diff --git a/tests/PhpGenerator/ClassType.from.php7.phpt b/tests/PhpGenerator/ClassType.from.php7.phpt new file mode 100644 index 00000000..17f0090c --- /dev/null +++ b/tests/PhpGenerator/ClassType.from.php7.phpt @@ -0,0 +1,32 @@ + Date: Sun, 4 Oct 2015 19:02:05 +0200 Subject: [PATCH 41/57] ClassType::from() fixes for ReflectionObject --- src/PhpGenerator/ClassType.php | 4 ++-- tests/PhpGenerator/ClassType.from.expect | 8 +++++++- tests/PhpGenerator/ClassType.from.phpt | 10 ++++++++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index 501229db..ec43019d 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -81,12 +81,12 @@ public static function from($from) $class->implements = array_diff($class->implements, $from->getParentClass()->getInterfaceNames()); } foreach ($from->getProperties() as $prop) { - if ($prop->getDeclaringClass() == $from) { // intentionally == + if ($prop->isDefault() && $prop->getDeclaringClass()->getName() === $from->getName()) { $class->properties[$prop->getName()] = Property::from($prop); } } foreach ($from->getMethods() as $method) { - if ($method->getDeclaringClass() == $from) { // intentionally == + if ($method->getDeclaringClass()->getName() === $from->getName()) { $class->methods[$method->getName()] = Method::from($method)->setNamespace($class->namespace); } } diff --git a/tests/PhpGenerator/ClassType.from.expect b/tests/PhpGenerator/ClassType.from.expect index 7c527d15..0d2122a7 100644 --- a/tests/PhpGenerator/ClassType.from.expect +++ b/tests/PhpGenerator/ClassType.from.expect @@ -57,4 +57,10 @@ class Class2 extends Class1 implements Interface2 { } -} \ No newline at end of file +} + +class Class3 +{ + public $prop1; + +} diff --git a/tests/PhpGenerator/ClassType.from.phpt b/tests/PhpGenerator/ClassType.from.phpt index f5f49870..cc6c0820 100644 --- a/tests/PhpGenerator/ClassType.from.phpt +++ b/tests/PhpGenerator/ClassType.from.phpt @@ -7,7 +7,6 @@ namespace Abc; use Nette\PhpGenerator\ClassType; -use ReflectionClass; use Tester\Assert; @@ -62,10 +61,17 @@ class Class2 extends Class1 implements Interface2 {} } +class Class3 +{ + public $prop1; +} $res[] = ClassType::from('Abc\Interface1'); $res[] = ClassType::from('Abc\Interface2'); $res[] = ClassType::from('Abc\Class1'); -$res[] = ClassType::from(new ReflectionClass('Abc\Class2')); +$res[] = ClassType::from(new \ReflectionClass('Abc\Class2')); +$obj = new Class3; +$obj->prop2 = 1; +$res[] = ClassType::from(new \ReflectionObject($obj)); Assert::matchFile(__DIR__ . '/ClassType.from.expect', implode("\n", $res)); From 138ae0cbe07a52a1ecda92dee1d6788236a7698b Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 5 Oct 2015 14:52:01 +0200 Subject: [PATCH 42/57] used https --- composer.json | 6 +++--- contributing.md | 4 ++-- license.md | 2 +- src/PhpGenerator/ClassType.php | 4 ++-- src/PhpGenerator/Helpers.php | 4 ++-- src/PhpGenerator/Method.php | 4 ++-- src/PhpGenerator/Parameter.php | 4 ++-- src/PhpGenerator/PhpFile.php | 4 ++-- src/PhpGenerator/PhpLiteral.php | 4 ++-- src/PhpGenerator/PhpNamespace.php | 4 ++-- src/PhpGenerator/Property.php | 4 ++-- 11 files changed, 22 insertions(+), 22 deletions(-) diff --git a/composer.json b/composer.json index b83a0d90..7e45ceb3 100644 --- a/composer.json +++ b/composer.json @@ -1,16 +1,16 @@ { "name": "nette/php-generator", "description": "Nette PHP Generator", - "homepage": "http://nette.org", + "homepage": "https://nette.org", "license": ["BSD-3-Clause", "GPL-2.0", "GPL-3.0"], "authors": [ { "name": "David Grudl", - "homepage": "http://davidgrudl.com" + "homepage": "https://davidgrudl.com" }, { "name": "Nette Community", - "homepage": "http://nette.org/contributors" + "homepage": "https://nette.org/contributors" } ], "require": { diff --git a/contributing.md b/contributing.md index a1cbbd53..860882bf 100644 --- a/contributing.md +++ b/contributing.md @@ -5,7 +5,7 @@ The issue tracker is the preferred channel for bug reports, features requests and submitting pull requests, but please respect the following restrictions: * Please **do not** use the issue tracker for personal support requests (use - [Nette forum](http://forum.nette.org) or [Stack Overflow](http://stackoverflow.com)). + [Nette forum](https://forum.nette.org) or [Stack Overflow](http://stackoverflow.com)). * Please **do not** derail or troll issues. Keep the discussion on topic and respect the opinions of others. @@ -21,7 +21,7 @@ fits with the scope and aims of the project. It's up to *you* to make a strong case to convince the project's developers of the merits of this feature. Nette welcomes **pull requests**. If you'd like to contribute, please take a moment -to [read the guidelines](http://nette.org/en/contributing) in order to make +to [read the guidelines](https://nette.org/en/contributing) in order to make the contribution process easy and effective for everyone involved. Thanks! diff --git a/license.md b/license.md index af571d59..cf741bd0 100644 --- a/license.md +++ b/license.md @@ -21,7 +21,7 @@ If your stuff is good, it will not take long to establish a reputation for yours New BSD License --------------- -Copyright (c) 2004, 2014 David Grudl (http://davidgrudl.com) +Copyright (c) 2004, 2014 David Grudl (https://davidgrudl.com) All rights reserved. Redistribution and use in source and binary forms, with or without modification, diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index ec43019d..35ebecb8 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -1,8 +1,8 @@ Date: Mon, 5 Oct 2015 14:52:13 +0200 Subject: [PATCH 43/57] travis: removed PHP 7 from allowed failures --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 4d2d58e4..e447d651 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,6 @@ php: matrix: allow_failures: - - php: 7.0 - php: hhvm include: From 979621523d9bb2241deb4494e610a1e3d50ab9cd Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 6 Oct 2015 17:28:22 +0200 Subject: [PATCH 44/57] use ClassName::class syntax --- tests/PhpGenerator/ClassType.from.phpt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/PhpGenerator/ClassType.from.phpt b/tests/PhpGenerator/ClassType.from.phpt index cc6c0820..93659ee2 100644 --- a/tests/PhpGenerator/ClassType.from.phpt +++ b/tests/PhpGenerator/ClassType.from.phpt @@ -66,10 +66,10 @@ class Class3 public $prop1; } -$res[] = ClassType::from('Abc\Interface1'); -$res[] = ClassType::from('Abc\Interface2'); -$res[] = ClassType::from('Abc\Class1'); -$res[] = ClassType::from(new \ReflectionClass('Abc\Class2')); +$res[] = ClassType::from(Interface1::class); +$res[] = ClassType::from(Interface2::class); +$res[] = ClassType::from(Class1::class); +$res[] = ClassType::from(new \ReflectionClass(Class2::class)); $obj = new Class3; $obj->prop2 = 1; $res[] = ClassType::from(new \ReflectionObject($obj)); From d53b437025ab4caa7136c540374934e3b15d325a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Kou=C5=99il?= Date: Thu, 8 Oct 2015 09:21:54 +0200 Subject: [PATCH 45/57] fixed inconsistent use statement coding style --- tests/PhpGenerator/ClassType.from.trait.phpt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/PhpGenerator/ClassType.from.trait.phpt b/tests/PhpGenerator/ClassType.from.trait.phpt index cfb62ed7..23fa076c 100644 --- a/tests/PhpGenerator/ClassType.from.trait.phpt +++ b/tests/PhpGenerator/ClassType.from.trait.phpt @@ -28,7 +28,8 @@ trait Trait2 abstract class Class1 { - use Trait1, Trait2; + use Trait1; + use Trait2; } class Class2 extends Class1 From 45c89d0239a67b48fd3a5b2b389b43609c4616c3 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 27 Nov 2015 01:02:39 +0100 Subject: [PATCH 46/57] added tests --- tests/PhpGenerator/PhpNamespace.fqn.phpt | 59 +++++++++++++++++++++ tests/PhpGenerator/PhpNamespace.fqn1.expect | 16 ++++++ tests/PhpGenerator/PhpNamespace.fqn2.expect | 16 ++++++ tests/PhpGenerator/PhpNamespace.phpt | 2 + 4 files changed, 93 insertions(+) create mode 100644 tests/PhpGenerator/PhpNamespace.fqn.phpt create mode 100644 tests/PhpGenerator/PhpNamespace.fqn1.expect create mode 100644 tests/PhpGenerator/PhpNamespace.fqn2.expect diff --git a/tests/PhpGenerator/PhpNamespace.fqn.phpt b/tests/PhpGenerator/PhpNamespace.fqn.phpt new file mode 100644 index 00000000..50954e42 --- /dev/null +++ b/tests/PhpGenerator/PhpNamespace.fqn.phpt @@ -0,0 +1,59 @@ +setExtends('\ParentClass') + ->addImplement('One') + ->addImplement('\Two') + ->addTrait('Three') + ->addTrait('\Four'); + +$class->addMethod('one') + ->setReturnType('One'); + +$method = $class->addMethod('two') + ->setReturnType('\Two'); + +$method->addParameter('one') + ->setTypeHint('One'); + +$method->addParameter('two') + ->setTypeHint('\Two'); + +Assert::matchFile(__DIR__ . '/PhpNamespace.fqn1.expect', (string) $class); + + +// global namespace +$class = new ClassType('Example', new PhpNamespace); +$class + ->setExtends('\ParentClass') + ->addImplement('One') + ->addImplement('\Two') + ->addTrait('Three') + ->addTrait('\Four'); + +$class->addMethod('one') + ->setReturnType('One'); + +$method = $class->addMethod('two') + ->setReturnType('\Two'); + +$method->addParameter('one') + ->setTypeHint('One'); + +$method->addParameter('two') + ->setTypeHint('\Two'); + +Assert::matchFile(__DIR__ . '/PhpNamespace.fqn2.expect', (string) $class); diff --git a/tests/PhpGenerator/PhpNamespace.fqn1.expect b/tests/PhpGenerator/PhpNamespace.fqn1.expect new file mode 100644 index 00000000..617f1078 --- /dev/null +++ b/tests/PhpGenerator/PhpNamespace.fqn1.expect @@ -0,0 +1,16 @@ +class Example extends ParentClass implements One, Two +{ + use Three; + use Four; + + + public function one(): One + { + } + + + public function two(One $one, Two $two): Two + { + } + +} diff --git a/tests/PhpGenerator/PhpNamespace.fqn2.expect b/tests/PhpGenerator/PhpNamespace.fqn2.expect new file mode 100644 index 00000000..617f1078 --- /dev/null +++ b/tests/PhpGenerator/PhpNamespace.fqn2.expect @@ -0,0 +1,16 @@ +class Example extends ParentClass implements One, Two +{ + use Three; + use Four; + + + public function one(): One + { + } + + + public function two(One $one, Two $two): Two + { + } + +} diff --git a/tests/PhpGenerator/PhpNamespace.phpt b/tests/PhpGenerator/PhpNamespace.phpt index 34c2e9ed..5a1b5daa 100644 --- a/tests/PhpGenerator/PhpNamespace.phpt +++ b/tests/PhpGenerator/PhpNamespace.phpt @@ -28,12 +28,14 @@ foreach (['String', 'string', 'int', 'float', 'bool', 'array', 'callable', 'self $namespace = new PhpNamespace('Foo'); +Assert::same('\A', $namespace->unresolveName('\A')); Assert::same('\A', $namespace->unresolveName('A')); Assert::same('A', $namespace->unresolveName('foo\A')); $namespace->addUse('Bar\C'); Assert::same('\Bar', $namespace->unresolveName('Bar')); +Assert::same('C', $namespace->unresolveName('\bar\C')); Assert::same('C', $namespace->unresolveName('bar\C')); Assert::same('C\D', $namespace->unresolveName('Bar\C\D')); From 25c327711b1b93ae61b29a1e13dd717534b63b1d Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 27 Nov 2015 01:06:40 +0100 Subject: [PATCH 47/57] ClassType, Method: class types are not resolved when namespace is not specified [#21] --- src/PhpGenerator/ClassType.php | 2 +- src/PhpGenerator/Method.php | 7 +++---- tests/PhpGenerator/PhpNamespace.fqn1.expect | 6 +++--- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index 35ebecb8..a46f1d73 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -121,7 +121,7 @@ public function __toString() } $mapper = function (array $arr) { - return array_map([$this->namespace ?: new PhpNamespace, 'unresolveName'], $arr); + return $this->namespace ? array_map([$this->namespace, 'unresolveName'], $arr) : $arr; }; return Strings::normalize( diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index fc8bd853..2ae095a3 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -103,12 +103,11 @@ public function __construct($name = NULL) */ public function __toString() { - $namespace = $this->namespace ?: new PhpNamespace; $parameters = []; foreach ($this->parameters as $param) { $variadic = $this->variadic && $param === end($this->parameters); - - $parameters[] = ($param->getTypeHint() ? $namespace->unresolveName($param->getTypeHint()) . ' ' : '') + $hint = $param->getTypeHint(); + $parameters[] = ($hint ? ($this->namespace ? $this->namespace->unresolveName($hint) : $hint) . ' ' : '') . ($param->isReference() ? '&' : '') . ($variadic ? '...' : '') . '$' . $param->getName() @@ -129,7 +128,7 @@ public function __toString() . ' ' . $this->name . '(' . implode(', ', $parameters) . ')' . ($this->uses ? ' use (' . implode(', ', $uses) . ')' : '') - . ($this->returnType ? ': ' . $namespace->unresolveName($this->returnType) : '') + . ($this->returnType ? ': ' . ($this->namespace ? $this->namespace->unresolveName($this->returnType) : $this->returnType) : '') . ($this->abstract || $this->body === FALSE ? ';' : ($this->name ? "\n" : ' ') . "{\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) . '}'); } diff --git a/tests/PhpGenerator/PhpNamespace.fqn1.expect b/tests/PhpGenerator/PhpNamespace.fqn1.expect index 617f1078..d36d3e10 100644 --- a/tests/PhpGenerator/PhpNamespace.fqn1.expect +++ b/tests/PhpGenerator/PhpNamespace.fqn1.expect @@ -1,7 +1,7 @@ -class Example extends ParentClass implements One, Two +class Example extends \ParentClass implements One, \Two { use Three; - use Four; + use \Four; public function one(): One @@ -9,7 +9,7 @@ class Example extends ParentClass implements One, Two } - public function two(One $one, Two $two): Two + public function two(One $one, \Two $two): \Two { } From 3347ceee2074a7b331ffb12d72e715a75d5db3ff Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 20 Jan 2016 04:24:35 +0100 Subject: [PATCH 48/57] Helpers::dump() support for infinite floats --- src/PhpGenerator/Helpers.php | 7 +++++-- tests/PhpGenerator/Helpers.dump().phpt | 7 +++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/PhpGenerator/Helpers.php b/src/PhpGenerator/Helpers.php index 02c3840f..197b7dbc 100644 --- a/src/PhpGenerator/Helpers.php +++ b/src/PhpGenerator/Helpers.php @@ -35,8 +35,11 @@ private static function _dump(& $var, $level = 0) return (string) $var; } elseif (is_float($var)) { - $var = var_export($var, TRUE); - return strpos($var, '.') === FALSE ? $var . '.0' : $var; + if (is_finite($var)) { + $var = var_export($var, TRUE); + return strpos($var, '.') === FALSE ? $var . '.0' : $var; // workaround for PHP < 7.0.2 + } + return str_replace('.0', '', var_export($var, TRUE)); // workaround for PHP 7.0.2 } elseif (is_bool($var)) { return $var ? 'TRUE' : 'FALSE'; diff --git a/tests/PhpGenerator/Helpers.dump().phpt b/tests/PhpGenerator/Helpers.dump().phpt index bdab67d1..816779ae 100644 --- a/tests/PhpGenerator/Helpers.dump().phpt +++ b/tests/PhpGenerator/Helpers.dump().phpt @@ -11,9 +11,16 @@ use Tester\Assert; require __DIR__ . '/../bootstrap.php'; +ini_set('serialize_precision', 14); +Assert::same('0', Helpers::dump(0)); Assert::same('1', Helpers::dump(1)); +Assert::same('0.0', Helpers::dump(0.0)); Assert::same('1.0', Helpers::dump(1.0)); +Assert::same('0.1', Helpers::dump(0.1)); +Assert::same('INF', Helpers::dump(INF)); +Assert::same('-INF', Helpers::dump(-INF)); +Assert::same('NAN', Helpers::dump(NAN)); Assert::same('NULL', Helpers::dump(NULL)); Assert::same('TRUE', Helpers::dump(TRUE)); Assert::same('FALSE', Helpers::dump(FALSE)); From 526f7e28a5666d63310e038dbfd4191d9a0fb08d Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sat, 6 Feb 2016 23:05:37 +0100 Subject: [PATCH 49/57] typo --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index e447d651..700267bc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,7 +24,7 @@ before_script: # Install Nette Tester & Code Checker - travis_retry composer update --no-interaction --prefer-dist $dependencies - travis_retry composer create-project nette/code-checker temp/code-checker ~2.5 --no-interaction - + sudo: false cache: From 7a59bbedc1266a8f026070f115d3162005cea4b1 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 15 Mar 2016 19:56:26 +0000 Subject: [PATCH 50/57] composer: added Tracy --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 7e45ceb3..73b2e485 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ "nette/utils": "~2.2" }, "require-dev": { - "nette/tester": "~1.4" + "nette/tester": "~1.4", + "tracy/tracy": "^2.3" }, "conflict": { "nette/nette": "<2.2" From 156d0215ed447b6aba681e34e1e2a098d50bcecb Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 6 Apr 2016 00:18:24 +0200 Subject: [PATCH 51/57] uses Nette\SmartObject & StaticClass --- composer.json | 2 +- src/PhpGenerator/ClassType.php | 4 +++- src/PhpGenerator/Helpers.php | 2 ++ src/PhpGenerator/Method.php | 4 +++- src/PhpGenerator/Parameter.php | 4 +++- src/PhpGenerator/PhpFile.php | 6 ++++-- src/PhpGenerator/PhpNamespace.php | 6 ++++-- src/PhpGenerator/Property.php | 4 +++- 8 files changed, 23 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index 73b2e485..6fb5d0c8 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ ], "require": { "php": ">=5.6.0", - "nette/utils": "~2.2" + "nette/utils": "~2.4" }, "require-dev": { "nette/tester": "~1.4", diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index a46f1d73..f4642e49 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -14,8 +14,10 @@ /** * Class/Interface/Trait description. */ -class ClassType extends Nette\Object +class ClassType { + use Nette\SmartObject; + const TYPE_CLASS = 'class'; const TYPE_INTERFACE = 'interface'; diff --git a/src/PhpGenerator/Helpers.php b/src/PhpGenerator/Helpers.php index 197b7dbc..398e3ce3 100644 --- a/src/PhpGenerator/Helpers.php +++ b/src/PhpGenerator/Helpers.php @@ -15,6 +15,8 @@ */ class Helpers { + use Nette\StaticClass; + const PHP_IDENT = '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'; const MAX_DEPTH = 50; diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index 2ae095a3..1005b9ed 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -13,8 +13,10 @@ /** * Method or function description. */ -class Method extends Nette\Object +class Method { + use Nette\SmartObject; + /** @var string|NULL */ private $name; diff --git a/src/PhpGenerator/Parameter.php b/src/PhpGenerator/Parameter.php index 24fb7f43..9ed048cc 100644 --- a/src/PhpGenerator/Parameter.php +++ b/src/PhpGenerator/Parameter.php @@ -13,8 +13,10 @@ /** * Method parameter description. */ -class Parameter extends Nette\Object +class Parameter { + use Nette\SmartObject; + /** @var string */ private $name = ''; diff --git a/src/PhpGenerator/PhpFile.php b/src/PhpGenerator/PhpFile.php index 0c6aa03e..278b52db 100644 --- a/src/PhpGenerator/PhpFile.php +++ b/src/PhpGenerator/PhpFile.php @@ -7,7 +7,7 @@ namespace Nette\PhpGenerator; -use Nette\Object; +use Nette; use Nette\Utils\Strings; @@ -19,8 +19,10 @@ * - doc comments * - one or more namespaces */ -class PhpFile extends Object +class PhpFile { + use Nette\SmartObject; + /** @var string|NULL */ private $comment; diff --git a/src/PhpGenerator/PhpNamespace.php b/src/PhpGenerator/PhpNamespace.php index 40ca90e0..c9d33049 100644 --- a/src/PhpGenerator/PhpNamespace.php +++ b/src/PhpGenerator/PhpNamespace.php @@ -7,8 +7,8 @@ namespace Nette\PhpGenerator; +use Nette; use Nette\InvalidStateException; -use Nette\Object; use Nette\Utils\Strings; @@ -20,8 +20,10 @@ * - variable amount of use statements * - one or more class declarations */ -class PhpNamespace extends Object +class PhpNamespace { + use Nette\SmartObject; + /** @var string */ private $name; diff --git a/src/PhpGenerator/Property.php b/src/PhpGenerator/Property.php index 62f12fa7..91258ce8 100644 --- a/src/PhpGenerator/Property.php +++ b/src/PhpGenerator/Property.php @@ -13,8 +13,10 @@ /** * Class property description. */ -class Property extends Nette\Object +class Property { + use Nette\SmartObject; + /** @var string */ private $name = ''; From bf60eaad18cd1a2ab372cc9637a509c38daa4252 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 20 Apr 2016 15:56:46 +0200 Subject: [PATCH 52/57] added some @property --- src/PhpGenerator/ClassType.php | 2 ++ src/PhpGenerator/Method.php | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index f4642e49..7b2e9168 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -13,6 +13,8 @@ /** * Class/Interface/Trait description. + * + * @property Method[] $methods */ class ClassType { diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index 1005b9ed..185e8c09 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -12,6 +12,8 @@ /** * Method or function description. + * + * @property string $body */ class Method { From a7a175bf0104705d1d31d639c05a678173b49208 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 20 Apr 2016 17:34:05 +0200 Subject: [PATCH 53/57] tests/travis: reports code coverage to Coveralls --- .travis.yml | 11 ++++++++++- readme.md | 1 + tests/.coveralls.yml | 4 ++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 tests/.coveralls.yml diff --git a/.travis.yml b/.travis.yml index 700267bc..7d58d6dd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ matrix: env: dependencies="--prefer-lowest --prefer-stable" script: - - vendor/bin/tester tests -s -p php + - vendor/bin/tester tests -s -p php $coverage - php temp/code-checker/src/code-checker.php --short-arrays after_failure: @@ -24,6 +24,15 @@ before_script: # Install Nette Tester & Code Checker - travis_retry composer update --no-interaction --prefer-dist $dependencies - travis_retry composer create-project nette/code-checker temp/code-checker ~2.5 --no-interaction + - if [ $TRAVIS_PHP_VERSION == "7.0" ]; then coverage="-p phpdbg --coverage ./coverage.xml --coverage-src ./src"; fi + +after_script: + # Report Code Coverage + - > + if [ "$coverage" != "" ]; then + wget https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar + && php coveralls.phar --verbose --config tests/.coveralls.yml + || true; fi sudo: false diff --git a/readme.md b/readme.md index 48e360ad..e9307e03 100644 --- a/readme.md +++ b/readme.md @@ -3,6 +3,7 @@ Nette PHP Generator [![Downloads this Month](https://img.shields.io/packagist/dm/nette/php-generator.svg)](https://packagist.org/packages/nette/php-generator) [![Build Status](https://travis-ci.org/nette/php-generator.svg?branch=master)](https://travis-ci.org/nette/php-generator) +[![Coverage Status](https://coveralls.io/repos/github/nette/php-generator/badge.svg?branch=master)](https://coveralls.io/github/nette/php-generator?branch=master) [![Latest Stable Version](https://poser.pugx.org/nette/php-generator/v/stable)](https://github.com/nette/php-generator/releases) [![License](https://img.shields.io/badge/license-New%20BSD-blue.svg)](https://github.com/nette/php-generator/blob/master/license.md) diff --git a/tests/.coveralls.yml b/tests/.coveralls.yml new file mode 100644 index 00000000..82764a3f --- /dev/null +++ b/tests/.coveralls.yml @@ -0,0 +1,4 @@ +# for php-coveralls +service_name: travis-ci +coverage_clover: coverage.xml +json_path: coverage.json From 301dc25514524d63b359bfc6c55676837ef5da98 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 17 May 2016 17:36:24 +0200 Subject: [PATCH 54/57] travis: removed HHVM --- .travis.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7d58d6dd..72ae79cb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,12 +2,8 @@ language: php php: - 5.6 - 7.0 - - hhvm matrix: - allow_failures: - - php: hhvm - include: - php: 5.6 env: dependencies="--prefer-lowest --prefer-stable" From 436453b8444897bc28fd0a512b8ca001f2fe8850 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 17 May 2016 17:49:30 +0200 Subject: [PATCH 55/57] composer: used Tester 2 --- .travis.yml | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 72ae79cb..e5bdfacd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,7 @@ matrix: env: dependencies="--prefer-lowest --prefer-stable" script: - - vendor/bin/tester tests -s -p php $coverage + - vendor/bin/tester tests -s $coverage - php temp/code-checker/src/code-checker.php --short-arrays after_failure: diff --git a/composer.json b/composer.json index 6fb5d0c8..de69ad90 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "nette/utils": "~2.4" }, "require-dev": { - "nette/tester": "~1.4", + "nette/tester": "~2.0", "tracy/tracy": "^2.3" }, "conflict": { From c78c01b664112f0319b1d8b722d0697dde6c7d6c Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 6 Jun 2016 20:51:14 +0200 Subject: [PATCH 56/57] deprecated setDocuments(), getDocuments() and addDocument() trigger E_USER_DEPRECATED --- src/PhpGenerator/ClassType.php | 3 +++ src/PhpGenerator/Method.php | 3 +++ src/PhpGenerator/PhpFile.php | 3 +++ src/PhpGenerator/Property.php | 5 ++++- 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index 7b2e9168..14552baa 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -367,6 +367,7 @@ public function addComment($val) /** @deprecated */ public function setDocuments(array $s) { + trigger_error(__METHOD__ . '() is deprecated, use similar setComment()', E_USER_DEPRECATED); return $this->setComment(implode("\n", $s)); } @@ -374,6 +375,7 @@ public function setDocuments(array $s) /** @deprecated */ public function getDocuments() { + trigger_error(__METHOD__ . '() is deprecated, use similar getComment()', E_USER_DEPRECATED); return $this->comment ? [$this->comment] : []; } @@ -381,6 +383,7 @@ public function getDocuments() /** @deprecated */ public function addDocument($s) { + trigger_error(__METHOD__ . '() is deprecated, use addComment()', E_USER_DEPRECATED); return $this->addComment($s); } diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index 185e8c09..3261bf85 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -409,6 +409,7 @@ public function addComment($val) /** @deprecated */ public function setDocuments(array $s) { + trigger_error(__METHOD__ . '() is deprecated, use similar setComment()', E_USER_DEPRECATED); return $this->setComment(implode("\n", $s)); } @@ -416,6 +417,7 @@ public function setDocuments(array $s) /** @deprecated */ public function getDocuments() { + trigger_error(__METHOD__ . '() is deprecated, use similar getComment()', E_USER_DEPRECATED); return $this->comment ? [$this->comment] : []; } @@ -423,6 +425,7 @@ public function getDocuments() /** @deprecated */ public function addDocument($s) { + trigger_error(__METHOD__ . '() is deprecated, use addComment()', E_USER_DEPRECATED); return $this->addComment($s); } diff --git a/src/PhpGenerator/PhpFile.php b/src/PhpGenerator/PhpFile.php index 278b52db..9755ef40 100644 --- a/src/PhpGenerator/PhpFile.php +++ b/src/PhpGenerator/PhpFile.php @@ -64,6 +64,7 @@ public function addComment($val) /** @deprecated */ public function setDocuments(array $s) { + trigger_error(__METHOD__ . '() is deprecated, use similar setComment()', E_USER_DEPRECATED); return $this->setComment(implode("\n", $s)); } @@ -71,6 +72,7 @@ public function setDocuments(array $s) /** @deprecated */ public function getDocuments() { + trigger_error(__METHOD__ . '() is deprecated, use similar getComment()', E_USER_DEPRECATED); return $this->comment ? [$this->comment] : []; } @@ -78,6 +80,7 @@ public function getDocuments() /** @deprecated */ public function addDocument($s) { + trigger_error(__METHOD__ . '() is deprecated, use addComment()', E_USER_DEPRECATED); return $this->addComment($s); } diff --git a/src/PhpGenerator/Property.php b/src/PhpGenerator/Property.php index 91258ce8..b742f106 100644 --- a/src/PhpGenerator/Property.php +++ b/src/PhpGenerator/Property.php @@ -170,13 +170,15 @@ public function addComment($val) /** @deprecated */ public function setDocuments(array $s) { + trigger_error(__METHOD__ . '() is deprecated, use similar setComment()', E_USER_DEPRECATED); return $this->setComment(implode("\n", $s)); -} + } /** @deprecated */ public function getDocuments() { + trigger_error(__METHOD__ . '() is deprecated, use similar getComment()', E_USER_DEPRECATED); return $this->comment ? [$this->comment] : []; } @@ -184,6 +186,7 @@ public function getDocuments() /** @deprecated */ public function addDocument($s) { + trigger_error(__METHOD__ . '() is deprecated, use addComment()', E_USER_DEPRECATED); return $this->addComment($s); } From 2cd0d0e922e4fad2fc58ccac6af8c9753d2b88c8 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 17 Jun 2016 17:18:56 +0200 Subject: [PATCH 57/57] ClassType::setName is not deprecated --- src/PhpGenerator/ClassType.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index 14552baa..b657638b 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -156,7 +156,10 @@ public function getNamespace() } - /** @deprecated */ + /** + * @param string + * @return self + */ public function setName($name) { $this->name = (string) $name;