Skip to content

Commit 8f83ca4

Browse files
authored
[Optimization] Add contract "HasDebugInfo" (#23)
* add contract "HasDebugInfo" * up * fix statanalyse * separate testsuites * fix doc * up version
1 parent 9182915 commit 8f83ca4

File tree

15 files changed

+114
-57
lines changed

15 files changed

+114
-57
lines changed

Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ ifneq ("$(wildcard .env)","")
33
export
44
endif
55

6+
.PHONY: docs
7+
68
env:
79
echo "MR_LINTER_GITHUB_HTTP_TOKEN=token\nMR_LINTER_GITLAB_HTTP_TOKEN=token" > .env
810

@@ -48,3 +50,7 @@ docker-pub-try:
4850
-e MR_LINTER_GITHUB_HTTP_TOKEN=${TOKEN} \
4951
-v "${PWD}/.mr-linter.json:/app/.mr-linter.json:ro" \
5052
artarts36/merge-request-linter:${MR_LINTER_VERSION} lint
53+
54+
docs:
55+
php docs/Builder/build_rules.php
56+
php docs/Builder/build_config_json_schema.php

README.MD

+10-11
Original file line numberDiff line numberDiff line change
@@ -168,17 +168,16 @@ credentials:
168168

169169
## Development Commands
170170

171-
| Command | Description |
172-
|-----------------------------------|---------------------------------------|
173-
| composer test | Run tests (via PHPUnit) |
174-
| composer lint | Run lint (via PHPCsFixer) |
175-
| composer doc-rules | Build rules doc page |
176-
| make env | Add .env file |
177-
| make try MR_ID=10 | Run MR-Linter on really pull request |
178-
| make try-gitlab MR_ID=10 | Run MR-Linter on really merge request |
179-
| composer build-config-json-schema | Build JSON Schema for config |
180-
| composer deptrac | Run deptrac |
181-
| composer check | Run test, lint, stat-analyse, deptrac |
171+
| Command | Description |
172+
|-----------------------------------|--------------------------------------------|
173+
| composer test | Run tests (via PHPUnit) |
174+
| composer lint | Run lint (via PHPCsFixer) |
175+
| composer deptrac | Run deptrac |
176+
| composer check | Run test, lint, stat-analyse, deptrac |
177+
| make docs | Build docs (rule page, Config JSON Schema) |
178+
| make env | Add .env file |
179+
| make try MR_ID=10 | Run MR-Linter on really pull request |
180+
| make try-gitlab MR_ID=10 | Run MR-Linter on really merge request |
182181

183182
## Console output example
184183

composer.json

-6
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@
5353
"mr-lint": [
5454
"./bin/mr-linter lint"
5555
],
56-
"doc-rules": [
57-
"php docs/Builder/build_rules.php"
58-
],
59-
"build-config-json-schema": [
60-
"php docs/Builder/build_config_json_schema.php"
61-
],
6256
"deptrac": [
6357
"./vendor/bin/deptrac"
6458
],

docs/custom_rule.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface Rule
1212
/**
1313
* Get rule name.
1414
*/
15-
public static function getName(): string;
15+
public function getName(): string;
1616

1717
/**
1818
* Lint "merge requests" by specific rules
@@ -39,7 +39,7 @@ use ArtARTs36\MergeRequestLinter\Contracts\Rule\Rule;use ArtARTs36\MergeRequestL
3939

4040
class ExampleRule implements Rule
4141
{
42-
public static function getName(): string
42+
public function getName(): string
4343
{
4444
return "@custom-rules/example_rule";
4545
}

phpunit.xml

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
</coverage>
1818
<testsuites>
1919
<testsuite name="Unit">
20-
<directory suffix="Test.php">./tests</directory>
20+
<directory suffix="Test.php">./tests/Unit</directory>
21+
</testsuite>
22+
<testsuite name="Feature">
23+
<directory suffix="Test.php">./tests/Feature</directory>
2124
</testsuite>
2225
</testsuites>
2326
</phpunit>

src/Console/Application/Application.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
class Application extends \Symfony\Component\Console\Application
1414
{
15-
public const VERSION = '0.7.5';
15+
public const VERSION = '0.7.6';
1616

1717
public function __construct(
1818
private readonly MetricManager $metrics,

src/Contracts/DataStructure/Collection.php

-5
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,4 @@ public function containsAll(iterable $values): bool;
3434
* Check collection is empty.
3535
*/
3636
public function isEmpty(): bool;
37-
38-
/**
39-
* Convert Collection to debug view string.
40-
*/
41-
public function debugView(): string;
4237
}

