Skip to content

Commit

Permalink
Backtrace: error when exception trace empty
Browse files Browse the repository at this point in the history
New Container Utility class
ServerRequest::parseStr cast $str to string
  • Loading branch information
bkdotcom committed Oct 25, 2023
1 parent 9c82a05 commit bf9fc86
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 74 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ $monolog->critical('all your base are belong to them');

![No Dependencies](https://img.shields.io/badge/dependencies-none-333333.svg)
![Supported PHP versions](https://img.shields.io/static/v1?label=PHP&message=5.4%20-%208.2&color=blue)
![Build Status](https://img.shields.io/github/actions/workflow/status/bkdotcom/PHPDebugConsole/phpunit.yml.svg?logo=github)
![Build Status](https://img.shields.io/github/actions/workflow/status/bkdotcom/PHPDebugConsole/phpunit.yml.svg?branch=master&logo=github)
[![Codacy Score](https://img.shields.io/codacy/grade/e950849edfd9463b993386080d39875e/master.svg?logo=codacy)](https://app.codacy.com/gh/bkdotcom/PHPDebugConsole/dashboard)
[![Maintainability](https://img.shields.io/codeclimate/maintainability/bkdotcom/PHPDebugConsole.svg?logo=codeclimate)](https://codeclimate.com/github/bkdotcom/PHPDebugConsole)
[![Coverage](https://img.shields.io/codeclimate/coverage-letter/bkdotcom/PHPDebugConsole.svg?logo=codeclimate)](https://codeclimate.com/github/bkdotcom/PHPDebugConsole)
Expand Down
2 changes: 1 addition & 1 deletion src/Backtrace/Backtrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ private static function getExceptionTrace($exception)
'file' => $exception->getFile(),
'line' => $exception->getLine(),
);
if (\array_intersect_assoc($fileLine, \reset($trace)) !== $fileLine) {
if (\array_intersect_assoc($fileLine, \reset($trace) ?: array()) !== $fileLine) {
\array_unshift($trace, $fileLine);
}
return $trace;
Expand Down
74 changes: 74 additions & 0 deletions src/Container/Utility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/**
* This file is part of PHPDebugConsole
*
* @package PHPDebugConsole
* @author Brad Kent <bkfake-github@yahoo.com>
* @license http://opensource.org/licenses/MIT MIT
* @copyright 2014-2022 Brad Kent
* @version v3.1
*/

namespace bdk\Container;

use bdk\Container;
use bdk\Container\ServiceProviderInterface;
use InvalidArgumentException;

/**
* Container utilities
*/
class Utility
{
/**
* Get the container's raw values
*
* @param Container $container Container instance
*
* @return array
*/
public static function getRawValues(Container $container)
{
$keys = $container->keys();
$return = array();
foreach ($keys as $key) {
$return[$key] = $container->raw($key);
}
return $return;
}

/**
* Get values from Container, ServiceProviderInterface, callable or plain array
*
* @param ServiceProviderInterface|callable|array $val dependency definitions
*
* @return array
*
* @throws InvalidArgumentException
*/
public static function toRawValues($val)
{
if ($val instanceof Container) {
return self::getRawValues($val);
}
if ($val instanceof ServiceProviderInterface) {
$container = new Container();
$container->registerProvider($val);
return self::getRawValues($container);
}
if (\is_callable($val)) {
$container = new Container();
\call_user_func($val, $container);
return self::getRawValues($container);
}
if (\is_array($val)) {
return $val;
}
$msg = 'toRawValues expects Container, ServiceProviderInterface, callable, or key->value array. %s provided';
$type = \is_object($val)
? \get_class($val)
: \gettype($val);
throw new InvalidArgumentException(\sprintf($msg, $type));
}
}
52 changes: 5 additions & 47 deletions src/Debug/AbstractDebug.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use bdk\Container;
use bdk\Container\ServiceProviderInterface;
use bdk\Container\Utility as ContainerUtility;
use bdk\Debug;
use bdk\Debug\LogEntry;
use bdk\Debug\ServiceProvider;
Expand Down Expand Up @@ -70,11 +71,7 @@ public function __construct($cfg)
*/
public function __call($methodName, $args)
{
$logEntry = new LogEntry(
$this,
$methodName,
$args
);
$logEntry = new LogEntry($this, $methodName, $args);
$this->publishBubbleEvent(Debug::EVENT_CUSTOM_METHOD, $logEntry);
if ($logEntry['handled'] !== true) {
$logEntry->setMeta('isCustomMethod', true);
Expand Down Expand Up @@ -208,20 +205,17 @@ public function onConfig(Event $event)
*/
public function onCfgServiceProvider($val)
{
$val = $this->serviceProviderToArray($val);
if (\is_array($val) === false) {
return $val;
}
$rawValues = ContainerUtility::toRawValues($val);
$services = $this->container['services'];
foreach ($val as $k => $v) {
foreach ($rawValues as $k => $v) {
if (\in_array($k, $services, true)) {
$this->serviceContainer[$k] = $v;
unset($val[$k]);
continue;
}
$this->container[$k] = $v;
}
return $val;
return $rawValues;
}

/**
Expand Down Expand Up @@ -400,40 +394,4 @@ private function bootstrapSetInstances($cfg)
$this->rootInstance = $this->parentInstance->rootInstance;
}
}

/**
* Convert serviceProvider to array of name => value
*
* @param ServiceProviderInterface|callable|array $val dependency definitions
*
* @return array
*/
private function serviceProviderToArray($val)
{
$getContainerRawVals = static function (Container $container) {
$keys = $container->keys();
$return = array();
foreach ($keys as $key) {
$return[$key] = $container->raw($key);
}
return $return;
};
if ($val instanceof ServiceProviderInterface) {
/*
convert to array
*/
$containerTmp = new Container();
$containerTmp->registerProvider($val);
return $getContainerRawVals($containerTmp);
}
if (\is_callable($val)) {
/*
convert to array
*/
$containerTmp = new Container();
\call_user_func($val, $containerTmp);
return $getContainerRawVals($containerTmp);
}
return $val;
}
}
50 changes: 25 additions & 25 deletions src/Debug/Utility/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,31 @@ public function __construct($rows = array(), array $meta = array(), Debug $debug
$this->setMeta();
}

/**
* Go through all the "rows" of array to determine what the keys are and their order
*
* @param TableRow[] $rows array of TableRow instance
*
* @return array
*/
public static function colKeys($rows)
{
if (\is_array($rows) === false) {
return array();
}
$colKeys = array();
foreach ($rows as $row) {
if (!$row instanceof TableRow) {
$row = new TableRow($row);
}
$curRowKeys = $row->keys();
if ($curRowKeys !== $colKeys) {
$colKeys = self::colKeysMerge($curRowKeys, $colKeys);
}
}
return $colKeys;
}

/**
* Get table rows
*
Expand Down Expand Up @@ -71,31 +96,6 @@ public function haveRows()
return \is_array($this->rows) && \count($this->rows) > 0;
}

/**
* Go through all the "rows" of array to determine what the keys are and their order
*
* @param TableRow[] $rows array of TableRow instance
*
* @return array
*/
private static function colKeys($rows)
{
if (\is_array($rows) === false) {
return array();
}
$colKeys = array();
foreach ($rows as $row) {
if (!$row instanceof TableRow) {
$row = new TableRow($row);
}
$curRowKeys = $row->keys();
if ($curRowKeys !== $colKeys) {
$colKeys = self::colKeysMerge($curRowKeys, $colKeys);
}
}
return $colKeys;
}

/**
* Merge current row's keys with merged keys
*
Expand Down
1 change: 1 addition & 0 deletions src/HttpMessage/AbstractServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ abstract class AbstractServerRequest extends Request
*/
public static function parseStr($str, $opts = array())
{
$str = (string) $str;
$opts = \array_merge(self::$parseStrOpts, $opts);
$useParseStr = ($opts['convDot'] || \strpos($str, '.') === false)
&& ($opts['convSpace'] || \strpos($str, ' ') === false);
Expand Down
2 changes: 2 additions & 0 deletions tests/Debug/DebugTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace bdk\Test\Debug;

use bdk\Debug;
use bdk\PhpUnitPolyfill\ExpectExceptionTrait;
use bdk\PubSub\Manager as EventManager;

/**
Expand All @@ -25,6 +26,7 @@ public function testNoComposer()

public function testBootstrap()
{
$this->expectException('InvalidArgumentException');
$debug = new Debug(array(
'container' => array(),
'serviceProvider' => 'invalid',
Expand Down

0 comments on commit bf9fc86

Please sign in to comment.