Skip to content

Commit

Permalink
Merge aec8102 into 6e52303
Browse files Browse the repository at this point in the history
  • Loading branch information
bkrukowski committed Dec 4, 2018
2 parents 6e52303 + aec8102 commit b6d25ca
Show file tree
Hide file tree
Showing 44 changed files with 1,380 additions and 348 deletions.
2 changes: 2 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ return PhpCsFixer\Config::create()
'native_function_invocation' => true,
'header_comment' => array('header' => $header),
'phpdoc_align' => true,
'phpdoc_order' => true,
'phpdoc_types_order' => true,
))
->setFinder($finder)
;
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ before_script:

script:
- if [[ "$RUN_CS_FIXER" = "true" ]]; then php -n ${PHP_CS_FIXER} --diff --dry-run -v --allow-risky=yes fix; fi
- php ${PHP_ARGS} vendor/bin/phpunit ${PHPUNIT_EXCLUDE}
- MODE_SPEED_TEST=true php ${PHP_ARGS} vendor/bin/phpunit ${PHPUNIT_EXCLUDE}
- php ${PHP_ARGS} vendor/bin/phpunit ${PHPUNIT_EXCLUDE} -v
- MODE_SPEED_TEST=true php ${PHP_ARGS} vendor/bin/phpunit ${PHPUNIT_EXCLUDE} -v
- |
if [[ ${TRAVIS_PHP_VERSION:0:3} == "7.1" ]]; then
wget https://github.com/infection/infection/releases/download/0.10.3/infection.phar;
Expand All @@ -97,10 +97,14 @@ script:
fi
- php examples/big-dump.php
- php examples/binary-string.php
- php examples/closure.php
- php examples/custom-indent.php
- php examples/debug-info.php
- php examples/dumper.php
- php examples/exception.php
- php examples/pre-exception-variadic.php
- php examples/object.php
- php examples/simple-array.php

after_script:
- mv ~.git .git
Expand Down
39 changes: 39 additions & 0 deletions examples/closure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the awesomite/var-dumper package.
*
* (c) Bartłomiej Krukowski <bartlomiej@krukowski.me>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Awesomite\VarDumper\LightVarDumper;

require __DIR__ . DIRECTORY_SEPARATOR . 'init.php';

global $argc, $argv;
$function = function ($a, $b) use ($argc, $argv) {
};

$dumper = new LightVarDumper();
$dumper->dump($function);

/*
Output:
object(Closure) #3 {[
$name => “{closure}”
$filename => “(...)/examples/closure.php”
$startLine => 17
$endLine => 18
$use =>
array(2) {
[argc] => 1
[argv] => array(1) {“examples/closure.php”}
}
]}
*/
77 changes: 77 additions & 0 deletions examples/exception-variadic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
* This file is part of the awesomite/var-dumper package.
*
* (c) Bartłomiej Krukowski <bartlomiej@krukowski.me>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Awesomite\VarDumper\LightVarDumper;

require __DIR__ . DIRECTORY_SEPARATOR . 'init.php';

/**
* @internal
*/
class DivideByZeroException extends \Exception
{
}

/**
* @internal
*/
class Divider
{
public function divide($a, $b)
{
if (0 === $b) {
throw new DivideByZeroException('Cannot divide by zero');
}

return $a/$b;
}
}

/**
* @internal
*/
class Controller
{
public static function execute($class, $method, ...$params)
{
(new $class())->$method(...$params);
}
}

try {
Controller::execute(Divider::class, 'divide', 10, 0);
} catch (\Exception $exception) {
$dumper = new LightVarDumper();
$dumper->dump($exception);
}

/*
Output:
object(DivideByZeroException) #2 {[
[message] => “Cannot divide by zero”
[code] => 0
[file] => “(...)/examples/exception-variadic.php:31”
[previous] => NULL
[trace] =>
1. (...)/examples/exception-variadic.php:45 Divider->divide(
a: 10
b: 0
)
2. (...)/examples/exception-variadic.php:50 Controller::execute(
class: “Divider”
method: “divide”
params: array(2) {10, 0}
)
]}
*/
82 changes: 82 additions & 0 deletions examples/exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/*
* This file is part of the awesomite/var-dumper package.
*
* (c) Bartłomiej Krukowski <bartlomiej@krukowski.me>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Awesomite\VarDumper\LightVarDumper;

require __DIR__ . DIRECTORY_SEPARATOR . 'init.php';

/**
* @internal
*/
class DivideByZeroException extends \Exception
{
}

/**
* @internal
*/
class Divider
{
public function divide($a, $b)
{
if (0 === $b) {
throw new DivideByZeroException('Cannot divide by zero');
}

return $a/$b;
}
}

