Skip to content

Commit

Permalink
chore: phpstan level 8, increased php and symfony minimum version
Browse files Browse the repository at this point in the history
fixed all found errors
  • Loading branch information
tuxes3 committed Oct 16, 2024
1 parent c0b9239 commit 9716e75
Show file tree
Hide file tree
Showing 21 changed files with 121 additions and 149 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ name: cronBundle
on:
push:
branches:
- 1.0-dev
- develop
- main
pull_request:
branches:
- 1.0-dev
- develop
- main

jobs:
phpunit:
runs-on: ubuntu-latest
steps:
- uses: shivammathur/setup-php@2cb9b829437ee246e9b3cac53555a39208ca6d28
with:
php-version: '8.2'
php-version: '8.3'
- uses: samin/[email protected]
with:
mysql root password: root
Expand All @@ -24,4 +26,6 @@ jobs:
- name: Check Code Styles
run: vendor/bin/ecs
- name: Execute tests (Unit and Feature tests) via PHPUnit
run: vendor/bin/simple-phpunit
run: vendor/bin/simple-phpunit
- name: Check with phpstan
run: vendor/bin/phpstan analyse
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ ecs:

## PHP Unit
phpunit:
vendor/bin/simple-phpunit
vendor/bin/simple-phpunit -v

## PHP Stan
phpstan:
vendor/bin/phpstan analyse
43 changes: 24 additions & 19 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,48 @@
{
"name": "Felix Ammann",
"email": "[email protected]"
},
{
"name": "whatwedo GmbH",
"email": "[email protected]"
}
],
"require": {
"php": ">=8.1",
"php": ">=8.3",
"ext-posix": "*",
"dragonmantank/cron-expression": "~3.3",
"symfony/console": "^7.0|^6.4|^5.4",
"symfony/event-dispatcher": "^7.0|^6.4|^5.4",
"symfony/security-bundle": "^7.0|^6.4|^5.4",
"symfony/framework-bundle": "^7.0|^6.4|^5.4",
"symfony/process": "^7.0|^6.4|^5.4",
"symfony/console": "^7.1|^6.4",
"symfony/event-dispatcher": "^7.1|^6.4",
"symfony/security-bundle": "^7.1|^6.4",
"symfony/framework-bundle": "^7.1|^6.4",
"symfony/process": "^7.1|^6.4",
"symfony/webpack-encore-bundle": "^1.14|^2.1",
"symfony/stimulus-bundle": "^2.16"
},
"require-dev": {
"doctrine/doctrine-bundle": "^2.5",
"doctrine/orm": "^2.11|3.0",
"doctrine/annotations": "^1.13.2|^2.0",
"symfony/phpunit-bridge": "^7.0",
"symfony/http-kernel": "^7.0",
"symfony/config": "^7.0",
"symfony/dependency-injection": "^7.0",
"symfony/yaml": "^7.0",
"symfony/validator": "^7.0",
"symfony/phpunit-bridge": "^7.1",
"symfony/http-kernel": "^7.1",
"symfony/config": "^7.1",
"symfony/dependency-injection": "^7.1",
"symfony/yaml": "^7.1",
"symfony/validator": "^7.1",
"symfony/maker-bundle": "^1.0.0",
"symfony/orm-pack": "^2.4",
"symfony/translation": "^7.0",
"symfony/twig-bundle": "^7.0",
"symfony/translation": "^7.1",
"symfony/twig-bundle": "^7.1",
"symfony/test-pack": "^1.0",
"symfony/runtime": "^7.0",
"symfony/dotenv": "^7.0",
"symfony/runtime": "^7.1",
"symfony/dotenv": "^7.1",
"gedmo/doctrine-extensions": "^3.5",
"whatwedo/php-coding-standard": "dev-develop",
"whatwedo/php-coding-standard": "^1.2",
"whatwedo/twig-bootstrap-icons": "^1.0",
"zenstruck/foundry": "^1.21",
"zenstruck/foundry": "^2.1",
"zenstruck/console-test": "^v1.3.0",
"zenstruck/browser": "^1.2"
"zenstruck/browser": "^1.2",
"phpstan/phpstan": "^1.12"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 2 additions & 6 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@
use PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting\ClassCommentSniff;
use PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting\FileCommentSniff;
use PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting\FunctionCommentThrowTagSniff;
use Symplify\EasyCodingStandard\ValueObject\Option;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return static function (ECSConfig $ecsConfig): void {
$ecsConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);

$ecsConfig->import('vendor/whatwedo/php-coding-standard/config/whatwedo-symfony.php');

$ecsConfig->parameters()->set(Option::SKIP, [
$ecsConfig->skip([
FileCommentSniff::class,
ClassCommentSniff::class,
FunctionCommentThrowTagSniff::class,
Expand All @@ -26,6 +23,5 @@
],
]);


$ecsConfig->parameters()->set(Option::PARALLEL, true);
$ecsConfig->parallel();
};
5 changes: 5 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 8
paths:
- src
- tests
9 changes: 2 additions & 7 deletions src/Command/CleanupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ protected function configure(): void
$this
->addOption('max-retention', null, InputOption::VALUE_REQUIRED, 'The maximum retention time (will be parsed by DateTime).', '1 month')
->addOption('max-retention-successful', null, InputOption::VALUE_REQUIRED, 'The maximum retention time for succeeded jobs (will be parsed by DateTime).', '7 days')
->addOption('per-call', null, InputOption::VALUE_REQUIRED, 'The maximum number of jobs to clean-up per call.', 1000)
;
}

