Skip to content

Commit

Permalink
[VarDumper] Add missing return types
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-lass authored and nicolas-grekas committed Apr 14, 2023
1 parent cb21bc9 commit 5767c81
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 10 deletions.
3 changes: 3 additions & 0 deletions Caster/ClassStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public function __construct(string $identifier, callable|array|string $callable
}
}

/**
* @return mixed
*/
public static function wrapCallable(mixed $callable)
{
if (\is_object($callable) || !\is_callable($callable)) {
Expand Down
3 changes: 3 additions & 0 deletions Caster/ExceptionCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, boo
return $a;
}

/**
* @return array
*/
public static function castFlattenException(FlattenException $e, array $a, Stub $stub, bool $isNested)
{
if ($isNested) {
Expand Down
2 changes: 1 addition & 1 deletion Caster/LinkStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __construct(string $label, int $line = 0, string $href = null)
}
}

private function getComposerRoot(string $file, bool &$inVendor)
private function getComposerRoot(string $file, bool &$inVendor): string|false
{
if (!isset(self::$vendorRoots)) {
self::$vendorRoots = [];
Expand Down
3 changes: 3 additions & 0 deletions Caster/StubCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public static function castStub(Stub $c, array $a, Stub $stub, bool $isNested)
return $a;
}

/**
* @return array
*/
public static function castCutArray(CutArrayStub $c, array $a, Stub $stub, bool $isNested)
{
return $isNested ? $c->preservedSubset : $a;
Expand Down
2 changes: 1 addition & 1 deletion Cloner/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ private function dumpChildren(DumperInterface $dumper, Cursor $parentCursor, arr
return $hashCut;
}

private function getStub(mixed $item)
private function getStub(mixed $item): mixed
{
if (!$item || !\is_array($item)) {
return $item;
Expand Down
8 changes: 8 additions & 0 deletions Cloner/DumperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ interface DumperInterface
{
/**
* Dumps a scalar value.
*
* @return void
*/
public function dumpScalar(Cursor $cursor, string $type, string|int|float|bool|null $value);

Expand All @@ -29,6 +31,8 @@ public function dumpScalar(Cursor $cursor, string $type, string|int|float|bool|n
* @param string $str The string being dumped
* @param bool $bin Whether $str is UTF-8 or binary encoded
* @param int $cut The number of characters $str has been cut by
*
* @return void
*/
public function dumpString(Cursor $cursor, string $str, bool $bin, int $cut);

Expand All @@ -38,6 +42,8 @@ public function dumpString(Cursor $cursor, string $str, bool $bin, int $cut);
* @param int $type A Cursor::HASH_* const for the type of hash
* @param string|int|null $class The object class, resource type or array count
* @param bool $hasChild When the dump of the hash has child item
*
* @return void
*/
public function enterHash(Cursor $cursor, int $type, string|int|null $class, bool $hasChild);

Expand All @@ -48,6 +54,8 @@ public function enterHash(Cursor $cursor, int $type, string|int|null $class, boo
* @param string|int|null $class The object class, resource type or array count
* @param bool $hasChild When the dump of the hash has child item
* @param int $cut The number of items the hash has been cut by
*
* @return void
*/
public function leaveHash(Cursor $cursor, int $type, string|int|null $class, bool $hasChild, int $cut);
}
2 changes: 1 addition & 1 deletion Dumper/CliDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ private function isWindowsTrueColor(): bool
return $result;
}

private function getSourceLink(string $file, int $line)
private function getSourceLink(string $file, int $line): string|false
{
if ($fmt = $this->displayOptions['fileLinkFormat']) {
return \is_string($fmt) ? strtr($fmt, ['%f' => $file, '%l' => $line]) : ($fmt->format($file, $line) ?: 'file://'.$file.'#L'.$line);
Expand Down
4 changes: 2 additions & 2 deletions Dumper/ContextualizedDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(DataDumperInterface $wrappedDumper, array $contextPr
}

/**
* @return void
* @return string|null
*/
public function dump(Data $data)
{
Expand All @@ -41,6 +41,6 @@ public function dump(Data $data)
$context[$contextProvider::class] = $contextProvider->getContext();
}

$this->wrappedDumper->dump($data->withContext($context));
return $this->wrappedDumper->dump($data->withContext($context));
}
}
4 changes: 4 additions & 0 deletions Dumper/DataDumperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@
*/
interface DataDumperInterface
{

/**
* @return string|null
*/
public function dump(Data $data);
}
28 changes: 27 additions & 1 deletion Dumper/HtmlDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,18 @@ public function __construct($output = null, string $charset = null, int $flags =
$this->styles = static::$themes['dark'] ?? self::$themes['dark'];
}

/**
* @return void
*/
public function setStyles(array $styles)
{
$this->headerIsDumped = false;
$this->styles = $styles + $this->styles;
}

/**
* @return void
*/
public function setTheme(string $themeName)
{
if (!isset(static::$themes[$themeName])) {
Expand All @@ -101,6 +107,8 @@ public function setTheme(string $themeName)
* Configures display options.
*
* @param array $displayOptions A map of display options to customize the behavior
*
* @return void
*/
public function setDisplayOptions(array $displayOptions)
{
Expand All @@ -110,6 +118,8 @@ public function setDisplayOptions(array $displayOptions)

/**
* Sets an HTML header that will be dumped once in the output stream.
*
* @return void
*/
public function setDumpHeader(?string $header)
{
Expand All @@ -118,6 +128,8 @@ public function setDumpHeader(?string $header)

/**
* Sets an HTML prefix and suffix that will encapse every single dump.
*
* @return void
*/
public function setDumpBoundaries(string $prefix, string $suffix)
{
Expand All @@ -136,6 +148,8 @@ public function dump(Data $data, $output = null, array $extraDisplayOptions = []

/**
* Dumps the HTML header.
*
* @return string
*/
protected function getDumpHeader()
{
Expand Down Expand Up @@ -773,6 +787,9 @@ function showCurrent(state)
return $this->dumpHeader = preg_replace('/\s+/', ' ', $line).'</style>'.$this->dumpHeader;
}

/**
* @return void
*/
public function dumpString(Cursor $cursor, string $str, bool $bin, int $cut)
{
if ('' === $str && isset($cursor->attr['img-data'], $cursor->attr['content-type'])) {
Expand All @@ -788,6 +805,9 @@ public function dumpString(Cursor $cursor, string $str, bool $bin, int $cut)
}
}

/**
* @return void
*/
public function enterHash(Cursor $cursor, int $type, string|int|null $class, bool $hasChild)
{
if (Cursor::HASH_OBJECT === $type) {
Expand Down Expand Up @@ -816,6 +836,9 @@ public function enterHash(Cursor $cursor, int $type, string|int|null $class, boo
}
}

/**
* @return void
*/
public function leaveHash(Cursor $cursor, int $type, string|int|null $class, bool $hasChild, int $cut)
{
$this->dumpEllipsis($cursor, $hasChild, $cut);
Expand Down Expand Up @@ -923,6 +946,9 @@ protected function style(string $style, string $value, array $attr = []): string
return $v;
}

/**
* @return void
*/
protected function dumpLine(int $depth, bool $endOfValue = false)
{
if (-1 === $this->lastDepth) {
Expand Down Expand Up @@ -950,7 +976,7 @@ protected function dumpLine(int $depth, bool $endOfValue = false)
AbstractDumper::dumpLine($depth);
}

private function getSourceLink(string $file, int $line)
private function getSourceLink(string $file, int $line): string|false
{
$options = $this->extraDisplayOptions + $this->displayOptions;

Expand Down
6 changes: 4 additions & 2 deletions Dumper/ServerDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ public function getContextProviders(): array
}

/**
* @return void
* @return string|null
*/
public function dump(Data $data)
{
if (!$this->connection->write($data) && $this->wrappedDumper) {
$this->wrappedDumper->dump($data);
return $this->wrappedDumper->dump($data);
}

return null;
}
}
4 changes: 2 additions & 2 deletions Server/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ public function write(Data $data): bool
}

/**
* @psalm-return false|resource
* @return resource|null
*/
private function createSocket()
{
set_error_handler(fn () => true);
try {
return stream_socket_client($this->host, $errno, $errstr, 3, \STREAM_CLIENT_CONNECT | \STREAM_CLIENT_ASYNC_CONNECT);
return stream_socket_client($this->host, $errno, $errstr, 3, \STREAM_CLIENT_CONNECT | \STREAM_CLIENT_ASYNC_CONNECT) ?: null;
} finally {
restore_error_handler();
}
Expand Down
2 changes: 2 additions & 0 deletions VarDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class VarDumper

/**
* @param string|null $label
*
* @return mixed
*/
public static function dump(mixed $var/* , string $label = null */)
{
Expand Down

0 comments on commit 5767c81

Please sign in to comment.