Skip to content

Commit b183d7e

Browse files
committed
Code: apple phpstan, simplify exceptions
1 parent a39eb18 commit b183d7e

12 files changed

Lines changed: 97 additions & 63 deletions

src/Cache/Cleaners/NetteCachingStorageCleaner.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
namespace Contributte\Console\Extra\Cache\Cleaners;
44

55
use Nette\Caching\Cache;
6-
use Nette\Caching\IStorage;
6+
use Nette\Caching\Storage;
77
use Symfony\Component\Console\Input\InputInterface;
88
use Symfony\Component\Console\Output\OutputInterface;
99

1010
class NetteCachingStorageCleaner implements ICleaner
1111
{
1212

13-
/** @var IStorage[] */
13+
/** @var Storage[] */
1414
private array $storages;
1515

1616
/**
17-
* @param IStorage[] $storages
17+
* @param Storage[] $storages
1818
*/
1919
public function __construct(array $storages)
2020
{
@@ -23,27 +23,27 @@ public function __construct(array $storages)
2323

2424
public function getDescription(): string
2525
{
26-
return IStorage::class;
26+
return Storage::class;
2727
}
2828

2929
public function clean(InputInterface $input, OutputInterface $output): bool
3030
{
3131
if ($this->storages === []) {
32-
$output->writeln(sprintf('<comment>Skipped %s cleaning, no IStorage services defined.</comment>', IStorage::class));
32+
$output->writeln(sprintf('<comment>Skipped %s cleaning, no IStorage services defined.</comment>', Storage::class));
3333

3434
return false;
3535
}
3636

37-
$output->writeln(sprintf('<comment>Cleaning %s</comment>', IStorage::class));
37+
$output->writeln(sprintf('<comment>Cleaning %s</comment>', Storage::class));
3838

3939
foreach ($this->storages as $name => $storage) {
4040
$output->writeln(sprintf('Cleaning storage instance %s', (string) $name), OutputInterface::VERBOSITY_VERBOSE);
4141
$storage->clean([
42-
Cache::ALL => true,
42+
Cache::All => true,
4343
]);
4444
}
4545

46-
$output->writeln(sprintf('<info>%s successfully cleaned.</info>', IStorage::class));
46+
$output->writeln(sprintf('<info>%s successfully cleaned.</info>', Storage::class));
4747

4848
return true;
4949
}

src/Cache/Generators/DiContainersCacheGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Contributte\Console\Extra\Cache\Generators;
44

5-
use Nette\Configurator;
5+
use Nette\Bootstrap\Configurator;
66
use Symfony\Component\Console\Input\InputInterface;
77
use Symfony\Component\Console\Output\OutputInterface;
88

@@ -50,7 +50,7 @@ public function generate(InputInterface $input, OutputInterface $output): bool
5050
));
5151

5252
$configurator = clone $this->configurator;
53-
$configurator->addParameters($parameters);
53+
$configurator->addStaticParameters($parameters);
5454
$configurator->loadContainer();
5555
}
5656

src/Cache/Generators/LatteTemplatesCacheGenerator.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22

33
namespace Contributte\Console\Extra\Cache\Generators;
44

5-
use Nette\Application\UI\ITemplateFactory;
65
use Nette\Bridges\ApplicationLatte\Template;
6+
use Nette\Bridges\ApplicationLatte\TemplateFactory;
77
use Nette\Utils\Finder;
8-
use Nette\Utils\Strings;
98
use Symfony\Component\Console\Input\InputInterface;
109
use Symfony\Component\Console\Output\OutputInterface;
1110
use Throwable;
1211