/**
* @internal
*/
class Calculator
{
public static function execute($action, $numberA, $numberB)
{
if ('divide' == $action) {
$divider = new Divider();

return $divider->divide($numberA, $numberB);
}
}
}

try {
$calculator = new Calculator();
$calculator->execute('divide', 5, 0);
} catch (\Exception $exception) {
$dumper = new LightVarDumper();
$dumper->dump($exception);
}

/*
Output:
object(DivideByZeroException) #4 {[
[message] => “Cannot divide by zero”
[code] => 0
[file] => “(...)/examples/exception.php:31”
[previous] => NULL
[trace] =>
1. (...)/examples/exception.php:48 Divider->divide(
a: 5
b: 0
)
2. (...)/examples/exception.php:55 Calculator::execute(
action: “divide”
numberA: 5
numberB: 0
)
]}
*/
16 changes: 16 additions & 0 deletions examples/pre-exception-variadic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the awesomite/var-dumper package.
*
* (c) Bartłomiej Krukowski <bartlomiej@krukowski.me>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

if (\version_compare(PHP_VERSION, '5.6') >= 0) {
require_once __DIR__ . DIRECTORY_SEPARATOR . 'exception-variadic.php';
} else {
echo "PHP >= 5.6 required\n";
}
2 changes: 1 addition & 1 deletion src/Config/AbstractConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* @internal
*/
class AbstractConfig
abstract class AbstractConfig
{
protected $maxChildren;

Expand Down
16 changes: 3 additions & 13 deletions src/Helpers/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
namespace Awesomite\VarDumper\Helpers;

use Awesomite\VarDumper\Config\AbstractConfig;
use Awesomite\VarDumper\LightVarDumper;
use Awesomite\VarDumper\Objects\HasherFactory;
use Awesomite\VarDumper\Objects\HasherInterface;
use Awesomite\VarDumper\Subdumpers\SubdumpersCollection;

/**
* @internal
Expand All @@ -27,13 +27,11 @@ final class Container

private $config;

private $printNlOnEnd;

private $dumper;

private static $hasher;

public function __construct(AbstractConfig $config, LightVarDumper $dumper)
public function __construct(AbstractConfig $config, SubdumpersCollection $dumper)
{
$this->config = $config;
$this->dumper = $dumper;
Expand Down Expand Up @@ -64,15 +62,7 @@ public function getDepth()
}

/**
* @return BoolValue
*/
public function getPrintNlOnEnd()
{
return $this->printNlOnEnd ?: $this->printNlOnEnd = new BoolValue(true);
}

/**
* @return LightVarDumper
* @return SubdumpersCollection
*/
public function getDumper()
{
Expand Down
48 changes: 48 additions & 0 deletions src/Helpers/FileNameDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the awesomite/var-dumper package.
*
* (c) Bartłomiej Krukowski <bartlomiej@krukowski.me>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Awesomite\VarDumper\Helpers;

/**
* @internal
*/
final class FileNameDecorator
{
const MAX_FILE_NAME_DEPTH = 3;

/**
* @param string $fileName
* @param null|int $maxDepth
*
* @return string
*/
public static function decorateFileName($fileName, $maxDepth = null)
{
$maxDepth = \is_null($maxDepth)
? static::MAX_FILE_NAME_DEPTH
: $maxDepth;

$relativeTo = $fileName;
for ($i = 0; $i < $maxDepth; $i++) {
$relativeTo = \dirname($relativeTo);
}

if ($relativeTo === $fileName) {
return $fileName;
}

$exploded = \explode(DIRECTORY_SEPARATOR, $fileName);
$newParts = \array_slice($exploded, -$maxDepth, $maxDepth);
\array_unshift($newParts, '(...)');

return \implode(DIRECTORY_SEPARATOR, $newParts);
}
}
17 changes: 16 additions & 1 deletion src/Helpers/KeyValuePrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

namespace Awesomite\VarDumper\Helpers;

use Awesomite\VarDumper\Strings\LinePart;
use Awesomite\VarDumper\Strings\PartInterface;
use Awesomite\VarDumper\Strings\Parts;

/**
* @internal
*/
Expand All @@ -33,13 +37,24 @@ public function add($key, $value, $strlen)
$this->rows[] = array($key, $value, $strlen);
}

/**
* @return null|PartInterface
*/
public function flush()
{
if (!$this->rows) {
return null;
}

$result = new Parts();
foreach ($this->rows as $data) {
list($key, $value, $strlen) = $data;
echo $key, \str_pad('', $this->maxLength - $strlen, ' '), $value, "\n";
$part = new LinePart($key . \str_pad('', $this->maxLength - $strlen, ' ') . $value);
$result->appendPart($part);
}
$this->rows = array();
$this->maxLength = 0;

return $result;
}
}

0 comments on commit b6d25ca

Please sign in to comment.