Skip to content

Commit

Permalink
minor #487 Add PHP-CS-Fixer (HypeMC)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 3.x-dev branch.

Discussion
----------

Add PHP-CS-Fixer

This repo seems to be the only one that doesn't use PHP-CS-Fixer, which is a bit annoying when trying to contribute since I have to manually worry about coding standards. I'm not sure if there's a specific reason for that, so I decided to give it a try in this PR.

I took the `.php-cs-fixer.dist.php` from the framework repo and made a few adjustments. I removed `nullable_type_declaration` and `nullable_type_declaration_for_default_null_value` since those are part of the standard Symfony set. Additionally, I set `modernize_strpos` to `false` since the bundle still supports PHP >= 7.2.

I'm not sure how to run it in the CI, as some repos use fabbot while others, like the MakerBundle, use GitHub Actions.

It would be best to hide whitespace changes while reviewing.

Commits
-------

7fe4fbd Add PHP-CS-Fixer
  • Loading branch information
derrabus committed Aug 6, 2024
2 parents b2b5f9a + 7fe4fbd commit 502856f
Show file tree
Hide file tree
Showing 22 changed files with 917 additions and 875 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ on:
pull_request:

jobs:
php-cs-fixer:
runs-on: ubuntu-latest
name: Coding Standards
steps:
- name: Checkout
uses: actions/checkout@v4

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

with:
php-version: 8.3
coverage: none
tools: php-cs-fixer, cs2pr

- name: Run PHP-CS-Fixer
run: php-cs-fixer fix --dry-run --format checkstyle | cs2pr

test:
runs-on: ubuntu-latest
name: PHPUnit Tests
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
vendor
.php-cs-fixer.cache
composer.phar
composer.lock
phpunit.xml
Expand Down
38 changes: 38 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

$fileHeaderComment = <<<'EOF'
This file is part of the Symfony package.
(c) Fabien Potencier <fabien@symfony.com>
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
EOF;

