Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install phpstan & run level 6 #3

Merged
merged 3 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
on: [pull_request]
name: Static analysis
jobs:
phpstan:
name: 🪨 PHPStan
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.3

- name: Download dependencies
uses: ramsey/composer-install@v3

- name: Install PHPStan
uses: ramsey/composer-install@v3
with:
working-directory: "tools/phpstan"

- name: PHPStan
run: tools/phpstan/vendor/bin/phpstan analyze --no-progress

php-cs-fixer:
name: 🧽 PHP-CS-Fixer
runs-on: ubuntu-latest
Expand All @@ -15,9 +39,9 @@ jobs:
php-version: 8.3

- name: Install php-cs-fixer
uses: ramsey/composer-install@v2
uses: ramsey/composer-install@v3
with:
composer-options: "--working-dir=tools/php-cs-fixer"
working-directory: "tools/php-cs-fixer"

- name: PHP-CS-Fixer
run: tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --dry-run --diff --config=.php-cs-fixer.dist.php
Expand All @@ -36,7 +60,7 @@ jobs:
php-version: 8.3

- name: Download dependencies
uses: ramsey/composer-install@v2
uses: ramsey/composer-install@v3

- name: Check whether a PHP dependency is compromised
shell: bash
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ jobs:
run: composer update ${{ matrix.composer-flags }} --no-interaction --no-progress --optimize-autoloader

- name: Run PHPUnit
run: vendor/bin/simple-phpunit
run: vendor/bin/phpunit
8 changes: 2 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@
"symfony/framework-bundle": "^5.4|^6.4|^7.0",
"symfony/phpunit-bridge": "^7.1",
"twig/twig": "^3.10",
"monolog/monolog": "^3.0"
"monolog/monolog": "^3.0",
"phpunit/phpunit": "^9.6"
},
"autoload": {
"psr-4": { "Ekino\\NewRelicBundle\\": "src/" }
},
"autoload-dev": {
"psr-4": { "Ekino\\NewRelicBundle\\Tests\\": "tests/" }
},
"extra": {
"branch-alias": {
"dev-master": "2.0-dev"
}
}
}
9 changes: 9 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
parameters:
level: 6

paths:
- src
- tests

ignoreErrors:
- '#Call to an undefined method Symfony\\Component\\Config\\Definition\\NodeInterface::getChildren\(\).#'
14 changes: 9 additions & 5 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
bootstrap="vendor/autoload.php"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
>
<testsuites>
<testsuite name="Ekino New Relic Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<testsuites>
<testsuite name="Ekino New Relic Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
</listeners>
</phpunit>
13 changes: 5 additions & 8 deletions src/Command/NotifyDeploymentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,9 @@ class NotifyDeploymentCommand extends Command
public const EXIT_UNAUTHORIZED = 2;
public const EXIT_HTTP_ERROR = 3;

protected static $defaultName = 'newrelic:notify-deployment';

private $newrelic;

public function __construct(Config $newrelic)
public function __construct(private Config $newrelic)
{
$this->newrelic = $newrelic;

parent::__construct();
parent::__construct('newrelic:notify-deployment');
}

protected function configure(): void
Expand Down Expand Up @@ -99,6 +93,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return $exitCode;
}

