Skip to content

Commit f51fe53

Browse files
committed
Clean code after refactoring
1 parent 6f93595 commit f51fe53

File tree

4 files changed

+23
-20
lines changed

4 files changed

+23
-20
lines changed

README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ at any environment.
99
Package allows your app to switch to Debug Mode:
1010
- automatically on localhost's environment by IP,
1111
- semi-automatically on any environment where you set `APP_DEBUG` environment variable (useful for Docker dev-stack),
12-
- semi-automatically disable Debug mode on any environment where you set `app-debug-mode` cookie variable (useful for
12+
- semi-automatically **disable** Debug mode on any environment where you set `app-debug-mode` cookie variable (useful for
1313
tests and similar cases),
1414
- manually enable/disable (force turn-on or turn-off) Debug Mode.
1515

@@ -20,7 +20,10 @@ Package is optimized for invoking in very early lifecycle phase of your App
2020
## Requirements
2121
Package requires:
2222

23-
- PHP version at least 7.3
23+
- PHP version at least 7.4
24+
25+
Enabler requires:
26+
2427
- Temporary directory with writable access
2528

2629
## Installation
@@ -31,17 +34,15 @@ composer require redbitcz/debug-mode-enabler
3134
## Using
3235
Anywhere in your app you can determine if app is running in Debug mode by simple code:
3336
```php
34-
$debugMode = \Redbitcz\DebugMode\Detector::detect($tempDir); //bool
37+
$debugMode = \Redbitcz\DebugMode\Detector::detect(); //bool
3538
```
36-
where `$tempDir` is required path to temporary directory.
3739

3840
It returns `$debugMode` = `true` when it detects Debug environment or manually switched.
3941

4042
### Using with Nette
4143
In Boostrap use package like in this example:
4244
```php
43-
$tempDir = __DIR__ . '/../temp';
44-
$debugModeDetector = new \Redbitcz\DebugMode\Detector($tempDir);
45+
$debugModeDetector = new \Redbitcz\DebugMode\Detector();
4546

4647
$configurator = new Configurator();
4748
$configurator->setDebugMode($debugModeDetector->isDebugMode());
@@ -72,6 +73,9 @@ Enabler provide feature to force enable or disable Debug Mode anywhere for user'
7273
This example turn on Debug Mode for user's browser:
7374
```php
7475
$enabler = new \Redbitcz\DebugMode\Enabler($tempDir);
76+
77+
$detector = new \Redbitcz\DebugMode\Detector(\Redbitcz\DebugMode\Detector::MODE_FULL, $enabler);
78+
7579
$enabler->activate(true);
7680
```
7781

@@ -93,7 +97,8 @@ internally with `Detector` instance in `Bootstrap`.
9397
To re-use already existing instance you can inject it to DI Container:
9498
```php
9599
$tempDir = __DIR__ . '/../temp';
96-
$debugModeDetector = new \Redbitcz\DebugMode\Detector($tempDir);
100+
$enabler = new \Redbitcz\DebugMode\Enabler($tempDir);
101+
$debugModeDetector = new \Redbitcz\DebugMode\Detector(\Redbitcz\DebugMode\Detector::MODE_FULL, $enabler);
97102
98103
$configurator = new Configurator();
99104
$configurator->setDebugMode($debugModeDetector->isDebugMode());

src/Detector.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,10 @@ class Detector
3535
public const MODE_FULL = self::MODE_ENABLER | self::MODE_SIMPLE;
3636

3737

38-
/** @var Enabler|null */
39-
private $enabler;
40-
/** @var int */
41-
private $mode;
38+
private ?Enabler $enabler;
39+
private int $mode;
4240
/** @var string[] */
43-
private $ips = ['::1', '127.0.0.1'];
41+
private array $ips = ['::1', '127.0.0.1'];
4442

4543
/**
4644
* @param int $mode Enables methods which is used to detect Debug mode
@@ -49,7 +47,7 @@ class Detector
4947
public function __construct(int $mode = self::MODE_SIMPLE, ?Enabler $enabler = null)
5048
{
5149
if ($enabler === null && $mode & self::MODE_ENABLER) {
52-
throw new MissingEnablerException(
50+
throw new InconsistentEnablerModeException(
5351
'Enabler mode (and Full mode) requires the Enabler instance in constructor'
5452
);
5553
}
@@ -61,7 +59,7 @@ public function __construct(int $mode = self::MODE_SIMPLE, ?Enabler $enabler = n
6159
public function getEnabler(): Enabler
6260
{
6361
if ($this->enabler === null) {
64-
throw new MissingEnablerException('Unable to get Enabler because Detector constructed without it');
62+
throw new InconsistentEnablerModeException('Unable to get Enabler because Detector constructed without it');
6563
}
6664

6765
return $this->enabler;
@@ -183,7 +181,7 @@ public static function detect(
183181
?bool $default = false
184182
): ?bool {
185183
if ($tempDir === null && $mode & self::MODE_ENABLER) {
186-
throw new MissingEnablerException('Enabler mode requires \'tempDir\' argument');
184+
throw new InconsistentEnablerModeException('Enabler mode (and Full mode) requires \'tempDir\' argument');
187185
}
188186

189187
$enabler = $tempDir === null ? null : new Enabler($tempDir);

src/MissingEnablerException.php renamed to src/InconsistentEnablerModeException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
namespace Redbitcz\DebugMode;
66

7-
class MissingEnablerException extends \LogicException
7+
class InconsistentEnablerModeException extends \LogicException
88
{
99
}

tests/DetectorTest.php

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

77
use Redbitcz\DebugMode\Detector;
88
use Redbitcz\DebugMode\Enabler;
9-
use Redbitcz\DebugMode\MissingEnablerException;
9+
use Redbitcz\DebugMode\InconsistentEnablerModeException;
1010
use Tester\Assert;
1111
use Tester\Helpers;
1212
use Tester\TestCase;
@@ -195,22 +195,22 @@ public function testMissingEnablerMode(): void
195195
{
196196
Assert::exception(function () {
197197
$detector = new Detector(Detector::MODE_FULL);
198-
}, MissingEnablerException::class);
198+
}, InconsistentEnablerModeException::class);
199199
}
200200

201201
public function testMissingEnabler(): void
202202
{
203203
Assert::exception(function () {
204204
$detector = new Detector(Detector::MODE_SIMPLE);
205205
$detector->getEnabler();
206-
}, MissingEnablerException::class);
206+
}, InconsistentEnablerModeException::class);
207207
}
208208

209209
public function testMissingEnablerShortcut(): void
210210
{
211211
Assert::exception(function () {
212212
Detector::detect(Detector::MODE_FULL);
213-
}, MissingEnablerException::class);
213+
}, InconsistentEnablerModeException::class);
214214
}
215215
}
216216

0 commit comments

Comments
 (0)