Skip to content

Commit 86b779d

Browse files
committed
close #16, close #15
1 parent 711dad3 commit 86b779d

File tree

8 files changed

+115
-47
lines changed

8 files changed

+115
-47
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Chevere.
5+
*
6+
* (c) Rodolfo Berrios <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Chevere\VarDump\Interfaces;
15+
16+
use Chevere\DataStructure\Interfaces\VectoredInterface;
17+
18+
/**
19+
* Describes the component in charge of holding object references.
20+
* @extends VectoredInterface<int>
21+
*/
22+
interface ObjectReferencesInterface extends VectoredInterface
23+
{
24+
/**
25+
* @return array<int> $array
26+
*/
27+
public function toArray(): array;
28+
29+
public function push(int $id): void;
30+
31+
public function has(int $id): bool;
32+
}

src/Interfaces/VarDumperInterface.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace Chevere\VarDump\Interfaces;
1515

1616
use Chevere\Parameter\Interfaces\TypeInterface;
17-
use Chevere\VarDump\ObjectReferences;
1817
use Chevere\VarDump\Processors\ArrayProcessor;
1918
use Chevere\VarDump\Processors\BoolProcessor;
2019
use Chevere\VarDump\Processors\FloatProcessor;
@@ -111,5 +110,5 @@ public function depth(): int;
111110
*/
112111
public function withProcess(): self;
113112

114-
public function objectReferences(): ObjectReferences;
113+
public function objectReferences(): ObjectReferencesInterface;
115114
}

src/ObjectReferences.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,13 @@
1313

1414
namespace Chevere\VarDump;
1515

16-
use Chevere\DataStructure\Interfaces\VectoredInterface;
1716
use Chevere\DataStructure\Traits\VectorTrait;
17+
use Chevere\VarDump\Interfaces\ObjectReferencesInterface;
1818