/**
* @return array{status: ?string, error: ?string}
*/
public function performRequest(string $api_key, string $payload, ?string $api_host = null): array
{
$headers = [
Expand Down
7 changes: 1 addition & 6 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,8 @@ class Configuration implements ConfigurationInterface
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('ekino_new_relic');
if (method_exists(TreeBuilder::class, 'getRootNode')) {
$rootNode = $treeBuilder->getRootNode();
} else {
$rootNode = $treeBuilder->root('ekino_new_relic');
}

$rootNode
$treeBuilder->getRootNode()
->fixXmlConfig('deployment_name')
->children()
->booleanNode('enabled')->defaultTrue()->end()
Expand Down
8 changes: 7 additions & 1 deletion src/DependencyInjection/EkinoNewRelicExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function load(array $configs, ContainerBuilder $container): void
$container->getDefinition(RequestListener::class)
->setArguments(
[
'$ignoreRoutes' => $config['http']['ignored_routes'],
'$ignoredRoutes' => $config['http']['ignored_routes'],
'$ignoredPaths' => $config['http']['ignored_paths'],
'$symfonyCache' => $config['http']['using_symfony_cache'],
]
Expand Down Expand Up @@ -132,6 +132,9 @@ public function load(array $configs, ContainerBuilder $container): void
}
}

/**
* @param array<string, mixed> $config
*/
private function getInteractorServiceId(array $config): string
{
if (!$config['enabled']) {
Expand All @@ -151,6 +154,9 @@ private function getInteractorServiceId(array $config): string
return $config['interactor'];
}

/**
* @param array<string, mixed> $config
*/
private function getTransactionNamingServiceId(array $config): string
{
switch ($config['http']['transaction_naming']) {
Expand Down
17 changes: 8 additions & 9 deletions src/Listener/CommandListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@

class CommandListener implements EventSubscriberInterface
{
private $interactor;
private $config;
private $ignoredCommands;

public function __construct(Config $config, NewRelicInteractorInterface $interactor, array $ignoredCommands)
{
$this->config = $config;
$this->interactor = $interactor;
$this->ignoredCommands = $ignoredCommands;
/**
* @param string[] $ignoredCommands
*/
public function __construct(
private readonly Config $config,
private readonly NewRelicInteractorInterface $interactor,
private readonly array $ignoredCommands,
) {
}

public static function getSubscribedEvents(): array
Expand Down
9 changes: 4 additions & 5 deletions src/Listener/DeprecationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@

class DeprecationListener
{
private $isRegistered = false;
private $interactor;
private bool $isRegistered = false;

public function __construct(NewRelicInteractorInterface $interactor)
{
$this->interactor = $interactor;
public function __construct(
private readonly NewRelicInteractorInterface $interactor,
) {
}

public function register(): void
Expand Down
24 changes: 5 additions & 19 deletions src/Listener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Ekino\NewRelicBundle\NewRelic\NewRelicInteractorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\KernelEvents;

Expand All @@ -25,11 +24,9 @@
*/
class ExceptionListener implements EventSubscriberInterface
{
private $interactor;

public function __construct(NewRelicInteractorInterface $interactor)
{
$this->interactor = $interactor;
public function __construct(
private readonly NewRelicInteractorInterface $interactor,
) {
}

public static function getSubscribedEvents(): array
Expand All @@ -39,22 +36,11 @@ public static function getSubscribedEvents(): array
];
}

/**
* @param GetResponseForExceptionEvent|ExceptionEvent $event
*/
public function onKernelException(KernelExceptionEvent $event): void
public function onKernelException(ExceptionEvent $event): void
{
$exception = method_exists($event, 'getThrowable') ? $event->getThrowable() : $event->getException();
$exception = $event->getThrowable();
if (!$exception instanceof HttpExceptionInterface) {
$this->interactor->noticeThrowable($exception);
}
}
}

if (!class_exists(KernelExceptionEvent::class)) {
if (class_exists(ExceptionEvent::class)) {
class_alias(ExceptionEvent::class, KernelExceptionEvent::class);
} else {
class_alias(GetResponseForExceptionEvent::class, KernelExceptionEvent::class);
}
}
46 changes: 14 additions & 32 deletions src/Listener/RequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,24 @@
use Ekino\NewRelicBundle\NewRelic\NewRelicInteractorInterface;
use Ekino\NewRelicBundle\TransactionNamingStrategy\TransactionNamingStrategyInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;

class RequestListener implements EventSubscriberInterface
{
private $ignoredRoutes;
private $ignoredPaths;
private $config;
private $interactor;
private $transactionNamingStrategy;
private $symfonyCache;

/**
* @param string[] $ignoredRoutes
* @param string[] $ignoredPaths
*/
public function __construct(
Config $config,
NewRelicInteractorInterface $interactor,
array $ignoreRoutes,
array $ignoredPaths,
TransactionNamingStrategyInterface $transactionNamingStrategy,
bool $symfonyCache = false
private Config $config,
private NewRelicInteractorInterface $interactor,
private array $ignoredRoutes,
private array $ignoredPaths,
private TransactionNamingStrategyInterface $transactionNamingStrategy,
private bool $symfonyCache = false
) {
$this->config = $config;
$this->interactor = $interactor;
$this->ignoredRoutes = $ignoreRoutes;
$this->ignoredPaths = $ignoredPaths;
$this->transactionNamingStrategy = $transactionNamingStrategy;
$this->symfonyCache = $symfonyCache;
}

public static function getSubscribedEvents(): array
Expand All @@ -58,7 +48,7 @@ public static function getSubscribedEvents(): array
];
}

public function setApplicationName(KernelRequestEvent $event): void
public function setApplicationName(RequestEvent $event): void
{
if (!$this->isEventValid($event)) {
return;
Expand All @@ -80,7 +70,7 @@ public function setApplicationName(KernelRequestEvent $event): void
}
}

public function setTransactionName(KernelRequestEvent $event): void
public function setTransactionName(RequestEvent $event): void
{
if (!$this->isEventValid($event)) {
return;
Expand All @@ -91,7 +81,7 @@ public function setTransactionName(KernelRequestEvent $event): void
$this->interactor->setTransactionName($transactionName);
}

public function setIgnoreTransaction(KernelRequestEvent $event): void
public function setIgnoreTransaction(RequestEvent $event): void
{
if (!$this->isEventValid($event)) {
return;
Expand All @@ -110,16 +100,8 @@ public function setIgnoreTransaction(KernelRequestEvent $event): void
/**
* Make sure we should consider this event. Example: make sure it is a master request.
*/
private function isEventValid(KernelRequestEvent $event): bool
private function isEventValid(RequestEvent $event): bool
{
return HttpKernelInterface::MAIN_REQUEST === $event->getRequestType();
}
}

if (!class_exists(KernelRequestEvent::class)) {
if (class_exists(RequestEvent::class)) {
class_alias(RequestEvent::class, KernelRequestEvent::class);
} else {
class_alias(GetResponseEvent::class, KernelRequestEvent::class);
}
}
Loading