Req #78683 [Opn->Nab]: [ Feature ] Add function to safely print nested objects / arrays in Exceptions

From: Date: Fri, 18 Oct 2019 11:38:25 +0000
Subject: Req #78683 [Opn->Nab]: [ Feature ] Add function to safely print nested objects / arrays in Exceptions
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-223244@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=78683&edit=1

 ID:                 78683
 Updated by:         requinix@php.net
 Reported by:        6562680 at gmail dot com
 Summary:            [ Feature ] Add function to safely print nested
                     objects / arrays in Exceptions
-Status:             Open
+Status:             Not a bug
 Type:               Feature/Change Request
 Package:            *General Issues
 Operating System:   Win10
 PHP Version:        7.3.10
 Block user comment: N
 Private report:     N

 New Comment:

print_r
var_dump
var_export
gettype
get_class
json_encode
serialize

This is a bug tracker. It is not suitable for Twitter rants.


Previous Comments:
------------------------------------------------------------------------
[2019-10-18 08:08:03] 6562680 at gmail dot com

Description:
------------
See the example

Usually it looks like exception with error message like "Something wents wrong", instead
of "your data is incorrect: {data}"

For example - in JS console you can print "anything". Every object has some string
representation. Its not realization, its only debug/output stuff.

Every person who i had asked answers like "you can extend exception to solve" - my answer
is "i can solve the problem, i want FAST AND COMFORTABLE solve the issue". I can solve it
adding argument with extending, adding Exceptions folder, there will be at least 20 lines of code,
then everywhere i should "use" that damn exception, remembmer about this dependency, and
remember that i should be inside the module. Of course i can create own function in own library or
file, so i need write composer json, and everybody should remember to composer install, know how
dependencies works and in fully example - know dependency injection at least.

Am using something like that, there is buggy, dev realization of course
```
// printval
public function printval($data) // : mixed
{
  if (is_null($data)) return 'null';
  if (false === $data) return 'false';
  if (true === $data) return 'true';
  if (is_float($var) && is_nan($var)) return 'NaN';
  if (is_scalar($var) && (! (is_bool($var) || is_nan($var) || ('' === $var))))
return $data;

  if (is_array($data)) {
    array_walk($data, function (&$item) {
      $item = $this->printval($item); // ! recursion
    });
    
    // some serialization or cutted one
    return $cut_serialized;
  }

  if (is_resource($h) || 'resource (closed)' === gettype($h)) {
    return '{ Resource #' . intval($data) . ' }';
  }
  
  if (is_string($var) || is_numeric($var) || (is_object($var) && method_exists($var,
'__toString'))) {
    return strval($data);
  }

  if (is_object($data)) {
    // reduce class length building it like scissors regarding @maxlen attribute or const
    // Hello/World/Test/Foo/Bar -> Hello/.../Bar -> Hello/World/.../Bar ->
Hello/World/.../Foo/Bar
   
    return $cut;
  }
  
  return strval($data);
}
```

Test script:
---------------
class Someclass
{
  protected $data;

  public function hasSomething($by) : bool
  {
    if (is_string($by)) return isset($this->data[ $by ]);
    elseif (is_object($by)) return $this->SOME_CHECK_METHOD_RETURNS_BOOL($by);
  }

  public function hasSomethingOrFail($by)
  {
    if (! $this->hasSomething($by)) {
      throw new \RuntimeException('Bad: ' . EXPECTED_FUNC($by));
    }
  }
}

Expected result:
----------------
function printval() {} // logval(), lognow() or something
function hash() // creates hash for variable depending on its type automatically, allows me to build
array with hash() keys to unique or merge records without tonns of foreachs.


Actual result:
--------------
There's no function like that.
You write it as you want, you extending exceptions, you should carry damn dependencies even if your
project should contain 1 file with simple logic.


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=78683&edit=1


Thread (16 messages)

« previous php.bugs (#223244) next »