src/Contracts/DataStructure/Map.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ArtARTs36\MergeRequestLinter\Contracts\DataStructure;
44

5+
use ArtARTs36\MergeRequestLinter\Contracts\HasDebugInfo;
56
use ArtARTs36\MergeRequestLinter\Support\DataStructure\Arrayee;
67

78
/**
@@ -10,7 +11,7 @@
1011
* @template V
1112
* @template-extends Collection<K, V>
1213
*/
13-
interface Map extends Collection
14+
interface Map extends Collection, HasDebugInfo
1415
{
1516
/**
1617
* Get value by key.

src/Contracts/HasDebugInfo.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace ArtARTs36\MergeRequestLinter\Contracts;
4+
5+
/**
6+
* Interface for debug objects.
7+
*/
8+
interface HasDebugInfo
9+
{
10+
/**
11+
* Get debug info.
12+
* @return array<string, mixed>
13+
*/
14+
public function __debugInfo(): array;
15+
}

src/IO/Console/ConsolePrinter.php

+43-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace ArtARTs36\MergeRequestLinter\IO\Console;
44

5-
use ArtARTs36\MergeRequestLinter\Contracts\DataStructure\Collection;
5+
use ArtARTs36\MergeRequestLinter\Contracts\HasDebugInfo;
66
use ArtARTs36\MergeRequestLinter\Contracts\IO\Printer;
77
use Symfony\Component\Console\Helper\Table;
88
use Symfony\Component\Console\Helper\TableSeparator;
@@ -61,7 +61,7 @@ private function separateProps(array $props): array
6161
}
6262

6363
/**
64-
* @param array<mixed> $props
64+
* @param array<array<string>> $props
6565
*/
6666
private function buildProps(object $object, array &$props, string $prefix): void
6767
{
@@ -72,21 +72,53 @@ private function buildProps(object $object, array &$props, string $prefix): void
7272
$k = $prefix . '.' . $key;
7373
}
7474

75-
if (is_bool($value)) {
76-
$props[] = [$k, $value ? 'true' : 'false'];
77-
} elseif ($value instanceof Collection) {
78-
$props[] = [$k, sprintf("- Count: %s \n- %s", $value->count(), $value->debugView())];
79-
} elseif (is_string($value) || $value instanceof \Stringable) {
80-
$props[] = [$k, sprintf('"%s"', $value)];
81-
} elseif (is_scalar($value)) {
82-
$props[] = [$k, $value];
83-
} elseif (is_object($value)) {
75+
if ($value instanceof HasDebugInfo) {
76+
$debugInfo = '';
77+
$debugBag = $value->__debugInfo();
78+
79+
foreach ($debugBag as $field => $debugVal) {
80+
$debugInfo .= sprintf(
81+
'- %s: %s',
82+
$field,
83+
$this->prepareValue($debugVal),
84+
);
85+
86+
if (next($debugBag) !== false) {
87+
$debugInfo .= "\n";
88+
}
89+
}
90+
91+
$props[] = [$k, $debugInfo];
92+
} elseif (is_object($value) && ! $value instanceof \Stringable) {
8493
$prefix = $k;
8594

8695
$this->buildProps($value, $props, $prefix);
96+
} else {
97+
$props[] = [$k, $this->prepareValue($value)];
8798
}
8899

89100
$prefix = '';
90101
}
91102
}
103+
104+
private function prepareValue(mixed $value): string
105+
{
106+
if (is_array($value)) {
107+
return ($json = json_encode($value)) ? $json : '';
108+
}
109+
110+
if (is_bool($value)) {
111+
return $value ? 'true' : 'false';
112+
}
113+
114+
if (is_string($value) || $value instanceof \Stringable) {
115+
return sprintf('"%s"', $value);
116+
}
117+
118+
if ($value === null) {
119+
return 'null';
120+
}
121+
122+
return '' . $value;
123+
}
92124
}

