From 568adedb703787930dcf5428e2cd5f7dd47eccee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20L=C3=A9v=C3=AAque?= Date: Tue, 1 Dec 2015 15:47:33 +0100 Subject: [PATCH] respect our convention --- .php_cs | 30 +++++++++++ DependencyInjection/Configuration.php | 9 +++- .../ElaoErrorNotifierExtension.php | 14 +++-- ElaoErrorNotifierBundle.php | 8 +++ Exception/InvokerException.php | 9 ++++ Listener/Notifier.php | 46 +++++++++------- Twig/DumpyTwigFilter.php | 53 +++++++++++-------- 7 files changed, 124 insertions(+), 45 deletions(-) create mode 100644 .php_cs diff --git a/.php_cs b/.php_cs new file mode 100644 index 0000000..c5f822a --- /dev/null +++ b/.php_cs @@ -0,0 +1,30 @@ + +EOF; + +Symfony\CS\Fixer\Contrib\HeaderCommentFixer::setHeader($header); + +return Symfony\CS\Config\Config::create() + // use default SYMFONY_LEVEL and extra fixers: + ->fixers([ + '-concat_without_spaces', + '-phpdoc_short_description', + '-pre_increment', + '-unalign_double_arrow', + '-unalign_equals', + 'align_double_arrow', + 'align_equals', + 'concat_with_spaces', + 'header_comment', + 'ordered_use', + 'phpdoc_order', + ]) + ->setUsingCache(true) + ->finder(Symfony\CS\Finder\DefaultFinder::create()->in(__DIR__)) +; diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index de7afde..d3b769c 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -1,5 +1,13 @@ + */ + namespace Elao\ErrorNotifierBundle\DependencyInjection; use Symfony\Component\Config\Definition\Builder\TreeBuilder; @@ -10,7 +18,6 @@ */ class Configuration implements ConfigurationInterface { - /** * Get config tree * diff --git a/DependencyInjection/ElaoErrorNotifierExtension.php b/DependencyInjection/ElaoErrorNotifierExtension.php index 44de9e7..aa59846 100644 --- a/DependencyInjection/ElaoErrorNotifierExtension.php +++ b/DependencyInjection/ElaoErrorNotifierExtension.php @@ -1,12 +1,20 @@ + */ + namespace Elao\ErrorNotifierBundle\DependencyInjection; use Symfony\Component\Config\FileLocator; -use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; +use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\HttpKernel\DependencyInjection\Extension; /** * ElaoErrorNotifier Extension @@ -28,7 +36,7 @@ public function load(array $configs, ContainerBuilder $container) $container->setParameter('elao.error_notifier.config', $config); - $loader = new XmlFileLoader($container, new FileLocator(array(__DIR__.'/../Resources/config/'))); + $loader = new XmlFileLoader($container, new FileLocator(array(__DIR__ . '/../Resources/config/'))); $loader->load('services.xml'); if ($config['mailer'] != 'mailer') { diff --git a/ElaoErrorNotifierBundle.php b/ElaoErrorNotifierBundle.php index fe15d4d..e7a5370 100644 --- a/ElaoErrorNotifierBundle.php +++ b/ElaoErrorNotifierBundle.php @@ -1,5 +1,13 @@ + */ + namespace Elao\ErrorNotifierBundle; use Symfony\Component\HttpKernel\Bundle\Bundle; diff --git a/Exception/InvokerException.php b/Exception/InvokerException.php index 8fde21d..6a22521 100755 --- a/Exception/InvokerException.php +++ b/Exception/InvokerException.php @@ -1,9 +1,18 @@ + */ + namespace Elao\ErrorNotifierBundle\Exception; /** * Exception which is thrown when a method cannot be invoked + * * @see Elao\ErrornotifierBundle\Twig\Dumpy */ class InvokerException extends \Exception diff --git a/Listener/Notifier.php b/Listener/Notifier.php index fee17df..05413c1 100644 --- a/Listener/Notifier.php +++ b/Listener/Notifier.php @@ -1,19 +1,27 @@ + */ + namespace Elao\ErrorNotifierBundle\Listener; use Swift_Mailer; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Event\ConsoleCommandEvent; +use Symfony\Component\Console\Event\ConsoleExceptionEvent; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Debug\Exception\FlattenException; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\Templating\EngineInterface; -use Symfony\Component\HttpKernel\HttpKernelInterface; -use Symfony\Component\HttpKernel\Exception\HttpException; -use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpKernel\Event\GetResponseEvent; -use Symfony\Component\Console\Event\ConsoleExceptionEvent; -use Symfony\Component\Console\Event\ConsoleCommandEvent; -use Symfony\Component\Debug\Exception\FlattenException; +use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; +use Symfony\Component\HttpKernel\Exception\HttpException; +use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\Templating\EngineInterface; /** * Notifier @@ -104,13 +112,13 @@ public function onKernelException(GetResponseForExceptionEvent $event) } if (strlen($this->ignoredAgentsPattern)) { - if (preg_match('#'.$this->ignoredAgentsPattern.'#', $event->getRequest()->headers->get('User-Agent'))) { + if (preg_match('#' . $this->ignoredAgentsPattern . '#', $event->getRequest()->headers->get('User-Agent'))) { return; } } if (strlen($this->ignoredUrlsPattern)) { - if (preg_match('#'.$this->ignoredUrlsPattern.'#', $event->getRequest()->getUri())) { + if (preg_match('#' . $this->ignoredUrlsPattern . '#', $event->getRequest()->getUri())) { return; } } @@ -171,7 +179,7 @@ public function onConsoleCommand(ConsoleCommandEvent $event) { $this->request = null; - $this->command = $event->getCommand(); + $this->command = $event->getCommand(); $this->commandInput = $event->getInput(); if ($this->reportErrors || $this->reportWarnings) { @@ -264,7 +272,8 @@ public function handlePhpFatalErrorAndWarnings() /** * Convert the error code to a readable format * - * @param integer $errorNo + * @param int $errorNo + * * @return string */ public function getErrorString($errorNo) @@ -313,15 +322,15 @@ public function createMailAndSend($exception, Request $request = null, $context 'status_code' => $exception->getCode(), 'context' => $context, 'command' => $command, - 'command_input' => $commandInput + 'command_input' => $commandInput, )); if ($this->request) { - $subject = '['.$request->headers->get('host').'] Error '.$exception->getStatusCode().': '.$exception->getMessage(); + $subject = '[' . $request->headers->get('host') . '] Error ' . $exception->getStatusCode() . ': ' . $exception->getMessage(); } elseif ($this->command) { - $subject = '['.$this->command->getName().'] Error '.$exception->getStatusCode().': '.$exception->getMessage(); + $subject = '[' . $this->command->getName() . '] Error ' . $exception->getStatusCode() . ': ' . $exception->getMessage(); } else { - $subject = 'Error '.$exception->getStatusCode().': '.$exception->getMessage(); + $subject = 'Error ' . $exception->getStatusCode() . ': ' . $exception->getMessage(); } if (function_exists('mb_substr')) { @@ -343,13 +352,14 @@ public function createMailAndSend($exception, Request $request = null, $context /** * Check last send time * - * @param FlattenException $exception + * @param FlattenException $exception + * * @return bool */ private function checkRepeat(FlattenException $exception) { - $key = md5($exception->getMessage().':'.$exception->getLine().':'.$exception->getFile()); - $file = $this->errorsDir.'/'.$key; + $key = md5($exception->getMessage() . ':' . $exception->getLine() . ':' . $exception->getFile()); + $file = $this->errorsDir . '/' . $key; $time = is_file($file) ? file_get_contents($file) : 0; if ($time < time()) { file_put_contents($file, time() + $this->repeatTimeout); diff --git a/Twig/DumpyTwigFilter.php b/Twig/DumpyTwigFilter.php index 4f8d6f5..5221ea3 100755 --- a/Twig/DumpyTwigFilter.php +++ b/Twig/DumpyTwigFilter.php @@ -1,5 +1,17 @@ + */ +namespace Elao\ErrorNotifierBundle\Twig; + +use Elao\ErrorNotifierBundle\Exception\InvokerException; +use Symfony\Component\Yaml\Dumper as YamlDumper; + /** * Extends Twig with * {{ "my string, whatever" | pre }} --> wraps with
@@ -21,14 +33,9 @@
  *   Default value is 1. (MAX_DEPTH const)
  *
  * @see https://gist.github.com/1747036
