Skip to content

Commit

Permalink
Add EasyCodingStandard
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisminett committed Oct 16, 2023
1 parent 8644e21 commit 05eb525
Show file tree
Hide file tree
Showing 20 changed files with 170 additions and 129 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/code-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ jobs:
- name: Composer
run: composer install --no-progress

- name: Check CS
run: vendor/bin/ecs

- name: PHPUnit
run: vendor/bin/phpunit --coverage-clover=coverage.xml

Expand Down
6 changes: 6 additions & 0 deletions .runConfigurations/All unit tests.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="All unit tests" type="PHPUnitRunConfigurationType" factoryName="PHPUnit">
<TestRunner configuration_file="$PROJECT_DIR$/phpunit.xml.dist" scope="XML" options="--verbose" />
<method v="2" />
</configuration>
</component>
10 changes: 10 additions & 0 deletions .runConfigurations/EasyCodingStandard.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="EasyCodingStandard" type="PhpLocalRunConfigurationType" factoryName="PHP Console" path="$PROJECT_DIR$/vendor/bin/ecs" scriptParameters="--fix">
<CommandLine workingDirectory="$PROJECT_DIR$">
<envs>
<env name="XDEBUG_MODE" value="off" />
</envs>
</CommandLine>
<method v="2" />
</configuration>
</component>
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
},

"require-dev": {
"phpunit/phpunit": "^9"
"phpunit/phpunit": "^9",
"symplify/easy-coding-standard": "^12"
},

"autoload-dev": {
Expand Down
35 changes: 35 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;

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

$ecsConfig->sets([
SetList::COMMON,
SetList::PSR_12,
]);

$ecsConfig->skip([
// Remove sniff, from common/control-structures
\PhpCsFixer\Fixer\ClassNotation\OrderedClassElementsFixer::class,

// Remove sniff, from common/spaces
\PhpCsFixer\Fixer\Operator\NotOperatorWithSuccessorSpaceFixer::class,
\PhpCsFixer\Fixer\CastNotation\CastSpacesFixer::class,
]);

// PER Coding Style 7.1: "The `fn` keyword MUST NOT be succeeded by a space."
$ecsConfig->ruleWithConfiguration(
\PhpCsFixer\Fixer\FunctionNotation\FunctionDeclarationFixer::class,
[
'closure_fn_spacing' => 'none',
]
);
};
4 changes: 2 additions & 2 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public function getLoggerConfig(string $name): array

if (!isset($loggerConfig['type'])) {
$loggerConfig = [
'type' => 'collection',
'loggers' => $loggerConfig
'type' => 'collection',
'loggers' => $loggerConfig,
];
}

Expand Down
1 change: 0 additions & 1 deletion src/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@
*/
interface ConfigInterface
{

public function getLoggerConfig(string $name): array;
}
1 change: 0 additions & 1 deletion src/Decorator/AbstractDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ abstract class AbstractDecorator extends AbstractLogger
* If the concrete requires use of the config value, override the constructor
* to add validation and store for re-use
*
* @param LoggerInterface $logger
* @param mixed $config
*/
public function __construct(LoggerInterface $logger, $config)
Expand Down
6 changes: 3 additions & 3 deletions src/Decorator/LevelFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Phlib\Logger\Decorator;

use Psr\Log\LogLevel;
use Psr\Log\InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

/**
* Class LevelFilter
Expand All @@ -17,7 +17,7 @@ class LevelFilter extends AbstractDecorator
/**
* Logging levels from syslog protocol defined in RFC 5424
*
* @var string[] $levels Logging levels
* @var string[] Logging levels
*/
private static $levels = [
LogLevel::EMERGENCY, // 0
Expand All @@ -27,7 +27,7 @@ class LevelFilter extends AbstractDecorator
LogLevel::WARNING, // 4
LogLevel::NOTICE, // 5
LogLevel::INFO, // 6
LogLevel::DEBUG // 7
LogLevel::DEBUG, // 7
];

/**
Expand Down
11 changes: 4 additions & 7 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,20 @@ class Factory
*/
private $decorators = [
'defaultContext' => \Phlib\Logger\Decorator\DefaultContext::class,
'level' => \Phlib\Logger\Decorator\LevelFilter::class
'level' => \Phlib\Logger\Decorator\LevelFilter::class,
];