src/Request/Data/Diff/Diff.php

-7
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,4 @@ public function hasChangeByContentContainsByRegex(string $content): bool
3030

3131
return false;
3232
}
33-
34-
public function debugView(): string
35-
{
36-
$json = json_encode($this->items);
37-
38-
return $json === false ? '' : $json;
39-
}
4033
}

src/Support/DataStructure/ArrayMap.php

+8
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,12 @@ public function debugView(): string
157157

158158
return $json === false ? '' : $json;
159159
}
160+
161+
public function __debugInfo(): array
162+
{
163+
return [
164+
'count' => $this->count(),
165+
'items' => $this->items,
166+
];
167+
}
160168
}

src/Support/DataStructure/Arrayee.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ArtARTs36\MergeRequestLinter\Support\DataStructure;
44

55
use ArtARTs36\MergeRequestLinter\Contracts\DataStructure\Collection;
6+
use ArtARTs36\MergeRequestLinter\Contracts\HasDebugInfo;
67
use ArtARTs36\MergeRequestLinter\Support\DataStructure\Traits\ContainsAll;
78
use ArtARTs36\MergeRequestLinter\Support\DataStructure\Traits\CountProxy;
89
use Traversable;
@@ -12,7 +13,7 @@
1213
* @template V
1314
* @template-implements Collection<K, V>
1415
*/
15-
class Arrayee implements Collection
16+
class Arrayee implements Collection, HasDebugInfo
1617
{
1718
use CountProxy;
1819
use ContainsAll;
@@ -66,11 +67,6 @@ public function implode(string $sep): string
6667
return implode($sep, $this->items);
6768
}
6869

69-
public function debugView(): string
70-
{
71-
return "[" . $this->implode(', ') . "]";
72-
}
73-
7470
/**
7571
* @param callable(V): mixed $mapper
7672
* @return array<mixed>
@@ -90,4 +86,12 @@ public function merge(Arrayee|array $that): Arrayee
9086

9187
return new Arrayee(array_merge($this->items, $items));
9288
}
89+
90+
public function __debugInfo(): array
91+
{
92+
return [
93+
'count' => $this->count(),
94+
'items' => $this->items,
95+
];
96+
}
9397
}

src/Support/DataStructure/MapProxy.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,15 @@ public function keys(): Arrayee
7878
return $this->retrieveMap()->keys();
7979
}
8080

81-
public function debugView(): string
81+
public function __debugInfo(): array
8282
{
8383
if ($this->map === null) {
84-
return 'MapProxy items not loaded';
84+
return [
85+
'count' => null,
86+
'items' => 'Not loaded',
87+
];
8588
}
8689

87-
return 'Items: ' . $this->map->debugView();
90+
return $this->map->__debugInfo();
8891
}
8992
}

src/Support/DataStructure/Set.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
namespace ArtARTs36\MergeRequestLinter\Support\DataStructure;
44

55
use ArtARTs36\MergeRequestLinter\Contracts\DataStructure\Collection;
6+
use ArtARTs36\MergeRequestLinter\Contracts\HasDebugInfo;
67
use ArtARTs36\MergeRequestLinter\Support\ArrayKeyIterator;
78
use ArtARTs36\MergeRequestLinter\Support\DataStructure\Traits\CountProxy;
89

910
/**
1011
* @template V
1112
* @template-implements Collection<int, V>
1213
*/
13-
class Set implements Collection
14+
class Set implements Collection, HasDebugInfo
1415
{
1516
use CountProxy;
1617

@@ -123,8 +124,11 @@ public function first()
123124
return array_key_first($this->items);
124125
}
125126

126-
public function debugView(): string
127+
public function __debugInfo(): array
127128
{
128-
return "[" . $this->implode(', ') . "]";
129+
return [
130+
'count' => $this->count(),
131+
'items' => array_keys($this->items),
132+
];
129133
}
130134
}

0 commit comments

Comments
 (0)