Skip to content

Commit

Permalink
coding standards & unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bkdotcom committed Oct 25, 2024
1 parent 1548713 commit bee6821
Show file tree
Hide file tree
Showing 24 changed files with 621 additions and 155 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"psr/simple-cache": ">=1.0",
"slim/slim": "^2.0",
"squizlabs/php_codesniffer": "^3.6",
"swiftmailer/swiftmailer": "^5.0",
"swiftmailer/swiftmailer": ">=5.0",
"symfony/http-foundation": ">=2.8"
},
"scripts": {
Expand Down
5 changes: 5 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@
<directory suffix=".php">src</directory>
</include>
<exclude>
<file>src/Debug/Collector/Debug/Pdo/CompatTrait.php</file>
<file>src/Debug/Collector/Doctrine/LoggerCompatTrait_legacy.php</file>
<file>src/Debug/Collector/Doctrine/LoggerCompatTrait.php</file>
<file>src/Debug/Collector/MonologHandler/CompatTrait.php</file>
<file>src/Debug/Collector/Pdo/CompatTrait.php</file>
<file>src/Debug/Collector/SimpleCache/CompatTrait.php</file>
<file>src/Debug/Dump/charData.php</file>
<directory>src/Debug/Framework</directory>
<directory>src/Debug/node_modules</directory>
Expand Down
37 changes: 24 additions & 13 deletions src/Backtrace/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,7 @@ public static function getFileLines($file, $start = null, $length = null)
private static function findEvalCode(array $backtrace, $index)
{
$backtrace = \array_slice($backtrace, $index);
$lines = false;
foreach ($backtrace as $frame) {
if (!isset($frame['function'])) {
break;
}
if ($frame['function'] !== 'eval') {
continue;
}
$lines = isset($frame['args'][0])
? $frame['args'][0]
: false;
break;
}
$lines = self::findEvalCodeLines($backtrace);
if ($lines === false) {
return false;
}
Expand All @@ -111,6 +99,29 @@ private static function findEvalCode(array $backtrace, $index)
return \array_values($lines);
}

/**
* find eval frame and return first arg (the eval'd code)
*
* @param array $frames backtrace frames
*
* @return string|false
*/
private static function findEvalCodeLines($frames)
{
foreach ($frames as $frame) {
if (!isset($frame['function'])) {
return false;
}
if ($frame['function'] !== 'eval') {
continue;
}
break;
}
return isset($frame['args'][0])
? $frame['args'][0]
: false;
}

/**
* "slice" lines of code
*
Expand Down
10 changes: 1 addition & 9 deletions src/Debug/Abstraction/Object/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use bdk\Debug\Abstraction\Object\Properties;
use bdk\PubSub\ValueStore;
use ReflectionClass;
use ReflectionEnum;

/**
* Abstracter: Gather class definition info common across all instances
Expand Down Expand Up @@ -322,14 +321,7 @@ protected function getInterfaces(ReflectionClass $reflector)
}
$remove = \array_unique($remove);
$interfaces = \array_diff_key($interfaces, \array_flip($remove));
// remove values... array_diff complains about array to string conversion
foreach ($remove as $classname) {
$key = \array_search($classname, $interfaces, true);
if ($key !== false) {
unset($interfaces[$key]);
}
}
return $interfaces;
return $this->debug->arrayUtil->diffStrict($interfaces, $remove);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Debug/Abstraction/Object/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ private function onStartMysqli(Abstraction $abs)
if so, don't collect property values
*/
$haveError = false;
\set_error_handler(static function ($errno, $errstr) use (&$haveError) {
\set_error_handler(static function () use (&$haveError) {
$haveError = true;
return true;
}, E_ALL);
try {
$mysqli = $abs->getSubject();
Expand Down
23 changes: 18 additions & 5 deletions src/Debug/Collector/Doctrine/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,23 @@ private function buildDsn()
'password' => null,
'user' => null,
), $this->params);
$parts = $this->paramsToUrlParts($params);
$dsn = (string) UriUtil::fromParsed($parts);
if ($parts['path'] === ':memory:') {
$dsn = \str_replace('/localhost', '/', $dsn);
}
return $dsn;
}

/**
* Convert params to url parts
*
* @param array $params Connection params
*
* @return array
*/
private function paramsToUrlParts(array $params)
{
$map = array(
'dbname' => 'path',
'driver' => 'scheme',
Expand All @@ -157,11 +174,7 @@ private function buildDsn()
$parts['path'] = ':memory:';
}
$parts['scheme'] = \str_replace('_', '-', $parts['scheme']);
$dsn = (string) UriUtil::fromParsed($parts);
if ($params['memory']) {
$dsn = \str_replace('/localhost', '/', $dsn);
}
return $dsn;
return $parts;
}

/**
Expand Down
11 changes: 2 additions & 9 deletions src/Debug/Collector/MySqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,7 @@ public function release_savepoint($name)
$this->debug->warn($this->error);
return $return;
}
$index = \array_search($name, $this->savePoints, true);
if ($index !== false) {
unset($this->savePoints[$index]);
$this->savePoints = \array_values($this->savePoints);
}
$this->savePoints = $this->debug->arrayUtil->diffStrict($this->savePoints, [$name]);
if (PHP_VERSION_ID < 70000) {
$this->savePoints[] = $name;
}
Expand Down Expand Up @@ -237,10 +233,7 @@ public function savepoint($name)
$this->debug->warn($this->error);
return $return;
}
$index = \array_search($name, $this->savePoints, true);
if ($index !== false) {
\array_splice($this->savePoints, $index, 1);
}
$this->savePoints = $this->debug->arrayUtil->diffStrict($this->savePoints, [$name]);
$this->savePoints[] = $name;
$this->debug->info('savepoint', $name);
return $return;
Expand Down
10 changes: 8 additions & 2 deletions src/Debug/Collector/SwiftMailerLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@
/**
* A SwiftMailer adapter
*/
class SwiftMailerLogger implements Swift_Events_CommandListener, Swift_Events_ResponseListener, Swift_Events_SendListener, Swift_Events_TransportChangeListener, Swift_Events_TransportExceptionListener, Swift_Plugins_Logger
class SwiftMailerLogger implements
Swift_Events_CommandListener,
Swift_Events_ResponseListener,
Swift_Events_SendListener,
Swift_Events_TransportChangeListener,
Swift_Events_TransportExceptionListener,
Swift_Plugins_Logger
{
/** @var list<string> */
protected $messages = array();
Expand All @@ -54,7 +60,7 @@ class SwiftMailerLogger implements Swift_Events_CommandListener, Swift_Events_Re
/**
* Constructor
*
* @param Debu|null $debug (optional) Specify PHPDebugConsole instance
* @param Debug|null $debug (optional) Specify PHPDebugConsole instance
* if not passed, will create Slim channel on singleton instance
* if root channel is specified, will create a SwiftMailer channel
*
Expand Down
64 changes: 48 additions & 16 deletions src/Debug/Plugin/AssertSettingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,69 @@ trait AssertSettingTrait
/**
* Assert ini setting is/is-not specified value
*
* @param array<string,mixed> $setting setting name, "type", comparison value, operator
* Multiple method signatures
* assertSetting($name[, $valCompare[, $opts]])
* assertSetting($name[, $opts])
* assertSetting($opts)
*
* @param string $name setting name (or array of options)
* @param mixed $valCompare comparison value
* @param array<string,mixed> $opts comparison operator, filter, message, etc
*
* @return void
*
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
*/
protected function assertSetting(array $setting)
protected function assertSetting($name, $valCompare = true, array $opts = array())
{
$setting = $this->assertSettingPrep(\array_merge(array(
'addParams' => array(),
'filter' => FILTER_DEFAULT, // filter applied if getting value from ini val
'msg' => '', // (optional) message displayed if assertion fails
'name' => '', // ini name
'operator' => '==',
'valActual' => '__use_ini_val__',
'valCompare' => true,
), $setting));
$opts = $this->assertSettingOpts(\func_get_args());
/** @var array{0:bool,1:string} */
$params = [
$this->debug->stringUtil->compare($setting['valActual'], $setting['valCompare'], $setting['operator']),
$setting['name']
? '%c' . $setting['name'] . '%c: ' . $setting['msg']
: $setting['msg'],
$this->debug->stringUtil->compare($opts['valActual'], $opts['valCompare'], $opts['operator']),
$opts['name']
? '%c' . $opts['name'] . '%c: ' . $opts['msg']
: $opts['msg'],
];
$cCount = \substr_count($params[1], '%c');
for ($i = 0; $i < $cCount; $i += 2) {
$params[] = 'font-family:monospace;';
$params[] = '';
}
$params = \array_merge($params, $setting['addParams']);
$params = \array_merge($params, $opts['addParams']);
\call_user_func_array([$this->debug, 'assert'], $params);
}

/**
* Get the assert options from passed arguments
*
* @param array $args Arguments passed to assertSetting
*
* @return array<string,mixed>
*/
private function assertSettingOpts(array $args)
{
$optsDefault = array(
'addParams' => array(),
'filter' => FILTER_DEFAULT, // filter applied if getting value from ini val
'msg' => '', // (optional) message displayed if assertion fails
'name' => '', // ini name
'operator' => '==',
'valActual' => '__use_ini_val__',
'valCompare' => true,
);
$opts = array();
$argNames = array('name', 'valCompare', 'opts');
foreach ($args as $i => $arg) {
if (\is_array($arg)) {
$opts = \array_merge($opts, $arg);
break;
}
$opts[$argNames[$i]] = $arg;
}
$opts = \array_merge($optsDefault, $opts);
return $this->assertSettingPrep($opts);
}

/**
* Merge in default valActual & msg values
*
Expand Down
Loading

0 comments on commit bee6821

Please sign in to comment.