Skip to content

Commit 98114ec

Browse files
committed
Coding standards
1 parent cd5029c commit 98114ec

54 files changed

Lines changed: 1172 additions & 637 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Push WordPress Plugin to SVN
2+
on:
3+
push:
4+
tags:
5+
- "*"
6+
jobs:
7+
tag:
8+
name: New tag
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout
12+
uses: actions/checkout@master
13+
- name: Install PHP
14+
uses: shivammathur/setup-php@v2 # https://github.com/marketplace/actions/setup-php-action
15+
with:
16+
php-version: '5.6'
17+
extensions: iconv, intl, mysqli, oauth
18+
ini-values: error_reporting=-1, memory_limit=512M, post_max_size=256M, xdebug.mode="coverage,debug,develop"
19+
coverage: xdebug #optional
20+
- name: Check Versions
21+
run: |
22+
php -v
23+
php -r 'echo "curl version: " . curl_version()["version"] . "\n";'
24+
composer --version
25+
- name: Validate composer.json and composer.lock
26+
run: composer validate --strict
27+
- name: Cache composer packages
28+
uses: actions/cache@v4
29+
id: composer-cache
30+
with:
31+
path: vendor
32+
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
33+
restore-keys: |
34+
${{ runner.os }}-php-
35+
- name: Install dependencies
36+
run: |
37+
composer install --no-dev --prefer-dist --no-progress
38+
- name: Move plugin file
39+
run: |
40+
mv src/Debug/Framework/WordPress/phpdebugconsole.php phpdebugconsole.php
41+
- name: WordPress Plugin Deploy
42+
uses: 10up/action-wordpress-plugin-deploy@stable
43+
env:
44+
SVN_PASSWORD: ${{ secrets.SVN_PASSWORD }}
45+
SVN_USERNAME: ${{ secrets.SVN_USERNAME }}
46+
SLUG: phpdebugconsole

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
"php -f vendor/bdk/devutil/src/coverageChecker.php -- coverage/clover.xml"
8383
],
8484
"cs" : [
85-
"vendor/bin/phpcs -p -s --colors ./src"
85+
"vendor/bin/phpcs -p -s --colors --ignore=./src/Debug/coverage ./src"
8686
],
8787
"post-update-cmd": [
8888
"bdk\\Debug\\Dev\\ComposerScripts::postUpdate"

src/Backtrace/Backtrace.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public static function getFileLines($file, $start = null, $length = null)
157157
/**
158158
* Convenience method for bdk\Backtrace\Normalizer::normalize()
159159
*
160-
* @param array $backtrace trace/stack to normalize
160+
* @param array $trace trace/stack to normalize
161161
*
162162
* @return array
163163
*/

src/CurlHttpMessage/Factory.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use bdk\CurlHttpMessage\Handler\CurlMulti;
1616
use bdk\CurlHttpMessage\HandlerStack;
1717
use bdk\CurlHttpMessage\Middleware;
18+
use bdk\Debug\Utility\StringUtil;
1819
use bdk\HttpMessage\Request;
1920
use bdk\HttpMessage\Response;
2021
use bdk\HttpMessage\Stream;
@@ -181,23 +182,37 @@ public function withBody(MessageInterface $message, $body)
181182
if ($body === null) {
182183
return $message;
183184
}
184-
$bodyIsEncoded = \is_string($body) || $body instanceof StreamInterface;
185185
$type = $this->inferContentType($message, $body);
186186
if ($type) {
187187
$message = $message->withHeader('Content-Type', $type);
188188
$type = \preg_replace('/;.*$/', '', $type);
189189
}
190-
if ($type === 'application/json' && $bodyIsEncoded === false) {
191-
$body = \json_encode($body, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
192-
} elseif ($type === 'application/x-www-form-urlencoded' && $bodyIsEncoded === false) {
193-
$body = \http_build_query($body);
194-
}
190+
$body = $this->serializeBody($body, $type);
195191
$stream = $body instanceof StreamInterface
196192
? $body
197193
: $this->stream($body);
198194
return $message->withBody($stream);
199195
}
200196

197+
/**
198+
* `json_encode` or `http_build_query` body if necessary
199+
*
200+
* @param mixed $body Message body
201+
* @param string|null $type Content-Type
202+
*
203+
* @return string|StreamInterface
204+
*/
205+
private function serializeBody($body, $type)
206+
{
207+
$bodyIsSerialized = \is_string($body) || $body instanceof StreamInterface;
208+
if ($type === 'application/json' && $bodyIsSerialized === false) {
209+
$body = \json_encode($body, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
210+
} elseif ($type === 'application/x-www-form-urlencoded' && $bodyIsSerialized === false) {
211+
$body = \http_build_query($body);
212+
}
213+
return $body;
214+
}
215+
201216
/**
202217
* Add headers to message
203218
*
@@ -245,7 +260,7 @@ private function inferContentType(MessageInterface $message, $body)
245260
if ($body instanceof StreamInterface) {
246261
$body = (string) $body;
247262
}
248-
if (\is_string($body) && \bdk\Debug\Utility\StringUtil::isJson($body)) {
263+
if (StringUtil::isJson($body)) {
249264
return $this->types['json'];
250265
}
251266
return '';

src/Debug/Abstraction/AbstractString.php

Lines changed: 79 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,9 @@ public function getType($val)
9292
Abstracter::RECURSION => Type::TYPE_RECURSION,
9393
Abstracter::UNDEFINED => Type::TYPE_UNDEFINED,
9494
);
95-
if (isset($debugVals[$val])) {
96-
return [$debugVals[$val], null];
97-
}
98-
if (\is_numeric($val) === false) {
99-
return [Type::TYPE_STRING, $this->getTypeMore($val)];
100-
}
101-
$typeMore = $this->abstracter->type->isTimestamp($val)
102-
? Type::TYPE_TIMESTAMP
103-
: Type::TYPE_STRING_NUMERIC;
104-
return [Type::TYPE_STRING, $typeMore];
95+
return isset($debugVals[$val])
96+
? [$debugVals[$val], null]
97+
: [Type::TYPE_STRING, $this->getTypeMore($val)];
10598
}
10699

107100
/**
@@ -114,26 +107,40 @@ public function getType($val)
114107
*/
115108
private function absFinish(Abstraction $abs)
116109
{
117-
$typeMore = $abs['typeMore'];
118-
if ($abs['brief'] && $typeMore !== Type::TYPE_STRING_BINARY) {
119-
$matches = [];
120-
$maxLen = $abs['maxlen'] > -1
121-
? $abs['maxlen']
122-
: 128;
123-
$regex = '/^([^\r\n]{1,' . $maxLen . '})/';
124-
\preg_match($regex, $abs['value'], $matches);
125-
$abs['value'] = $matches
126-
? $matches[1]
127-
: \substr($abs['value'], 0, $maxLen);
128-
$abs['strlenValue'] = \strlen($abs['value']);
129-
}
110+
$this->trimValueIfBrief($abs);
111+
112+
// cleanup length info
130113
if ($abs['strlen'] === $abs['strlenValue'] && $abs['strlen'] === \strlen($abs['value'])) {
131114
unset($abs['strlen'], $abs['strlenValue']);
132115
}
116+
117+
// remove temporary values
133118
unset($abs['maxlen'], $abs['valueRaw']);
134119
return $abs;
135120
}
136121

122+
/**
123+
* Trim the abstraction value if in brief mode and not binary
124+
*
125+
* @param Abstraction $abs Abstraction to modify
126+
*
127+
* @return void
128+
*/
129+
private function trimValueIfBrief(Abstraction $abs)
130+
{
131+
if (!$abs['brief'] || $abs['typeMore'] === Type::TYPE_STRING_BINARY) {
132+
return;
133+
}
134+
$maxLen = $abs['maxlen'] > -1 ? $abs['maxlen'] : 128;
135+
$matches = [];
136+
$regex = '/^([^\r\n]{1,' . $maxLen . '})/';
137+
\preg_match($regex, $abs['value'], $matches);
138+
$abs['value'] = $matches
139+
? $matches[1]
140+
: \substr($abs['value'], 0, $maxLen);
141+
$abs['strlenValue'] = \strlen($abs['value']);
142+
}
143+
137144
/**
138145
* Get a string abstraction..
139146
*
@@ -312,31 +319,43 @@ private function getAbsSerialized(Abstraction $abs)
312319
* @param string $cat category (ie base64, binary, other)
313320
* @param int $strlen string length
314321
*
315-
* @return int -1 for no limit
322+
* @return int max length value (-1 for no limit)
316323
*/
317324
private function getMaxLen($cat, $strlen)
318325
{
319326
$stringMaxLen = $this->cfg['brief']
320327
? $this->cfg['stringMaxLenBrief']
321328
: $this->cfg['stringMaxLen'];
322-
$maxLen = \array_key_exists($cat, $stringMaxLen)
329+
$maxLen = \array_key_exists($cat, $stringMaxLen)
323330
? $stringMaxLen[$cat]
324331
: $stringMaxLen['other'];
325-
if (\is_array($maxLen) === false) {
326-
return $maxLen !== null
327-
? $maxLen
328-
: -1;
332+
333+
if (\is_array($maxLen)) {
334+
$maxLen = $this->getBreakpointBasedMaxLen($maxLen, $strlen);
329335
}
336+
return $maxLen !== null
337+
? $maxLen
338+
: -1;
339+
}
340+
341+
/**
342+
* Calculate max length based on breakpoints
343+
*
344+
* @param array $breakpoints array of breakpoint => length values
345+
* @param int $strlen string length to check against breakpoints
346+
*
347+
* @return int max length value (-1 for no limit)
348+
*/
349+
private function getBreakpointBasedMaxLen(array $breakpoints, $strlen)
350+
{
330351
$len = -1;
331-
foreach ($maxLen as $breakpoint => $lenNew) {
352+
foreach ($breakpoints as $breakpoint => $lenNew) {
332353
if ($breakpoint > $strlen) {
333354
break;
334355
}
335356
$len = $lenNew;
336357
}
337-
return $len !== null
338-
? $len
339-
: -1;
358+
return $len;
340359
}
341360

342361
/**
@@ -348,23 +367,20 @@ private function getMaxLen($cat, $strlen)
348367
*/
349368
private function getTypeMore($val)
350369
{
351-
$strLen = \strlen($val);
352-
$strLenEncoded = $this->cfg['stringMinLen']['encoded'];
353-
$typeMore = null;
354-
if ($strLenEncoded > -1 && $strLen >= $strLenEncoded) {
355-
$typeMore = $this->getTypeMoreEncoded($val);
356-
}
370+
$typeMore = \is_numeric($val)
371+
? $this->getTypeMoreNumeric($val)
372+
: $this->getTypeMoreEncoded($val);
357373
if ($typeMore) {
358374
return $typeMore;
359375
}
360376
if ($this->debug->utf8->isUtf8($val) === false) {
361377
return Type::TYPE_STRING_BINARY;
362378
}
379+
$strLen = \strlen($val);
363380
$maxlen = $this->getMaxLen('other', $strLen);
364-
if ($maxlen > -1 && $strLen > $maxlen) {
365-
return Type::TYPE_STRING_LONG;
366-
}
367-
return null;
381+
return $maxlen > -1 && $strLen > $maxlen
382+
? Type::TYPE_STRING_LONG
383+
: null;
368384
}
369385

370386
/**
@@ -376,6 +392,12 @@ private function getTypeMore($val)
376392
*/
377393
private function getTypeMoreEncoded($val)
378394
{
395+
$strLen = \strlen($val);
396+
$minLen = $this->cfg['stringMinLen']['encoded'];
397+
if ($minLen < 0 || $strLen < $minLen) {
398+
return null; // not long enough to test
399+
}
400+
379401
if ($this->debug->stringUtil->isBase64Encoded($val)) {
380402
return Type::TYPE_STRING_BASE64;
381403
}
@@ -387,4 +409,18 @@ private function getTypeMoreEncoded($val)
387409
}
388410
return null;
389411
}
412+
413+
/**
414+
* Determine if value is timestamp or plain numeric
415+
*
416+
* @param string $val numeric string value
417+
*
418+
* @return string
419+
*/
420+
private function getTypeMoreNumeric($val)
421+
{
422+
return $this->abstracter->type->isTimestamp($val)
423+
? Type::TYPE_TIMESTAMP
424+
: Type::TYPE_STRING_NUMERIC;
425+
}
390426
}

src/Debug/Abstraction/Object/PropertiesInstance.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ private function addDebug(Abstraction $abs)
9191
private function addDebugWalk(Abstraction $abs)
9292
{
9393
$debugInfo = $abs['debugInfo'];
94-
$keys = \array_keys($abs['properties']);
95-
$properties = \array_map(static function ($info, $name) use ($abs, &$debugInfo) {
94+
$arrayUtil = $this->abstracter->debug->arrayUtil;
95+
$properties = $arrayUtil->mapWithKeys(static function ($info, $name) use ($abs, &$debugInfo) {
9696
if (\array_key_exists($name, $abs['propertyOverrideValues'])) {
9797
// we used override value
9898
return $info;
@@ -109,8 +109,7 @@ private function addDebugWalk(Abstraction $abs)
109109
&& $isInherited;
110110
$info['debugInfoExcluded'] = $isPrivateAncestor === false;
111111
return $info;
112-
}, $abs['properties'], $keys);
113-
$properties = \array_combine($keys, $properties);
112+
}, $abs['properties']);
114113
$abs['debugInfo'] = \array_diff_key($debugInfo, $properties);
115114
return $properties;
116115
}
@@ -265,16 +264,10 @@ private function mergeOverrideValue(array $propInfo, $overrideValue)
265264
private function valueFromReflection(array $propInfo, Abstraction $abs, ReflectionProperty $refProperty)
266265
{
267266
$refProperty->setAccessible(true); // only accessible via reflection
268-
$obj = $abs->getSubject();
269267
if ($propInfo['isVirtual']) {
270-
if (\in_array('get', $propInfo['hooks'], true) === false) {
271-
// virtual property with no getter = write-only
272-
return $propInfo['value']; // undefined
273-
} elseif ($abs['cfgFlags'] & AbstractObject::PROP_VIRTUAL_VALUE_COLLECT) {
274-
return $refProperty->getValue($obj);
275-
}
276-
return Abstracter::NOT_INSPECTED;
268+
return $this->valueFromReflectionVirtual($propInfo, $abs, $refProperty);
277269
}
270+
$obj = $abs->getSubject();
278271
$isInitialized = PHP_VERSION_ID < 70400 || $refProperty->isInitialized($obj);
279272
if ($isInitialized === false) {
280273
return $propInfo['value']; // undefined
@@ -284,4 +277,25 @@ private function valueFromReflection(array $propInfo, Abstraction $abs, Reflecti
284277
}
285278
return $refProperty->getValue($obj);
286279
}
280+
281+
/**
282+
* Obtain property value via `getRawValue` or `getValue`
283+
*
284+
* @param array $propInfo propInfo array
285+
* @param Abstraction $abs Object Abstraction instance
286+
* @param ReflectionProperty $refProperty ReflectionProperty
287+
*
288+
* @return mixed property value
289+
*/
290+
private function valueFromReflectionVirtual(array $propInfo, Abstraction $abs, ReflectionProperty $refProperty)
291+
{
292+
if (\in_array('get', $propInfo['hooks'], true) === false) {
293+
// virtual property with no getter = write-only
294+
return $propInfo['value']; // undefined
295+
} elseif ($abs['cfgFlags'] & AbstractObject::PROP_VIRTUAL_VALUE_COLLECT) {
296+
$obj = $abs->getSubject();
297+
return $refProperty->getValue($obj);
298+
}
299+
return Abstracter::NOT_INSPECTED;
300+
}
287301
}

src/Debug/Collector/DatabaseTrait.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,13 @@ protected function currentDatabase()
7676
*/
7777
private function logRuntime(Debug $debug, $connectionString = null)
7878
{
79+
$currentDatabase = $connectionString
80+
? null
81+
: $this->currentDatabase();
7982
if ($connectionString) {
8083
$debug->log($debug->i18n->trans('db.connection-string'), $connectionString, $debug->meta('redact'));
81-
} elseif ($database = $this->currentDatabase()) {
82-
$debug->log('database', $database);
84+
} elseif ($currentDatabase) {
85+
$debug->log('database', $currentDatabase);
8386
}
8487
$debug->log($this->debug->i18n->trans('runtime.logged-operations') . ': ', $this->statementInfoLogger->getLoggedCount());
8588
$debug->time($this->debug->i18n->trans('runtime.total-time'), $this->statementInfoLogger->getTimeSpent());

0 commit comments

Comments
 (0)