/**
* Register a new decorator class
*
* Class must implement DecoratorInterface
*
* @param string $configKey
* @param string $className
*/
public function registerDecorator(string $configKey, string $className): void
{
if (isset($this->decorators[$configKey])) {
throw new \RuntimeException('Decorator key already in use: ' . $configKey);
}
if (in_array($className, $this->decorators)) {
if (in_array($className, $this->decorators, true)) {
throw new \RuntimeException('Decorator class already registered: ' . $className);
}
$this->decorators[$configKey] = $className;
Expand All @@ -60,7 +57,7 @@ public function createLogger(string $name, array $config): LoggerInterface
if (!method_exists($this, $methodName)) {
throw new \DomainException(sprintf('Cannot find a logger type named "%s"', $type));
}
$logger = $this->$methodName($name, $config);
$logger = $this->{$methodName}($name, $config);

$logger = $this->applyDecorators($logger, $config);

Expand Down Expand Up @@ -114,7 +111,7 @@ public function createGelfLogger(string $name, array $config): \Gelf\Logger
$host = $config['host'] ?? false;
$port = $config['port'] ?? 12201;

$transport = new \Gelf\Transport\UdpTransport($host, $port);
$transport = new \Gelf\Transport\UdpTransport($host, $port);
$messagePublisher = new \Gelf\Publisher($transport);

return new \Gelf\Logger($messagePublisher, $name);
Expand Down
19 changes: 8 additions & 11 deletions src/LoggerType/CliColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,27 @@ class CliColor extends Stream

/**
* @see Stream::__construct()
* @param string $name
* @param resource|string $stream Optional. Default to Standard Error
*/
public function __construct(string $name, $stream = STDERR)
{
parent::__construct($name, $stream);

$this->formatter = new OutputFormatter(true, [
'debug' => new OutputFormatterStyle(),
'info' => new OutputFormatterStyle('blue'),
'notice' => new OutputFormatterStyle('green'),
'warning' => new OutputFormatterStyle('yellow'),
'error' => new OutputFormatterStyle('red'),
'critical' => new OutputFormatterStyle('red', 'yellow'),
'alert' => new OutputFormatterStyle('white', 'red', ['bold']),
'emergency' => new OutputFormatterStyle('white', 'red', ['bold', 'underscore'])
'debug' => new OutputFormatterStyle(),
'info' => new OutputFormatterStyle('blue'),
'notice' => new OutputFormatterStyle('green'),
'warning' => new OutputFormatterStyle('yellow'),
'error' => new OutputFormatterStyle('red'),
'critical' => new OutputFormatterStyle('red', 'yellow'),
'alert' => new OutputFormatterStyle('white', 'red', ['bold']),
'emergency' => new OutputFormatterStyle('white', 'red', ['bold', 'underscore']),
]);
}

/**
* @see Stream::getMessageFormat()
* @param mixed $level
* @param array $context
* @return string
*/
protected function getMessageFormat($level, array $context = []): string
{
Expand Down
1 change: 0 additions & 1 deletion src/LoggerType/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
class Collection extends AbstractLogger
{

/**
* @var \SplObjectStorage
*/
Expand Down
17 changes: 5 additions & 12 deletions src/LoggerType/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class Stream extends AbstractLogger
private $dateFormat = 'Y-m-d H:i:s';

/**
* @param string $name
* @param resource|string $stream
*/
public function __construct(string $name, $stream)
Expand Down Expand Up @@ -62,8 +61,6 @@ public function setMessageFormat(string $format): self
* This method can be overridden by extending classes to modify the behaviour
*
* @param mixed $level
* @param array $context
* @return string
*/
protected function getMessageFormat($level, array $context = []): string
{
Expand All @@ -85,10 +82,10 @@ public function log($level, $message, array $context = []): void
{
$meta = [
'datetime' => date($this->dateFormat),
'name' => $this->name,
'level' => $level,
'message' => $this->formatMessage((string)$message, $context),
'context' => $this->formatContext($context)
'name' => $this->name,
'level' => $level,
'message' => $this->formatMessage((string)$message, $context),
'context' => $this->formatContext($context),
];

$message = static::interpolate($this->getMessageFormat($level, $context), $meta);
Expand Down Expand Up @@ -123,8 +120,6 @@ protected function formatContext(array $context): string
* trying to ensure no error, warning or notice is happening
*
* @param mixed $message
* @param array $context
* @return string
*/
private static function interpolate($message, array $context): string
{
Expand All @@ -144,16 +139,14 @@ private static function interpolate($message, array $context): string
* Converts a context value into its appropriate string representation
*
* @param mixed $val
* @return string
*/
private static function contextValueToString($val): string
{

if (is_bool($val)) {
return var_export($val, true);
} elseif (is_scalar($val)) {
return (string)$val;
} elseif (is_null($val)) {
} elseif ($val === null) {
return 'NULL';
} elseif (is_object($val)) {
if (is_callable([$val, '__toString'])) {
Expand Down
5 changes: 2 additions & 3 deletions src/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
class Pool
{

/**
* @var ConfigInterface
*/
Expand All @@ -36,7 +35,7 @@ class Pool

public function __construct(ConfigInterface $config, Factory $loggerFactory)
{
$this->config = $config;
$this->config = $config;
$this->loggerFactory = $loggerFactory;
}

Expand Down Expand Up @@ -65,7 +64,7 @@ public function getLoggerCollection(string $name): Collection

if (!$logger instanceof Collection) {
$logger = $this->loggerFactory->createCollectionLogger($name, [
'loggers' => [$logger]
'loggers' => [$logger],
]);
}

Expand Down
Loading

0 comments on commit 05eb525

Please sign in to comment.