Expand All @@ -59,11 +58,8 @@ protected function configure(): void
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$perCall = (int) $input->getOption('per-call');

$deletedSuccessful = $this->executionRepository->deleteSuccessfulJobs(
new \DateTimeImmutable('-'.$input->getOption('max-retention-successful')),
$perCall
new \DateTimeImmutable('-'.$input->getOption('max-retention-successful'))
);

$output->writeln(sprintf(
Expand All @@ -72,8 +68,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
));

$deletedNotSuccessful = $this->executionRepository->deleteNotSuccessfulJobs(
new \DateTimeImmutable('-'.$input->getOption('max-retention')),
$perCall
new \DateTimeImmutable('-'.$input->getOption('max-retention'))
);

$output->writeln(sprintf(
Expand Down
18 changes: 8 additions & 10 deletions src/Command/ExecuteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ public function checkMaxRuntime(Execution $execution, CronInterface $cronJob, Pr
return;
}
$now = new \DateTime();
$diff = $now->getTimestamp() - $execution->getStartedAt()->getTimestamp();
$diff = $now->getTimestamp() - ($execution->getStartedAt() ?? $now)->getTimestamp();
if ($diff > $cronJob->getMaxRuntime()) {
$this->logger->info(sprintf('execute: max RunTime reached PID:%s', $process->getPid()));
$execution
->setState(Execution::STATE_TERMINATED)
->setPid(null)
->setStdout($process->getOutput())
->setStderr($process->getErrorOutput());
$this->entityManager->flush($execution);
$this->entityManager->flush();
throw new MaxRuntimeReachedException($execution);
}
}
Expand Down Expand Up @@ -105,15 +105,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
->setState(Execution::STATE_RUNNING)
->setCommand($command);
$this->entityManager->persist($execution);
$this->entityManager->flush($execution);
$this->entityManager->flush();

// Run command
$process = new Process($command, $this->projectDir);
$process->setTimeout($cronJob->getMaxRuntime());
$process->start();
$execution->setPid($process->getPid());
$this->logger->info(sprintf('execute %s: PID:%s', $logId, $execution->getPid()));
$this->entityManager->flush($execution);
$this->entityManager->flush();
$this->eventDispatcher->dispatch(new CronStartEvent($cronJob), CronStartEvent::NAME);

// Update command output every 5 seconds
Expand All @@ -127,7 +127,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
sleep(5);
$execution->setStdout($process->getOutput())
->setStderr($process->getErrorOutput());
$this->entityManager->flush($execution);
$this->entityManager->flush();
}
$this->logger->info(sprintf('execute %s: is finisched PID:%s', $logId, $execution->getPid()));

Expand All @@ -137,7 +137,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

// Finish execution
$output->writeln('Execution finished with exit code '.$process->getExitCode());
$this->logger->info(sprintf('execute %s: is finisched PID:%s exitCode:', $logId, $execution->getPid(), $process->getExitCode()));
$this->logger->info(sprintf('execute %s: is finished PID: %s exitCode: %s', $logId, $execution->getPid(), $process->getExitCode()));

$execution
->setState(Execution::STATE_FINISHED)
Expand All @@ -151,13 +151,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$execution->setState(Execution::STATE_ERROR);
}

$this->entityManager->flush($execution);
$this->entityManager->flush();
$this->eventDispatcher->dispatch(new CronFinishEvent($cronJob), CronFinishEvent::NAME);

// cleanup Executions
foreach ($cronJob->getExecutionRetention() as $state => $maxRetained) {
$expr = $this->entityManager->getExpressionBuilder();

$topIds = $this->entityManager->getRepository(Execution::class)->createQueryBuilder('execution')
->select('execution.id')
->where('execution.job = :jobClass')
Expand Down Expand Up @@ -199,7 +197,7 @@ protected function getCronCommand(CronInterface $cron): string
}

if ($cron instanceof Command) {
return $cron->getDefaultName();
return $cron->getDefaultName() ?? '';
}