return (new PhpCsFixer\Config())
->setRules([
'@PHP71Migration' => true,
'@PHPUnit75Migration:risky' => true,
'@Symfony' => true,
'@Symfony:risky' => true,
'protected_to_private' => false,
'header_comment' => ['header' => $fileHeaderComment],
'modernize_strpos' => false,
'trailing_comma_in_multiline' => ['elements' => ['arrays', 'match', 'parameters']],
])
->setRiskyAllowed(true)
->setFinder(
PhpCsFixer\Finder::create()
->in([__DIR__])
->append([__FILE__])
)
;
8 changes: 4 additions & 4 deletions DependencyInjection/Compiler/AddProcessorsPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@ public function process(ContainerBuilder $container)
foreach ($container->findTaggedServiceIds('monolog.processor') as $id => $tags) {
foreach ($tags as $tag) {
if (!empty($tag['channel']) && !empty($tag['handler'])) {
throw new \InvalidArgumentException(sprintf('you cannot specify both the "handler" and "channel" attributes for the "monolog.processor" tag on service "%s"', $id));
throw new \InvalidArgumentException(\sprintf('you cannot specify both the "handler" and "channel" attributes for the "monolog.processor" tag on service "%s"', $id));
}

if (!empty($tag['handler'])) {
$definition = $container->findDefinition(sprintf('monolog.handler.%s', $tag['handler']));
$definition = $container->findDefinition(\sprintf('monolog.handler.%s', $tag['handler']));
$parentDef = $definition;
while (!$parentDef->getClass() && $parentDef instanceof ChildDefinition) {
$parentDef = $container->findDefinition($parentDef->getParent());
}
$class = $container->getParameterBag()->resolveValue($parentDef->getClass());
if (!method_exists($class, 'pushProcessor')) {
throw new \InvalidArgumentException(sprintf('The "%s" handler does not accept processors', $tag['handler']));
throw new \InvalidArgumentException(\sprintf('The "%s" handler does not accept processors', $tag['handler']));
}
} elseif (!empty($tag['channel'])) {
if ('app' === $tag['channel']) {
$definition = $container->getDefinition('monolog.logger');
} else {
$definition = $container->getDefinition(sprintf('monolog.logger.%s', $tag['channel']));
$definition = $container->getDefinition(\sprintf('monolog.logger.%s', $tag['channel']));
}
} else {
$definition = $container->getDefinition('monolog.logger_prototype');
Expand Down
5 changes: 1 addition & 4 deletions DependencyInjection/Compiler/AddSwiftMailerTransportPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace Symfony\Bundle\MonologBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
Expand All @@ -25,9 +25,6 @@
*/
class AddSwiftMailerTransportPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
$handlers = $container->getParameter('monolog.swift_mailer.handlers');
Expand Down
10 changes: 5 additions & 5 deletions DependencyInjection/Compiler/DebugHandlerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

namespace Symfony\Bundle\MonologBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Monolog\Logger;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Monolog\Logger;
use Symfony\Component\DependencyInjection\Reference;

/**
* Adds the DebugHandler when the profiler is enabled and kernel.debug is true.
Expand All @@ -31,7 +31,7 @@ class DebugHandlerPass implements CompilerPassInterface

public function __construct(LoggerChannelPass $channelPass)
{
@trigger_error('The '.__CLASS__.' class is deprecated since version 2.12 and will be removed in 4.0. Use AddDebugLogProcessorPass in FrameworkBundle instead.', E_USER_DEPRECATED);
@trigger_error('The '.__CLASS__.' class is deprecated since version 2.12 and will be removed in 4.0. Use AddDebugLogProcessorPass in FrameworkBundle instead.', \E_USER_DEPRECATED);

$this->channelPass = $channelPass;
}
Expand All @@ -51,7 +51,7 @@ public function process(ContainerBuilder $container)

foreach ($this->channelPass->getChannels() as $channel) {
$container
->getDefinition($channel === 'app' ? 'monolog.logger' : 'monolog.logger.'.$channel)
->getDefinition('app' === $channel ? 'monolog.logger' : 'monolog.logger.'.$channel)
->addMethodCall('pushHandler', [new Reference('monolog.handler.debug')]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/Compiler/FixEmptyLoggerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function process(ContainerBuilder $container)
{
$container->register('monolog.handler.null_internal', 'Monolog\Handler\NullHandler');
foreach ($this->channelPass->getChannels() as $channel) {
$def = $container->getDefinition($channel === 'app' ? 'monolog.logger' : 'monolog.logger.'.$channel);
$def = $container->getDefinition('app' === $channel ? 'monolog.logger' : 'monolog.logger.'.$channel);
foreach ($def->getMethodCalls() as $method) {
if ('pushHandler' === $method[0]) {
continue 2;
Expand Down
17 changes: 7 additions & 10 deletions DependencyInjection/Compiler/LoggerChannelPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ class LoggerChannelPass implements CompilerPassInterface
{
protected $channels = ['app'];

/**
* {@inheritDoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('monolog.logger')) {
Expand All @@ -49,7 +46,7 @@ public function process(ContainerBuilder $container)
$resolvedChannel = $container->getParameterBag()->resolveValue($tag['channel']);

$definition = $container->getDefinition($id);
$loggerId = sprintf('monolog.logger.%s', $resolvedChannel);
$loggerId = \sprintf('monolog.logger.%s', $resolvedChannel);
$this->createLogger($resolvedChannel, $loggerId, $container);

foreach ($definition->getArguments() as $index => $argument) {
Expand Down Expand Up @@ -84,10 +81,10 @@ public function process(ContainerBuilder $container)

// create additional channels
foreach ($container->getParameter('monolog.additional_channels') as $chan) {
if ($chan === 'app') {
if ('app' === $chan) {
continue;
}
$loggerId = sprintf('monolog.logger.%s', $chan);
$loggerId = \sprintf('monolog.logger.%s', $chan);
$this->createLogger($chan, $loggerId, $container);
$container->getDefinition($loggerId)->setPublic(true);
}
Expand All @@ -98,7 +95,7 @@ public function process(ContainerBuilder $container)
foreach ($handlersToChannels as $handler => $channels) {
foreach ($this->processChannels($channels) as $channel) {
try {
$logger = $container->getDefinition($channel === 'app' ? 'monolog.logger' : 'monolog.logger.'.$channel);
$logger = $container->getDefinition('app' === $channel ? 'monolog.logger' : 'monolog.logger.'.$channel);
} catch (InvalidArgumentException $e) {
$msg = 'Monolog configuration error: The logging channel "'.$channel.'" assigned to the "'.substr($handler, 16).'" handler does not exist.';
throw new \InvalidArgumentException($msg, 0, $e);
Expand Down Expand Up @@ -133,20 +130,20 @@ protected function processChannels(?array $configuration)
}

/**
* Create new logger from the monolog.logger_prototype
* Create new logger from the monolog.logger_prototype.
*
* @return void
*/
protected function createLogger(string $channel, string $loggerId, ContainerBuilder $container)
{
if (!in_array($channel, $this->channels)) {
if (!\in_array($channel, $this->channels)) {
$logger = new ChildDefinition('monolog.logger_prototype');
$logger->replaceArgument(0, $channel);
$container->setDefinition($loggerId, $logger);
$this->channels[] = $channel;
}

$parameterName = $channel . 'Logger';
$parameterName = $channel.'Logger';

$container->registerAliasForArgument($loggerId, LoggerInterface::class, $parameterName);
}
Expand Down
Loading

0 comments on commit 502856f

Please sign in to comment.