This commit is contained in:
Sebastian Bergmann 2019-08-14 16:54:55 +02:00
parent 180a73e608
commit ff5bade60c
8 changed files with 74 additions and 86 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@
/vendor
/.php_cs
/.php_cs.cache
/.phpunit.result.cache

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
$header = <<<'EOF'
This file is part of exporter package.
This file is part of sebastian/exporter.
(c) Sebastian Bergmann <sebastian@phpunit.de>
@ -142,6 +142,10 @@ return PhpCsFixer\Config::create()
],
],
'ordered_imports' => true,
'ordered_interfaces' => [
'direction' => 'ascend',
'order' => 'alpha',
],
'phpdoc_add_missing_param_annotation' => true,
'phpdoc_align' => true,
'phpdoc_annotation_without_dot' => true,
@ -157,7 +161,7 @@ return PhpCsFixer\Config::create()
'phpdoc_to_comment' => true,
'phpdoc_trim' => true,
'phpdoc_trim_consecutive_blank_line_separation' => true,
'phpdoc_types' => true,
'phpdoc_types' => ['groups' => ['simple', 'meta']],
'phpdoc_types_order' => true,
'phpdoc_var_without_name' => true,
'pow_to_exponentiation' => true,
@ -175,10 +179,17 @@ return PhpCsFixer\Config::create()
'single_quote' => true,
'standardize_not_equals' => true,
'ternary_to_null_coalescing' => true,
'trailing_comma_in_multiline_array' => true,
'trim_array_spaces' => true,
'unary_operator_spaces' => true,
'visibility_required' => true,
//'void_return' => true,
'visibility_required' => [
'elements' => [
'const',
'method',
'property',
],
],
'void_return' => true,
'whitespace_after_comma_in_array' => true,
]
)

View File

@ -1,11 +1,10 @@
language: php
php:
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4snapshot
- nightly
before_install:
- composer self-update

View File

@ -2,6 +2,10 @@
All notable changes are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.
## [3.1.2] - 2019-MM-DD
## [4.0.0] - 2019-MM-DD
[3.1.2]: https://github.com/sebastianbergmann/exporter/compare/3.1.1...3.1.2
### Removed
* This component is no longer supported on PHP 7.0 and PHP 7.1
[4.0.0]: https://github.com/sebastianbergmann/exporter/compare/3.1.1...4.0.0

View File

@ -2,7 +2,7 @@
"name": "sebastian/exporter",
"description": "Provides the functionality to export PHP variables for visualization",
"keywords": ["exporter","export"],
"homepage": "http://www.github.com/sebastianbergmann/exporter",
"homepage": "https://github.com/sebastianbergmann/exporter",
"license": "BSD-3-Clause",
"authors": [
{
@ -30,14 +30,15 @@
"optimize-autoloader": true,
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^7.0",
"php": "^7.2",
"ext-mbstring": "*",
"sebastian/recursion-context": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "^6.0",
"ext-mbstring": "*"
"phpunit/phpunit": "^8.4"
},
"autoload": {
"classmap": [
@ -46,7 +47,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.1.x-dev"
"dev-master": "4.0-dev"
}
}
}

View File

