Skip to content

Commit 29515d4

Browse files
committed
csfix
1 parent 5c960dd commit 29515d4

10 files changed

Lines changed: 106 additions & 89 deletions

ecs.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php declare(strict_types=1);
2+
3+
use PhpCsFixer\Fixer\Import\NoUnusedImportsFixer;
4+
use PhpCsFixer\Fixer\Strict\DeclareStrictTypesFixer;
5+
use Symplify\EasyCodingStandard\Config\ECSConfig;
6+
7+
return ECSConfig::configure()
8+
->withPaths([
9+
__DIR__ . '/src',
10+
__DIR__ . '/tests',
11+
12+
])
13+
// ->withRules([
14+
// DeclareStrictTypesFixer::class,
15+
// ])
16+
// ->withPhpCsFixerSets(perCS20: true)
17+
->withPreparedSets(symplify: true, psr12: true, common: true)
18+
->withConfiguredRule(PhpCsFixer\Fixer\Phpdoc\PhpdocLineSpanFixer::class, [
19+
'property' => 'single',
20+
'method' => 'single',
21+
'const' => 'single',
22+
])
23+
->withConfiguredRule(PhpCsFixer\Fixer\Operator\BinaryOperatorSpacesFixer::class, [
24+
'operators' => [
25+
'=>' => 'align',
26+
'=' => 'align'
27+
],
28+
])
29+
->withConfiguredRule(PhpCsFixer\Fixer\Phpdoc\PhpdocLineSpanFixer::class, [
30+
'const' => 'single',
31+
'method' => 'single',
32+
'property' => 'single',
33+
])
34+
->withSkip([
35+
Symplify\CodingStandard\Fixer\ArrayNotation\ArrayOpenerAndCloserNewlineFixer::class,
36+
37+
// <?php declare(strict_types=1); doesn't need to take up three or four lines:
38+
PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer::class,
39+
PhpCsFixer\Fixer\PhpTag\LinebreakAfterOpeningTagFixer::class,
40+
41+
Symplify\CodingStandard\Fixer\ArrayNotation\StandaloneLineInMultilineArrayFixer::class,
42+
Symplify\CodingStandard\Fixer\ArrayNotation\ArrayListItemNewlineFixer::class
43+
])
44+
;

src/MonologStackLogger.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Monolog\Handler\NullHandler;
88
use Monolog\Logger as MonologLogger;
9-
use Psr\Log\LoggerInterface as PsrInterface;
109

1110
/**
1211
* Extends Psr3Logger to provide a monolog-like withName() method.
@@ -24,7 +23,7 @@ public function withName(string $name): static
2423
{
2524
// this works, but requires WrappedPSR3::$logger to be non-private.
2625
// Is there a better way?
27-
$new = clone $this;
26+
$new = clone $this;
2827
$new->logger = $this->logger->withName($name);
2928
$new->parent = $this;
3029
return $new;

src/Psr3StackLogger.php

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Psr\Log\LoggerInterface as PsrInterface;
88
use Psr\Log\LoggerTrait;
99
use Psr\Log\NullLogger;
10-
use function PHPStan\dumpType;
1110

1211
/**
1312
* Implements LoggerInterface by wrapping a PSR3 logger.
@@ -21,7 +20,7 @@ class Psr3StackLogger implements StackLogger
2120
/** @var L */
2221
protected PsrInterface $logger;
2322

24-
/** @var static */
23+
/** @var static */
2524
protected ?StackLogger $parent = null;
2625

2726
/** @var array<mixed> */
@@ -39,23 +38,16 @@ public function getWrapped(): PsrInterface
3938
return $this->logger;
4039
}
4140

42-
/** @param L $logger */
43-
final protected function setWrapped(PsrInterface $logger): void
44-
{
45-
$this->logger = $logger;
46-
}
47-
4841
/**
4942
* Context Handling
5043
*
5144
* @param array<string, mixed> $context
52-
* @return static
5345
*/
5446
#[\Override]
5547
public function withContext(array $context = []): static
5648
{
57-
$child = clone $this;
58-
$child->parent = $this;
49+
$child = clone $this;
50+
$child->parent = $this;
5951
$child->context = $context;
6052
return $child;
6153
}
@@ -71,6 +63,28 @@ public function addContext(array $context): static
7163
return $this;
7264
}
7365