+ *
  * @author Goutte
  */
-
-namespace Elao\ErrorNotifierBundle\Twig;
-
-use Symfony\Component\Yaml\Dumper as YamlDumper;
-use Elao\ErrorNotifierBundle\Exception\InvokerException;
-
 class DumpyTwigFilter extends \Twig_Extension
 {
     /** @const INLINE : default value for the inline parameter of the YAML dumper aka the expanding-level */
@@ -52,7 +59,7 @@ public function getFilters()
 
     public function pre($stringable)
     {
-        return "
" . (string) $stringable . "
"; + return '
' . (string) $stringable . '
'; } public function preDump($values) @@ -69,7 +76,8 @@ public function preYamlDump($values, $depth = self::MAX_DEPTH) * Encodes as YAML the passed $input * * @param $input - * @param int $inline + * @param int $inline + * * @return mixed */ public function encode($input, $inline = self::INLINE) @@ -86,8 +94,9 @@ public function encode($input, $inline = self::INLINE) /** * Returns a templating-helper dump of depth-sanitized var as yaml string * - * @param mixed $value What to dump - * @param int $depth Recursion max depth + * @param mixed $value What to dump + * @param int $depth Recursion max depth + * * @return string */ public function yamlDump($value, $depth = self::MAX_DEPTH) @@ -101,8 +110,9 @@ public function yamlDump($value, $depth = self::MAX_DEPTH) * A bit dirty as this should be in another Class, but hey * * @param $value - * @param int $maxRecursionDepth The maximum depth of recursion - * @param int $recursionDepth The depth of recursion (used internally) + * @param int $maxRecursionDepth The maximum depth of recursion + * @param int $recursionDepth The depth of recursion (used internally) + * * @return array|string */ public function sanitize($value, $maxRecursionDepth = self::MAX_DEPTH, $recursionDepth = 0) @@ -143,10 +153,9 @@ public function sanitize($value, $maxRecursionDepth = self::MAX_DEPTH, $recursio } return $classInfo; - } else { // Get all accessors and their values - $data = array(); - $data['class'] = '' . $class->getShortName() . ''; + $data = array(); + $data['class'] = '' . $class->getShortName() . ''; if ($class->isIterateable()) { $data['iterateable'] = $this->sanitizeIterateable($value, $maxRecursionDepth, $recursionDepth); } else { @@ -160,18 +169,16 @@ public function sanitize($value, $maxRecursionDepth = self::MAX_DEPTH, $recursio $methodInfo = ($method->getNumberOfParameters() ? substr($methodInfo, 0, -2) : $methodInfo) . ')'; if (!$method->getNumberOfRequiredParameters()) { // Get the value, we don't need params try { - $methodValue = $method->invoke($value); + $methodValue = $method->invoke($value); $data['accessors'][$methodInfo] = $this->sanitize($methodValue, $maxRecursionDepth, $recursionDepth + 1); } catch (\Exception $e) { $data['accessors'][$methodInfo] = $this->sanitize(new InvokerException('Couldn\'t invoke method: Exception "' . get_class($e) . '" with message "' . $e->getMessage() . '"'), $maxRecursionDepth, $recursionDepth + 1); } - } else { // Get only method name and its params $data['accessors'][] = $methodInfo; } } } - } return $data; @@ -179,15 +186,15 @@ public function sanitize($value, $maxRecursionDepth = self::MAX_DEPTH, $recursio } if (is_string($value)) { - $value = '(string) '.$value; + $value = '(string) ' . $value; } if (is_int($value)) { - $value = '(int) '.$value; + $value = '(int) ' . $value; } if (is_float($value)) { - $value = '(float) '.$value; + $value = '(float) ' . $value; } if (is_null($value)) { @@ -208,9 +215,9 @@ public function sanitize($value, $maxRecursionDepth = self::MAX_DEPTH, $recursio public function sanitizeIterateable($value, $maxRecursionDepth = self::MAX_DEPTH, $recursionDepth = 0) { if ($recursionDepth < $maxRecursionDepth) { - $r = array (); + $r = array(); $arrayCount = count($value); - $count = 0; + $count = 0; foreach ($value as $k => $v) { $r[$k] = $this->sanitize($v, $maxRecursionDepth, $recursionDepth + 1);