diff --git a/.travis.yml b/.travis.yml index e5bdfacd..fe3e4729 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,12 +2,16 @@ language: php php: - 5.6 - 7.0 + - 7.1 matrix: include: - php: 5.6 env: dependencies="--prefer-lowest --prefer-stable" + allow_failures: + - php: 7.1 + script: - vendor/bin/tester tests -s $coverage - php temp/code-checker/src/code-checker.php --short-arrays diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index b657638b..5f9f03d3 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -15,6 +15,7 @@ * Class/Interface/Trait description. * * @property Method[] $methods + * @property Property[] $properties */ class ClassType { diff --git a/src/PhpGenerator/Helpers.php b/src/PhpGenerator/Helpers.php index 398e3ce3..3fcfd0ca 100644 --- a/src/PhpGenerator/Helpers.php +++ b/src/PhpGenerator/Helpers.php @@ -19,6 +19,7 @@ class Helpers const PHP_IDENT = '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'; const MAX_DEPTH = 50; + const WRAP_LENGTH = 70; /** @@ -92,7 +93,7 @@ private static function _dump(& $var, $level = 0) } unset($var[$marker]); } - return '[' . (strpos($out, "\n") === FALSE && strlen($out) < 40 ? $out : $outAlt) . ']'; + return '[' . (strpos($out, "\n") === FALSE && strlen($out) < self::WRAP_LENGTH ? $out : $outAlt) . ']'; } elseif ($var instanceof \Serializable) { $var = serialize($var); @@ -102,12 +103,16 @@ private static function _dump(& $var, $level = 0) throw new Nette\InvalidArgumentException('Cannot dump closure.'); } elseif (is_object($var)) { + $class = get_class($var); if (PHP_VERSION_ID >= 70000 && (new \ReflectionObject($var))->isAnonymous()) { throw new Nette\InvalidArgumentException('Cannot dump anonymous class.'); + + } elseif (in_array($class, ['DateTime', 'DateTimeImmutable'], TRUE)) { + return self::formatArgs("new $class(?, new DateTimeZone(?))", [$var->format('Y-m-d H:i:s.u'), $var->getTimeZone()->getName()]); } + $arr = (array) $var; $space = str_repeat("\t", $level); - $class = get_class($var); static $list = []; if ($level > self::MAX_DEPTH || in_array($var, $list, TRUE)) { @@ -172,7 +177,7 @@ public static function formatArgs($statement, array $args) $sep = ''; foreach ($arg as $tmp) { $s .= $sep . self::dump($tmp); - $sep = strlen($s) - strrpos($s, "\n") > 100 ? ",\n\t" : ', '; + $sep = strlen($s) - strrpos($s, "\n") > self::WRAP_LENGTH ? ",\n\t" : ', '; } $statement = $s . substr($statement, $a + 2); $a = strlen($s); diff --git a/tests/PhpGenerator/Helpers.dump().phpt b/tests/PhpGenerator/Helpers.dump().phpt index 816779ae..057d9072 100644 --- a/tests/PhpGenerator/Helpers.dump().phpt +++ b/tests/PhpGenerator/Helpers.dump().phpt @@ -37,7 +37,7 @@ Assert::same("[\$s]", Helpers::dump([new PhpLiteral('$s')])); 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("[\n\t[\n\t\t'a',\n\t\t'loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong',\n\t],\n]", Helpers::dump([['a', 'loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong']])); 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])); @@ -94,3 +94,21 @@ Assert::equal(new Test3, eval('return ' . Helpers::dump(new Test3) . ';')); Assert::exception(function () { Helpers::dump(function () {}); }, Nette\InvalidArgumentException::class, 'Cannot dump closure.'); + + + +class TestDateTime extends DateTime +{} + +Assert::same( + "new DateTime('2016-06-22 20:52:43.123400', new DateTimeZone('Europe/Prague'))", + Helpers::dump(new DateTime('2016-06-22 20:52:43.1234', new DateTimeZone('Europe/Prague'))) +); +Assert::same( + "new DateTimeImmutable('2016-06-22 20:52:43.123400', new DateTimeZone('Europe/Prague'))", + Helpers::dump(new DateTimeImmutable('2016-06-22 20:52:43.1234', new DateTimeZone('Europe/Prague'))) +); +Assert::match( + "Nette\\PhpGenerator\\Helpers::createObject('TestDateTime', %A%)", + Helpers::dump(new TestDateTime('2016-06-22 20:52:43.1234', new DateTimeZone('Europe/Prague'))) +); diff --git a/tests/PhpGenerator/Helpers.format.phpt b/tests/PhpGenerator/Helpers.format.phpt index 0b662e21..3382cd7f 100644 --- a/tests/PhpGenerator/Helpers.format.phpt +++ b/tests/PhpGenerator/Helpers.format.phpt @@ -19,7 +19,7 @@ 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)", + "func(10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,\n\t27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40)", Helpers::formatArgs('func(?*)', [range(10, 40)]) );