66+
/* PSR3 Implementation, wrapping our wrapped instance. */
67+
68+
#[\Override]
69+
public function log($level, string|\Stringable $message, array $context = []): void
70+
{
71+
assert(is_string($level) || is_int($level));
72+
$context = $this->processContext($context);
73+
$this->logger->log($level, $message, $context);
74+
}
75+
76+
public static function makeNullLogger(): StackLogger
77+
{
78+
$instance = new NullLogger();
79+
return new self($instance);
80+
}
81+
82+
/** @param L $logger */
83+
final protected function setWrapped(PsrInterface $logger): void
84+
{
85+
$this->logger = $logger;
86+
}
87+
7488
/**
7589
* Merges $context on top of the instances accumulated context, and
7690
* processes any callable elements in the final context.
@@ -83,10 +97,7 @@ protected function processContext(array $context = []): array
8397
$context = array_merge($this->mergedContext(), $context);
8498

8599
// handle any callables in final context.
86-
return array_map(
87-
static fn($c): mixed => is_callable($c) ? $c($context) : $c,
88-
$context
89-
);
100+
return array_map(static fn ($c): mixed => is_callable($c) ? $c($context) : $c, $context);
90101
}
91102

92103
/**
@@ -95,30 +106,12 @@ protected function processContext(array $context = []): array
95106
*/
96107
protected function contextToMerge(): array
97108
{
98-
return [
99-
... ($this->parent !== null) ? $this->parent->contextToMerge() : [],
100-
$this->context
101-
];
109+
return [...($this->parent !== null) ? $this->parent->contextToMerge() : [], $this->context];
102110
}
103111

104112
/** @return array<mixed> */
105113
protected function mergedContext(): array
106114
{
107115
return array_merge(...$this->contextToMerge());
108116
}
109-
110-
/* PSR3 Implementation, wrapping our wrapped instance. */
111-
112-
#[\Override]
113-
public function log($level, string|\Stringable $message, array $context = []): void
114-
{
115-
assert(is_string($level) || is_int($level));
116-
$context = $this->processContext($context);
117-
$this->logger->log($level, $message, $context);
118-
}
119-
public static function makeNullLogger(): StackLogger
120-
{
121-
$instance = new NullLogger();
122-
return new self($instance);
123-
}
124117
}

tests/BaseTestCase.php

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace TimDev\StackLogger\Test;
66

77
use PHPUnit\Framework\TestCase;
8-
use TimDev\StackLogger\StackLogger;
98
use TimDev\StackLogger\Test\Support\TestStackLogger;
109

1110
/**
@@ -17,12 +16,10 @@
1716
*/
1817
abstract class BaseTestCase extends TestCase
1918
{
20-
abstract protected function makeTestSubject(): TestStackLogger;
21-
2219
private TestStackLogger $log;
2320

2421
#[\Override]
25-
public function setUp(): void
22+
protected function setUp(): void
2623
{
2724
$this->log = $this->makeTestSubject();
2825
}
@@ -50,10 +47,7 @@ public function testAddsContext(): void
5047

5148
$this->log
5249
->addContext(['even more' => 'context'])
53-
->warning(
54-
'This message should get four context elements.',
55-
['foo' => 'bar']
56-
);
50+
->warning('This message should get four context elements.', ['foo' => 'bar']);
5751
self::assertEquals(4, $this->log->contextCountAt(2));
5852

5953
$this->log->debug('Back to three!');
@@ -70,7 +64,7 @@ public function testCreateChildWithContext(): void
7064

7165
public function testAccumulatesContext(): void
7266
{
73-
$log = $this->log->withContext(['initial' => 'context']);
67+
$log = $this->log->withContext(['initial' => 'context']);
7468
$child = $log->withContext(['more' => 'context']);
7569

7670
// $child should have two context items.
@@ -87,10 +81,7 @@ public function testAccumulatesContext(): void
8781
$log->info('I should have one context item (on my second record)');
8882

8983
self::assertCount(1, $log->contextAt(1));
90-
self::assertEquals(
91-
['initial'],
92-
$log->contextKeysAt(1)
93-
);
84+
self::assertEquals(['initial'], $log->contextKeysAt(1));
9485
}
9586

9687
public function testMergesContext(): void
@@ -101,29 +92,23 @@ public function testMergesContext(): void
10192
$log
10293
->withContext(['a' => 'Allison', 'b' => 'Bob'])
10394
->emergency('Allison and Bruno', ['b' => 'Bruno']);
104-
self::assertEquals(
105-
['Allison', 'Bruno'],
106-
$this->log->contextValuesAt(0)
107-
);
95+
self::assertEquals(['Allison', 'Bruno'], $this->log->contextValuesAt(0));
10896

10997
// But original logger should still have (only) Alice.
11098
$log->critical('Alice alone.');
111-
self::assertEquals(
112-
['a' => 'Alice'],
113-
$this->log->contextAt(1)
114-
);
99+
self::assertEquals(['a' => 'Alice'], $this->log->contextAt(1));
115100
}
116101

117102
public function testInvokesCallables(): void
118103
{
119104
$logger = $this->log;
120-
$child = $logger->withContext(
105+
$child = $logger->withContext(
121106
[
122107
// A callable context element that returns the number of
123108
// elements in the record's context.
124109
'counter' => function (array $ctx) {
125110
return count($ctx);
126-
}
111+
},
127112
]
128113
);
129114
$child->notice('Only one context element, the result of the callable.');
@@ -136,8 +121,8 @@ public function testInvokesCallables(): void
136121
$child->contextAt(1)['counter']
137122
);
138123