@ -1,15 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.0/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
executionOrder="depends,defects"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true">
<testsuite>
<directory suffix="Test.php">tests</directory>
</testsuite>
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
/*
* This file is part of exporter package.
* This file is part of sebastian/exporter.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
@ -15,14 +15,14 @@ use SebastianBergmann\RecursionContext\Context;
* A nifty utility for visualizing PHP variables.
*
* <code>
* <?php
* <?php declare(strict_types=1);
* use SebastianBergmann\Exporter\Exporter;
*
* $exporter = new Exporter;
* print $exporter->export(new Exception);
* </code>
*/
class Exporter
final class Exporter
{
/**
* Exports a value as a string
@ -36,26 +36,16 @@ class Exporter
* - Strings are always quoted with single quotes
* - Carriage returns and newlines are normalized to \n
* - Recursion and repeated rendering is treated properly
*
* @param int $indentation The indentation level of the 2nd+ line
*
* @return string
*/
public function export($value, $indentation = 0)
public function export($value, int $indentation = 0): string
{
return $this->recursiveExport($value, $indentation);
}
/**
* @param mixed $data
* @param Context $context
*
* @return string
*/
public function shortenedRecursiveExport(&$data, Context $context = null)
public function shortenedRecursiveExport(&$data, Context $context = null): string
{
$result = [];
$exporter = new self();
$exporter = new self;
if (!$context) {
$context = new Context;
@ -91,23 +81,15 @@ class Exporter
* Newlines are replaced by the visible string '\n'.
* Contents of arrays and objects (if any) are replaced by '...'.
*
* @return string
*
* @see SebastianBergmann\Exporter\Exporter::export
* @see \SebastianBergmann\Exporter\Exporter::export
*/
public function shortenedExport($value)
public function shortenedExport($value): string
{
if (\is_string($value)) {
$string = \str_replace("\n", '', $this->export($value));
if (\function_exists('mb_strlen')) {
if (\mb_strlen($string) > 40) {
$string = \mb_substr($string, 0, 30) . '...' . \mb_substr($string, -7);
}
} else {
if (\strlen($string) > 40) {
$string = \substr($string, 0, 30) . '...' . \substr($string, -7);
}
if (\mb_strlen($string) > 40) {
$string = \mb_substr($string, 0, 30) . '...' . \mb_substr($string, -7);
}
return $string;
@ -134,10 +116,8 @@ class Exporter
/**
* Converts an object to an array containing all of its private, protected
* and public properties.
*
* @return array
*/
public function toArray($value)
public function toArray($value): array
{
if (!\is_object($value)) {
return (array) $value;
@ -175,13 +155,13 @@ class Exporter
if ($value instanceof \SplObjectStorage) {
// However, the fast method does work in HHVM, and exposes the
// internal implementation. Hide it again.
if (\property_exists('\SplObjectStorage', '__storage')) {
if (\property_exists(\SplObjectStorage::class, '__storage')) {
unset($array['__storage']);
} elseif (\property_exists('\SplObjectStorage', 'storage')) {
} elseif (\property_exists(\SplObjectStorage::class, 'storage')) {
unset($array['storage']);
}
if (\property_exists('\SplObjectStorage', '__key')) {
if (\property_exists(\SplObjectStorage::class, '__key')) {
unset($array['__key']);
}
@ -197,17 +177,9 @@ class Exporter
}
/**
* Recursive implementation of export
*
* @param mixed $value The value to export
* @param int $indentation The indentation level of the 2nd+ line
* @param \SebastianBergmann\RecursionContext\Context $processed Previously processed objects
*
* @return string
*
* @see SebastianBergmann\Exporter\Exporter::export
* @param mixed $value
*/
protected function recursiveExport(&$value, $indentation, $processed = null)
private function recursiveExport(&$value, int $indentation, Context $processed = null): string
{
if ($value === null) {
return 'null';
@ -228,7 +200,7 @@ class Exporter
if (\is_resource($value)) {
return \sprintf(
'resource(%d) of type (%s)',
$value,
(int) $value,
\get_resource_type($value)
);
}
@ -267,7 +239,7 @@ class Exporter
$key = $processed->add($value);
$values = '';
if (\count($array) > 0) {
if (\is_array($array) && !empty($array)) {
foreach ($array as $k => $v) {
$values .= \sprintf(
'%s %s => %s' . "\n",

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
/*
* This file is part of exporter package.
* This file is part of sebastian/exporter.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
@ -12,21 +12,21 @@ namespace SebastianBergmann\Exporter;
use PHPUnit\Framework\TestCase;
/**
* @covers SebastianBergmann\Exporter\Exporter
* @covers \SebastianBergmann\Exporter\Exporter
*/
class ExporterTest extends TestCase
final class ExporterTest extends TestCase
{
/**
* @var Exporter
*/
private $exporter;
protected function setUp()
protected function setUp(): void
{
$this->exporter = new Exporter;
}
public function exportProvider()
public function exportProvider(): array
{
$obj2 = new \stdClass;
$obj2->foo = 'bar';
@ -154,19 +154,19 @@ EOF
],
[
\chr(0) . \chr(1) . \chr(2) . \chr(3) . \chr(4) . \chr(5),
'Binary String: 0x000102030405'
'Binary String: 0x000102030405',
],
[
\implode('', \array_map('chr', \range(0x0e, 0x1f))),
'Binary String: 0x0e0f101112131415161718191a1b1c1d1e1f'
'Binary String: 0x0e0f101112131415161718191a1b1c1d1e1f',
],
[
\chr(0x00) . \chr(0x09),
'Binary String: 0x0009'
'Binary String: 0x0009',
],
[
'',
"''"
"''",
],
'export Exception without trace' => [
new \Exception('The exception message', 42),
@ -200,7 +200,7 @@ EOF
/**
* @dataProvider exportProvider
*/
public function testExport($value, $expected)
public function testExport($value, string $expected): void
{
$this->assertStringMatchesFormat(
$expected,
@ -208,7 +208,7 @@ EOF
);
}
public function testExport2()
public function testExport2(): void
{
if (\PHP_VERSION === '5.3.3') {
$this->markTestSkipped('Skipped due to "Nesting level too deep - recursive dependency?" fatal error');
@ -292,7 +292,7 @@ EOF;
);
}
public function shortenedExportProvider()
public function shortenedExportProvider(): array
{
$obj = new \stdClass;
$obj->foo = 'bar';
@ -320,7 +320,7 @@ EOF;
/**
* @dataProvider shortenedExportProvider
*/
public function testShortenedExport($value, $expected)
public function testShortenedExport($value, string $expected): void
{
$this->assertSame(
$expected,
@ -328,10 +328,7 @@ EOF;
);
}
/**
* @requires extension mbstring
*/
public function testShortenedExportForMultibyteCharacters()
public function testShortenedExportForMultibyteCharacters(): void
{
$oldMbLanguage = \mb_language();
\mb_language('Japanese');
@ -340,8 +337,8 @@ EOF;
try {
$this->assertSame(
"'いろはにほへとちりぬるをわかよたれそつねならむうゐのおくや...しゑひもせす'",
$this->trimNewline($this->exporter->shortenedExport('いろはにほへとちりぬるをわかよたれそつねならむうゐのおくやまけふこえてあさきゆめみしゑひもせす'))
"'いろはにほへとちりぬるをわかよたれそつねならむうゐのおくや...しゑひもせす'",
$this->trimNewline($this->exporter->shortenedExport('いろはにほへとちりぬるをわかよたれそつねならむうゐのおくやまけふこえてあさきゆめみしゑひもせす'))
);
} catch (\Exception $e) {
\mb_internal_encoding($oldMbInternalEncoding);
@ -354,7 +351,7 @@ EOF;
\mb_language($oldMbLanguage);
}
public function provideNonBinaryMultibyteStrings()
public function provideNonBinaryMultibyteStrings(): array
{
return [
[\implode('', \array_map('chr', \range(0x09, 0x0d))), 9],
@ -366,7 +363,7 @@ EOF;
/**
* @dataProvider provideNonBinaryMultibyteStrings
*/
public function testNonBinaryStringExport($value, $expectedLength)
public function testNonBinaryStringExport($value, int $expectedLength): void
{
$this->assertRegExp(
"~'.{{$expectedLength}}'\$~s",
@ -374,12 +371,12 @@ EOF;
);
}
public function testNonObjectCanBeReturnedAsArray()
public function testNonObjectCanBeReturnedAsArray(): void
{
$this->assertEquals([true], $this->exporter->toArray(true));
}
private function trimNewline($string)
private function trimNewline(string $string): string
{
return \preg_replace('/[ ]*\n/', "\n", $string);
}