return '';
Expand Down
6 changes: 3 additions & 3 deletions src/Command/InfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$table->setHeaders(['Name', 'Value']);
$table->addRows([
['Cron job', get_class($cronJob)],
['Description', $cronJob->getDescription()],
['Description', $cronJob instanceof CronJobInterface ? $cronJob->getDescription() : $cronJob::class],
['Expression', $cronJob->getExpression()],
['Command', $this->getCommand($cronJob)],
['Arguments', $this->getArgumentString($cronJob)],
Expand All @@ -97,7 +97,7 @@ protected function getCommand(CronInterface $cronJob): string
}

if ($cronJob instanceof Command) {
return $cronJob->getDefaultName();
return $cronJob->getDefaultName() ?? '';
}

return '';
Expand Down Expand Up @@ -139,7 +139,7 @@ protected function getNextExecutionDateString(CronInterface $cronJob): ?string
return $this->getFormattedDate($nextExecutionDate);
}

protected function getFormattedDate(\DateTime $date): ?string
protected function getFormattedDate(?\DateTime $date): ?string
{
if (! $date) {
return null;
Expand Down
4 changes: 3 additions & 1 deletion src/Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use whatwedo\CronBundle\CronJob\CronJobInterface;
use whatwedo\CronBundle\Manager\CronJobManager;

#[AsCommand(name: 'whatwedo:cron:list', description: 'List all cron jobs')]
Expand All @@ -57,7 +58,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$nextRunDate = $cronExpression->getNextRunDate()->format('Y-m-d H:i:s');
}

$table->addRow([get_class($cronJob), $cronJob->getDescription(), $nextRunDate]);
$description = $cronJob instanceof CronJobInterface ? $cronJob->getDescription() : $cronJob::class;
$table->addRow([get_class($cronJob), $description, $nextRunDate]);
}
$table->render();

Expand Down
45 changes: 6 additions & 39 deletions src/Controller/CronJobController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use whatwedo\CronBundle\Entity\Execution;
use whatwedo\CronBundle\Exception\CronJobNotFoundException;
use whatwedo\CronBundle\Manager\CronJobManager;
use whatwedo\CronBundle\Manager\ExecutionManager;
use whatwedo\CronBundle\Repository\ExecutionRepository;
Expand Down Expand Up @@ -53,10 +54,9 @@ public function show(string $class): Response
}

return $this->render('@whatwedoCron/show.html.twig', [
'content_title' => $cronJob->getCommand(),
'content_title' => $cronJob::class,
'cronjob' => $cronJob,
'lastExecutions' => $lastExecutions,
'activable' => $cronJob instanceof CronJobActivable,
'allwedToRun' => $allowedToRun,
'nextExecutionDate' => $this->executionManager->getNextExecutionDate($cronJob),
]);
Expand Down Expand Up @@ -102,45 +102,12 @@ public function run(string $class): Response
]));
}

public function activate(string $class): Response
{
$cronJob = $this->cronJobManager->getCronJob($class);
if (! $cronJob instanceof CronJobActivable) {
throw new \Exception('Job not deactivable in Frontend');
}

$this->addFlash('success', 'cronjob.activate');

$url = $this->generateUrl('whatwedo_cronjob_show', [
'class' => $class,
]);

return $this->redirect($url);
}

public function deactivate(string $class): Response
{
$cronJob = $this->cronJobManager->getCronJob($class);

if (! $cronJob instanceof CronJobActivable) {
throw new \Exception('Job not deactivable in Frontend');
}

$this->addFlash('success', 'cronjob.deactivate');

$url = $this->generateUrl('whatwedo_cronjob_show', [
'class' => $class,
]);

return $this->redirect($url);
}

public function clean(string $class, string $state): Response
{
$cronJob = $this->cronJobManager->getCronJob($class);

if (! $cronJob) {
throw new \Exception('Job not found');
try {
$cronJob = $this->cronJobManager->getCronJob($class);
} catch (CronJobNotFoundException) {
throw $this->createNotFoundException();
}

$this->executionRepository->deleteExecutions($cronJob, $state);
Expand Down
2 changes: 1 addition & 1 deletion src/CronJob/CleanupCronJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CleanupCronJob
{
public function getCommand(): string
{
return CleanupCommand::getDefaultName();
return CleanupCommand::getDefaultName() ?? throw new \RuntimeException('Command name not found');
}

public function getExpression(): string
Expand Down
3 changes: 3 additions & 0 deletions src/CronJob/CronInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,8 @@ public function isParallelAllowed(): bool;
*/
public function isActive(): bool;

/**
* @return array<string, int>
*/
public function getExecutionRetention(): array;
}
Loading

0 comments on commit 9716e75

Please sign in to comment.