139-
$start = microtime(true);
140-
$child2 = $logger->withContext(['elapsed_micros' => fn() => 1000000 * (microtime(true) - $start)]);
124+
$start = microtime(true);
125+
$child2 = $logger->withContext(['elapsed_micros' => fn () => 1000000 * (microtime(true) - $start)]);
141126
usleep(1000);
142127
$child2->alert('At least 1000 μ-sec have passed.');
143128
self::assertGreaterThan(1000, $child2->contextAt(2)['elapsed_micros']);
@@ -149,15 +134,16 @@ public function testInvokesCallables(): void
149134
*/
150135
public function testLongChain(): void
151136
{
152-
$logger = $this->log;
137+
$logger = $this->log;
153138
$numLoggers = 20;
154139
foreach (range(1, $numLoggers) as $i) {
155140
$logger = $logger->withContext(["gen{$i}" => $i]);
156141
}
157-
$logger->withContext(["count" => fn(array $ctx) => count($ctx)]);
158-
$logger->error("I come from a long lineage", ['final' => 'I should be the 21st context element']);
142+
$logger->withContext(['count' => fn (array $ctx) => count($ctx)]);
143+
$logger->error('I come from a long lineage', ['final' => 'I should be the 21st context element']);
159144
self::assertIsIterable($logger->recordAt(0)['context']);
160145
self::assertCount($numLoggers + 1, $logger->recordAt(0)['context']);
161146
}
162147

148+
abstract protected function makeTestSubject(): TestStackLogger;
163149
}

tests/MonologTest.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace TimDev\StackLogger\Test;
66

7-
use TimDev\StackLogger\StackLogger;
87
use TimDev\StackLogger\Test\Support\MonologStackLogger;
98

109
/**
@@ -15,15 +14,11 @@
1514
*/
1615
class MonologTest extends BaseTestCase
1716
{
18-
protected function makeTestSubject(): MonologStackLogger
19-
{
20-
return new MonologStackLogger();
21-
}
22-
2317
public function testWithnameCloning(): void
2418
{
2519
// push some context.
26-
$log = $this->makeTestSubject()->withContext(['basic' => 'context']);
20+
$log = $this->makeTestSubject()
21+
->withContext(['basic' => 'context']);
2722

2823
// get a clone with a new monolog channel-name, and log to it.
2924
$newChannel = $log->withName('other');
@@ -50,7 +45,8 @@ public function testWithnameCloning(): void
5045

5146
public function testWithnameClonesTrackParentContext(): void
5247
{
53-
$original = $this->makeTestSubject()->withContext(['original' => 'context']);
48+
$original = $this->makeTestSubject()
49+
->withContext(['original' => 'context']);
5450

5551
// withName()'d clone includes parent's context.
5652
$renamed = $original->withName('new');
@@ -74,7 +70,8 @@ public function testWithnameConvoluted(): void
7470
of a general sanity-check.
7571
*/
7672

77-
$first = $this->makeTestSubject()->withName('first');
73+
$first = $this->makeTestSubject()
74+
->withName('first');
7875
$second = $first->withName('second');
7976
$second = $second->withContext(['second' => 'context']);
8077

@@ -104,4 +101,9 @@ public function testNullLoggerFactory(): void
104101
$null = MonologStackLogger::makeNullLogger();
105102
self::assertInstanceOf(\TimDev\StackLogger\MonologStackLogger::class, $null);
106103
}
104+
105+
protected function makeTestSubject(): MonologStackLogger
106+
{
107+
return new MonologStackLogger();
108+
}
107109
}

tests/Psr3Test.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
*/
1313
class Psr3Test extends BaseTestCase
1414
{
15-
protected function makeTestSubject(): TestStackLogger
16-
{
17-
return new Psr3StackLogger();
18-
}
19-
2015
public function testNullLoggerFactory(): void
2116
{
2217
$null = \TimDev\StackLogger\Psr3StackLogger::makeNullLogger();
2318
self::assertInstanceOf(\TimDev\StackLogger\Psr3StackLogger::class, $null);
2419
}
20+
21+
protected function makeTestSubject(): TestStackLogger
22+
{
23+
return new Psr3StackLogger();
24+
}
2525
}

tests/Support/MonologStackLogger.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
use Monolog\Logger as MonologLogger;
99
use Monolog\LogRecord as MonologRecord;
1010
use TimDev\StackLogger\MonologStackLogger as BaseMonologStackLogger;
11-
use TimDev\StackLogger\StackLogger;
1211

1312
/**
1413
* TestLoggerInterface implementation that extends WrappedMonolog.
15-
*
1614
*/
1715
class MonologStackLogger extends BaseMonologStackLogger implements TestStackLogger
1816
{
@@ -50,7 +48,7 @@ public function getChannelRecords(): array
5048
return array_values(
5149
array_filter(
5250
$this->getRecords(),
53-
fn(MonologRecord $rec) => $rec['channel'] === $this->getWrapped()->getName()
51+
fn (MonologRecord $rec) => $rec['channel'] === $this->getWrapped()->getName()
5452
)
5553
);
5654
}

0 commit comments

Comments
 (0)