1312
class LatteTemplatesCacheGenerator implements IGenerator
1413
{
1514

16-
private ITemplateFactory $templateFactory;
15+
private TemplateFactory $templateFactory;
1716

1817
/** @var string[] */
1918
private array $dirs;
@@ -27,7 +26,7 @@ class LatteTemplatesCacheGenerator implements IGenerator
2726
* @param string[] $dirs
2827
* @param string[] $excludeDirs
2928
*/
30-
public function __construct(ITemplateFactory $templateFactory, array $dirs, array $excludeDirs = [], ?string $rootDir = null)
29+
public function __construct(TemplateFactory $templateFactory, array $dirs, array $excludeDirs = [], ?string $rootDir = null)
3130
{
3231
$this->templateFactory = $templateFactory;
3332
$this->dirs = $dirs;
@@ -65,7 +64,7 @@ public function generate(InputInterface $input, OutputInterface $output): bool
6564

6665
foreach ($finder as $path => $file) {
6766
$path = (string) realpath($path);
68-
$outputPath = $this->rootDir !== null && Strings::startsWith($path, $this->rootDir)
67+
$outputPath = $this->rootDir !== null && str_starts_with($path, $this->rootDir)
6968
? substr($path, mb_strlen($this->rootDir))
7069
: $path;
7170

src/Command/AdvancedCache/CacheCleanCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Contributte\Console\Extra\Command\AdvancedCache;
44

55
use Contributte\Console\Extra\Cache\Cleaners\ICleaner;
6-
use Contributte\Console\Extra\Exception\Logical\InvalidArgumentException;
6+
use Contributte\Console\Extra\Exception\LogicalException;
77
use Contributte\Console\Extra\Utils\Utils;
88
use Symfony\Component\Console\Attribute\AsCommand;
99
use Symfony\Component\Console\Command\Command;
@@ -61,7 +61,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6161

6262
if (($cleanerName = $input->getOption('cleaner')) !== null) {
6363
if (!is_string($cleanerName) || !isset($this->cleaners[$cleanerName])) {
64-
throw new InvalidArgumentException(sprintf('Cannot run undefined cleaner "%s"', Utils::stringify($cleanerName)));
64+
throw new LogicalException(sprintf('Cannot run undefined cleaner "%s"', Utils::stringify($cleanerName)));
6565
}
6666

6767
$this->cleaners[$cleanerName]->clean($input, $output);

src/Command/AdvancedCache/CacheGenerateCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Contributte\Console\Extra\Command\AdvancedCache;
44

55
use Contributte\Console\Extra\Cache\Generators\IGenerator;
6-
use Contributte\Console\Extra\Exception\Logical\InvalidArgumentException;
6+
use Contributte\Console\Extra\Exception\LogicalException;
77
use Contributte\Console\Extra\Utils\Utils;
88
use Symfony\Component\Console\Attribute\AsCommand;
99
use Symfony\Component\Console\Command\Command;
@@ -62,7 +62,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6262

6363
if (($generatorName = $input->getOption('generator')) !== null) {
6464
if (!is_string($generatorName) || !isset($this->generators[$generatorName])) {
65-
throw new InvalidArgumentException(sprintf('Cannot run undefined generator "%s"', Utils::stringify($generatorName)));
65+
throw new LogicalException(sprintf('Cannot run undefined generator "%s"', Utils::stringify($generatorName)));
6666
}
6767

6868
$this->generators[$generatorName]->generate($input, $output);

src/Command/Caching/CachingClearCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4141
$style->title('Caching Clear');
4242

4343
if ($input->getOption('all') === null) {
44-
$this->storage->clean([Cache::ALL => true]);
44+
$this->storage->clean([Cache::All => true]);
4545
$style->success('Clearing whole storage done.');
4646
} elseif ($input->getOption('tag') !== null) {
47-
$this->storage->clean([Cache::TAGS => $input->getOption('tag')]);
47+
$this->storage->clean([Cache::Tags => $input->getOption('tag')]);
4848
$style->listing((array) $input->getOption('tag'));
4949
$style->success('Clearing by tags done.');
5050
} elseif ($input->getOption('priority') !== null) {
51-
$this->storage->clean([Cache::PRIORITY => $input->getOption('priority')]);
51+
$this->storage->clean([Cache::Priority => $input->getOption('priority')]);
5252
$style->comment(Utils::stringify($input->getOption('priority')));
5353
$style->success('Clearing by priority done.');
5454
} else {

src/Command/Router/RouterDumpCommand.php

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Contributte\Console\Extra\Command\Router;
44

5+
use Contributte\Console\Extra\Exception\LogicalException;
56
use Nette\Application\Routers\Route;
67
use Nette\Application\Routers\RouteList;
7-
use Nette\Application\Routers\SimpleRouter;
88
use Nette\Routing\Router;
9+
use stdClass;
910
use Symfony\Component\Console\Attribute\AsCommand;
1011
use Symfony\Component\Console\Command\Command;
1112
use Symfony\Component\Console\Helper\Table;
@@ -41,40 +42,66 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4142
}
4243

4344
/**
44-
* @return mixed[]
45+
* @return array<mixed>
4546
*/
4647
protected function createRows(): array
4748
{
4849
return $this->analyse($this->router);
4950
}
5051

5152
/**
52-
* @return mixed[]|object
53+
* @return array<mixed>
5354
*/
54-
protected function analyse(Router $router, ?string $module = null): array|object
55+
protected function analyse(Router $router, ?string $module = null): array
5556
{
5657
if ($router instanceof RouteList) {
57-
$routes = [];
58+
$routes = $this->analyseRouteList($router, $module);
59+
} elseif ($router instanceof Route) {
60+
$routes = [(array) $this->analyseRoute($router, $module)];
61+
} else {
62+
throw new LogicalException(sprintf('Router "%s" is not supported', $router::class));
63+
}
5864

59-
foreach ($router as $subRouter) {
60-
$route = $this->analyse($subRouter, $module . $router->getModule());
65+
return $routes;
66+
}
6167

62-
if (is_array($route)) {
63-
$routes = array_merge($routes, $route);
64-
} else {
65-
$routes[] = (array) $route;
66-
}
68+
/**
69+
* @return array<mixed>
70+
*/
71+
protected function analyseRouteList(RouteList $router, ?string $module = null): array
72+
{
73+
$routes = [];
74+
75+
foreach ($router->getRouters() as $subRouter) {
76+
if ($subRouter instanceof RouteList) {
77+
$routes = array_merge(
78+
$routes,
79+
$this->analyseRouteList($subRouter, $module . $router->getModule())
80+
);
81+
} elseif ($subRouter instanceof Route) {
82+
$routes = array_merge(
83+
$routes,
84+
[(array) $this->analyseRoute($subRouter, $module . $router->getModule())]
85+
);
86+
} else {
87+
throw new LogicalException(sprintf('Router "%s" is not supported', $router::class));
6788
}
68-
69-
return $routes;
70-
} else {
71-
return (object) [
72-
'mask' => $router instanceof Route ? $router->getMask() : null,
73-
'module' => rtrim((string) $module, ':'),
74-
'defaults' => $router instanceof Route || $router instanceof SimpleRouter ? $this->analyseDefaults($router->getDefaults()) : null,
75-
'class' => $router::class,
76-
];
7789
}
90+
91+
return $routes;
92+
}
93+
94+
/**
95+
* @return stdClass
96+
*/
97+
protected function analyseRoute(Route $router, ?string $module = null): object
98+
{
99+
return (object) [
100+
'mask' => $router->getMask(),
101+
'module' => rtrim((string) $module, ':'),
102+
'defaults' => $this->analyseDefaults($router->getDefaults()),
103+
'class' => $router::class,
104+
];
78105
}
79106

80107
/**

src/Command/Security/SecurityPasswordCommand.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Contributte\Console\Extra\Command\Security;
44

5+
use Contributte\Console\Extra\Utils\Utils;
56
use Nette\Security\Passwords;
67
use Nette\Utils\Random;
78
use Symfony\Component\Console\Attribute\AsCommand;
@@ -43,27 +44,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4344

4445
if ($input->getArgument('password') !== null) {
4546
// Generate one password
46-
$password = $input->getArgument('password');
47+
$password = Utils::stringify($input->getArgument('password'));
4748
$style->comment('Password given');
48-
$encrypted = $this->passwords->hash(strval($password));
49+
$encrypted = $this->passwords->hash($password);
4950
$style->success(sprintf('Hashed password: %s', $encrypted));
5051

5152
return 0;
5253
} else {
5354
// Generate more passwords
5455
$table = new Table($output);
5556
$table->setHeaders(['ID', 'Generated random password']);
57+
$count = Utils::numerize($input->getOption('count'));
5658

57-
for ($i = 1; $i <= $input->getOption('count'); $i++) {
59+
for ($i = 1; $i <= $count; $i++) {
5860
$table->addRow([$i, $this->passwords->hash(sha1(Random::generate(50) . time() . random_bytes(20)))]);
5961

60-
if ($i !== intval($input->getOption('count'))) {
62+
if ($i !== $count) {
6163
$table->addRow(new TableSeparator());
6264
}
6365
}
6466

6567
$table->render();
66-
$style->success(sprintf('Total generated and hashed passwords %d.', intval($input->getOption('count'))));
68+
$style->success(sprintf('Total generated and hashed passwords %d.', $count));
6769

6870
return 0;
6971
}

src/Command/Utils/UtilsRandomCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Contributte\Console\Extra\Command\Utils;
44

5+
use Contributte\Console\Extra\Utils\Utils;
56
use Nette\Utils\Random;
67
use Symfony\Component\Console\Attribute\AsCommand;
78
use Symfony\Component\Console\Command\Command;
@@ -33,8 +34,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3334
$table = new Table($output);
3435
$table->setHeaders(['ID', 'Generated strings']);
3536

36-
$count = max(intval($input->getOption('count')), 1);
37-
$length = max(intval($input->getOption('length')), 1);
37+
$count = max(Utils::numerize($input->getOption('count')), 1);
38+
$length = max(Utils::numerize($input->getOption('length')), 1);
3839
for ($i = 1; $i <= $count; $i++) {
3940
$table->addRow([$i, Random::generate($length)]);
4041

src/DI/AbstractCompilerExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Contributte\Console\Extra\DI;
44

5-
use Contributte\Console\Extra\Exception\Logical\InvalidArgumentException;
5+
use Contributte\Console\Extra\Exception\LogicalException;
66
use Nette\DI\CompilerExtension;
77
use stdClass;
88

@@ -18,7 +18,7 @@ abstract class AbstractCompilerExtension extends CompilerExtension
1818
public function __construct(bool $cliMode = false)
1919
{
2020
if (func_num_args() <= 0) {
21-
throw new InvalidArgumentException(sprintf('Provide CLI mode, e.q. %s(%%consoleMode%%).', static::class));
21+
throw new LogicalException(sprintf('Provide CLI mode, e.q. %s(%%consoleMode%%).', static::class));
2222
}
2323

2424
$this->cliMode = $cliMode;

0 commit comments

Comments
 (0)