19-
/**
20-
* @implements VectoredInterface<int>
21-
*/
22-
final class ObjectReferences implements VectoredInterface
19+
final class ObjectReferences implements ObjectReferencesInterface
2320
{
2421
use VectorTrait;
2522

26-
/**
27-
* @return array<int> $array
28-
*/
2923
public function toArray(): array
3024
{
3125
return $this->vector->toArray();

src/Processors/ArrayProcessor.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,6 @@ private function isCircularRef(array $array): bool
8888
if (is_array($var)) {
8989
return $this->isCircularRef($var);
9090
}
91-
// if (is_object($var)) {
92-
// $objectId = spl_object_id($var);
93-
// if ($this->varDumper->knownObjectsId()->find($objectId) !== null) {
94-
// // $this->varDumper->writer()->write('->[known]');
95-
96-
// return true;
97-
// }
98-
// }
9991
}
10092

10193
return false;

src/Processors/ObjectProcessor.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,12 @@ public function write(): void
9595
private function setProperties(ReflectionObject $reflection): void
9696
{
9797
$properties = [];
98-
// $properties = $reflection->isInternal()
99-
// ? $this->getPublicProperties()
100-
// : $this->getExternalProperties($reflection);
101-
$properties = $this->getExternalProperties($reflection);
98+
$properties = $this->getProperties($reflection);
99+
// @codeCoverageIgnoreStart
102100
if ($properties === [] && $reflection->isInternal()) {
103101
$properties = $this->getPublicProperties();
104102
}
103+
// @codeCoverageIgnoreEnd
105104
$keys = array_keys($properties);
106105
foreach ($keys as $name) {
107106
$name = strval($name);
@@ -113,6 +112,7 @@ private function setProperties(ReflectionObject $reflection): void
113112

114113
/**
115114
* @return array<string, array<mixed>>
115+
* @codeCoverageIgnore
116116
*/
117117
private function getPublicProperties(): array
118118
{
@@ -129,7 +129,7 @@ private function getPublicProperties(): array
129129
/**
130130
* @return array<string, array<mixed>>
131131
*/
132-
private function getExternalProperties(
132+
private function getProperties(
133133
ReflectionObject $reflection
134134
): array {
135135
$properties = [];

src/VarDumper.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Chevere\DataStructure\Vector;
1818
use Chevere\Parameter\Interfaces\TypeInterface;
1919
use Chevere\VarDump\Interfaces\FormatInterface;
20+
use Chevere\VarDump\Interfaces\ObjectReferencesInterface;
2021
use Chevere\VarDump\Interfaces\ProcessorInterface;
2122
use Chevere\VarDump\Interfaces\VarDumpableInterface;
2223
use Chevere\VarDump\Interfaces\VarDumperInterface;
@@ -39,13 +40,13 @@ public function __construct(
3940
private WriterInterface $writer,
4041
private FormatInterface $format,
4142
private VarDumpableInterface $dumpable,
42-
private ObjectReferences $objectReferences
43+
private ObjectReferencesInterface $objectReferences
4344
) {
4445
$this->knownObjectsId = new Vector();
4546
++$this->depth;
4647
}
4748

48-
public function objectReferences(): ObjectReferences
49+
public function objectReferences(): ObjectReferencesInterface
4950
{
5051
return $this->objectReferences;
5152
}

tests/ObjectReferencesTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Chevere.
5+
*
6+
* (c) Rodolfo Berrios <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Chevere\Tests;
15+
16+
use Chevere\VarDump\ObjectReferences;
17+
use PHPUnit\Framework\TestCase;
18+
19+
final class ObjectReferencesTest extends TestCase
20+
{
21+
public function testToArray(): void
22+
{
23+
$objectReferences = new ObjectReferences();
24+
$this->assertSame([], $objectReferences->toArray());
25+
$objectReferences->push(1);
26+
$this->assertSame([1], $objectReferences->toArray());
27+
}
28+
29+
public function testPushHas(): void
30+
{
31+
$objectReferences = new ObjectReferences();
32+
$objectReferences->push(1);
33+
$this->assertTrue($objectReferences->has(1));
34+
$objectReferences->push(1);
35+
$objectReferences->push(2);
36+
$this->assertTrue($objectReferences->has(2));
37+
$this->assertSame([1, 2], $objectReferences->toArray());
38+
}
39+
}

tests/Processors/ObjectProcessorTest.php

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -96,28 +96,39 @@ public function testObjectProperty(): void
9696
);
9797
}
9898

99-
// public function testStdClass(): void
100-
// {
101-
// $var = new stdClass();
102-
// $ref = new stdClass();
103-
// $ref->scalar = 'REF';
104-
// $ref->circular = $ref;
105-
// $ref->var = $var;
106-
// $var->scalar = 'VAR';
107-
// $var->ref = $ref;
108-
// $var->circular = $var;
109-
// $var->refArr = [$ref];
110-
// $var->circularArr = [$var];
111-
// $varDumper = $this->getVarDumper($var);
112-
// var_dump("\n" . $varDumper->writer()->__toString());
113-
// exit();
114-
// // $id = strval(spl_object_id($object));
115-
// // $varDumper = $this->getVarDumper($object);
116-
// // $this->assertSame(
117-
// // 'stdClass#' . $id,
118-
// // $varDumper->writer()->__toString()
119-
// // );
120-
// }
99+
public function testCircularReferences(): void
100+
{
101+
$var = new stdClass();
102+
$ref = new stdClass();
103+
$ref->scalar = 'REF';
104+
$ref->circular = $ref;
105+
$ref->var = $var;
106+
$var->scalar = 'VAR';
107+
$var->ref = $ref;
108+
$var->circular = $var;
109+
$var->refArr = [$ref];
110+
$var->circularArr = [$var];
111+
$varDumper = $this->getVarDumper($var);
112+
$varId = strval(spl_object_id($var));
113+
$refId = strval(spl_object_id($ref));
114+
$dump = <<<EOT
115+
stdClass#{$varId}
116+
public scalar string VAR (length=3)
117+
public ref stdClass#{$refId}
118+
public scalar string REF (length=3)
119+
public circular stdClass#{$refId} (circular reference #{$refId})
120+
public var stdClass#{$varId} (circular reference #{$varId})
121+
public circular stdClass#{$varId} (circular reference #{$varId})
122+
public refArr array (size=1)
123+
0 => stdClass#{$refId} (circular reference #{$refId})
124+
public circularArr array (size=1)
125+
0 => stdClass#{$varId} (circular reference #{$varId})
126+
EOT;
127+
$this->assertSame(
128+
$dump,
129+
$varDumper->writer()->__toString()
130+
);
131+
}
121132

122133
public function testAnonClass(): void
123134
{

0 commit comments

Comments
 (0)