Req #78683 [NEW]: [ Feature ] Add function to safely print nested objects / arrays in Exceptions

From: Date: Fri, 18 Oct 2019 08:08:03 +0000
Subject: Req #78683 [NEW]: [ Feature ] Add function to safely print nested objects / arrays in Exceptions
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-223233@lists.php.net to get a copy of this message
From:             6562680 at gmail dot com
Operating system: Win10
PHP version:      7.3.10
Package:          *General Issues
Bug Type:         Feature/Change Request
Bug description:[ Feature ] Add function to safely print nested objects / arrays in Exceptions

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 bug report at https://bugs.php.net/bug.php?id=78683&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=78683&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=78683&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=78683&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=78683&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=78683&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=78683&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=78683&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=78683&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=78683&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=78683&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=78683&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=78683&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=78683&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=78683&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=78683&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=78683&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=78683&r=mysqlcfg


Thread (16 messages)

« previous php.bugs (#223233) next »