Skip to content

Commit

Permalink
Run PHP-CS-Fixer automatically (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpdude authored Jul 1, 2024
1 parent bb3b78c commit 63c4e68
Show file tree
Hide file tree
Showing 24 changed files with 151 additions and 135 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/fix-cs-php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Update this by running
# curl https://gist.github.com/mpdude/ca93a185bcbf56eb7e341632ad4f8263/raw/fix-cs-php.yml > .github/workflows/fix-cs-php.yml

on:
push:
branches:
- master
pull_request:

name: Coding Standards

jobs:
fix-cs-issues:
name: PHP-CS-Fixer
runs-on: ubuntu-22.04
if: github.actor != 'dependabot[bot]'
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}

- name: Run PHP-CS-Fixer
uses: docker://oskarstark/php-cs-fixer-ga

- name: Commit and push back changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "Fix CS with PHP-CS-Fixer"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ vendor/
phpunit.xml
composer.lock
.phpunit.result.cache
.php-cs-fixer.cache
19 changes: 19 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

return (new PhpCsFixer\Config())
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'array_syntax' => array('syntax' => 'short'),
'no_unreachable_default_argument_value' => false,
'braces' => array('allow_single_line_closure' => true),
'heredoc_to_nowdoc' => false,
'psr_autoloading' => false,
])
->setRiskyAllowed(true)
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__)
->notPath('vendor/')
)
;
8 changes: 4 additions & 4 deletions src/JMS/ObjectRouting/Attribute/ObjectRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

/*
* Copyright 2013 Johannes M. Schmitt <[email protected]>
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -28,7 +28,7 @@ final class ObjectRoute
public $name;

/** @var array */
public $params = array();
public $params = [];

public function __construct(string $type, string $name, array $params = [])
{
Expand Down
4 changes: 1 addition & 3 deletions src/JMS/ObjectRouting/Exception/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace JMS\ObjectRouting\Exception;


interface Exception
{

}
}
4 changes: 1 addition & 3 deletions src/JMS/ObjectRouting/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace JMS\ObjectRouting\Exception;


class RuntimeException extends \RuntimeException implements Exception
{

}
}
9 changes: 4 additions & 5 deletions src/JMS/ObjectRouting/Exception/XmlErrorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

namespace JMS\ObjectRouting\Exception;


class XmlErrorException extends RuntimeException
{
private $xmlError;

public function __construct(\LibXMLError $error)
{
switch ($error->level) {
case LIBXML_ERR_WARNING:
case \LIBXML_ERR_WARNING:
$level = 'WARNING';
break;
case LIBXML_ERR_FATAL:
case \LIBXML_ERR_FATAL:
$level = 'FATAL';
break;
case LIBXML_ERR_ERROR:
case \LIBXML_ERR_ERROR:
$level = 'ERROR';
break;
default:
Expand All @@ -30,4 +29,4 @@ public function getXmlError()
{
return $this->xmlError;
}
}
}
21 changes: 10 additions & 11 deletions src/JMS/ObjectRouting/Metadata/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

/*
* Copyright 2013 Johannes M. Schmitt <[email protected]>
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -23,14 +23,14 @@

class ClassMetadata extends MergeableClassMetadata
{
public $routes = array();
public $routes = [];

public function addRoute($type, $name, array $params = array())
public function addRoute($type, $name, array $params = [])
{
$this->routes[$type] = array(
$this->routes[$type] = [
'name' => $name,
'params' => $params,
);
];
}

public function merge(MergeableInterface $object): void
Expand All @@ -42,10 +42,10 @@ public function merge(MergeableInterface $object): void
public function serialize(): string
{
return serialize(
array(
[
$this->routes,
parent::serialize(),
)
]
);
}

Expand All @@ -54,9 +54,8 @@ public function unserialize($str): void
list(
$this->routes,
$parentStr
) = unserialize($str);
) = unserialize($str);

parent::unserialize($parentStr);
}

}
6 changes: 3 additions & 3 deletions src/JMS/ObjectRouting/Metadata/Driver/AttributeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

/*
* Copyright 2013 Johannes M. Schmitt <[email protected]>
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down
5 changes: 2 additions & 3 deletions src/JMS/ObjectRouting/Metadata/Driver/PhpDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@

namespace JMS\ObjectRouting\Metadata\Driver;


use JMS\ObjectRouting\Exception\RuntimeException;
use JMS\ObjectRouting\Metadata\ClassMetadata;
use Metadata\Driver\AbstractFileDriver;

/**
* Class PhpDriver
* @package JMS\ObjectRouting\Metadata\Driver
* Class PhpDriver.
*
* @author Sebastian Kroczek <[email protected]>
*/
class PhpDriver extends AbstractFileDriver
Expand Down
23 changes: 8 additions & 15 deletions src/JMS/ObjectRouting/Metadata/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,21 @@

namespace JMS\ObjectRouting\Metadata\Driver;


use JMS\ObjectRouting\Exception\RuntimeException;
use JMS\ObjectRouting\Exception\XmlErrorException;
use JMS\ObjectRouting\Metadata\ClassMetadata;
use Metadata\Driver\AbstractFileDriver;

/**
* Class XmlDriver
* @package JMS\ObjectRouting\Metadata\Driver
* Class XmlDriver.
*
* @author Sebastian Kroczek <[email protected]>
*/
class XmlDriver extends AbstractFileDriver
{

/**
* Parses the content of the file, and converts it to the desired metadata.
*
* @param \ReflectionClass $class
* @param string $file
*
* @return \Metadata\ClassMetadata|null
*/
protected function loadMetadataFromFile(\ReflectionClass $class, string $file): ?ClassMetadata
Expand All @@ -57,23 +52,23 @@ protected function loadMetadataFromFile(\ReflectionClass $class, string $file):
$metadata->fileResources[] = $class->getFileName();

if (null !== $xmlRootName = $elem->attributes()->{'xml-root-name'}) {
$metadata->xmlRootName = (string)$xmlRootName;
$metadata->xmlRootName = (string) $xmlRootName;
}
if (null !== $xmlRootNamespace = $elem->attributes()->{'xml-root-namespace'}) {
$metadata->xmlRootNamespace = (string)$xmlRootNamespace;
$metadata->xmlRootNamespace = (string) $xmlRootNamespace;
}

foreach ($elem->xpath('./route') as $r) {
if ('' === $type = (string)$r->attributes()->{'type'}) {
if ('' === $type = (string) $r->attributes()->{'type'}) {
throw new RuntimeException('Could not find attribute "type" inside XML element.');
}
if ('' === $name = (string)$r->attributes()->{'name'}) {
if ('' === $name = (string) $r->attributes()->{'name'}) {
throw new RuntimeException('Could not find attribute "name" inside XML element.');
}

$params = array();
$params = [];
foreach ($r->xpath('./param') as $p) {
$params[(string)$p->attributes()] = (string)$p;
$params[(string) $p->attributes()] = (string) $p;
}

$metadata->addRoute($type, $name, $params);
Expand All @@ -84,8 +79,6 @@ protected function loadMetadataFromFile(\ReflectionClass $class, string $file):

/**
* Returns the extension of the file.
*
* @return string
*/
protected function getExtension(): string
{
Expand Down
17 changes: 4 additions & 13 deletions src/JMS/ObjectRouting/Metadata/Driver/YamlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,15 @@
use Symfony\Component\Yaml\Yaml;

/**
* Class YamlDriver
* @package JMS\ObjectRouting\Metadata\Driver
* Class YamlDriver.
*
* @author Sebastian Kroczek <[email protected]>
*/
class YamlDriver extends AbstractFileDriver
{


/**
* Parses the content of the file, and converts it to the desired metadata.
*
* @param \ReflectionClass $class
* @param string $file
*
* @return \Metadata\ClassMetadata|null
*/
protected function loadMetadataFromFile(\ReflectionClass $class, string $file): ?ClassMetadata
Expand All @@ -48,27 +43,23 @@ protected function loadMetadataFromFile(\ReflectionClass $class, string $file):
throw new RuntimeException(sprintf('Expected metadata for class %s to be defined in %s.', $class->name, $file));
}


$config = $config[$name];
$metadata = new ClassMetadata($name);
$metadata->fileResources[] = $file;
$metadata->fileResources[] = $class->getFileName();

foreach ($config as $type => $value) {
if (!array_key_exists('name', $value)) {
if (!\array_key_exists('name', $value)) {
throw new RuntimeException('Could not find key "type" inside yaml element.');
}
$metadata->addRoute($type, $value['name'], array_key_exists('params', $value) ? $value['params'] : array());
$metadata->addRoute($type, $value['name'], \array_key_exists('params', $value) ? $value['params'] : []);
}

return $metadata;

}

/**
* Returns the extension of the file.
*
* @return string
*/
protected function getExtension(): string
{
Expand Down
Loading

0 comments on commit 63c4e68

Please sign in to comment.