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 361ec5f..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 * @@ -27,7 +34,9 @@ public function getConfigTreeBuilder() ->arrayNode('to') ->beforeNormalization() ->ifString() - ->then(function($value) { return array($value); }) + ->then(function ($value) { + return array($value); + }) ->end() ->isRequired() ->cannotBeEmpty() diff --git a/DependencyInjection/ElaoErrorNotifierExtension.php b/DependencyInjection/ElaoErrorNotifierExtension.php index 30ae8b8..aa59846 100644 --- a/DependencyInjection/ElaoErrorNotifierExtension.php +++ b/DependencyInjection/ElaoErrorNotifierExtension.php @@ -1,27 +1,31 @@ + */ + namespace Elao\ErrorNotifierBundle\DependencyInjection; use Symfony\Component\Config\FileLocator; -use Symfony\Component\DependencyInjection\Loader; -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 */ class ElaoErrorNotifierExtension extends Extension { - /** * load configuration * * @param array $configs configs * @param ContainerBuilder $container container - * - * @return void */ public function load(array $configs, ContainerBuilder $container) { @@ -32,11 +36,12 @@ 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') { - $container->getDefinition('elao.error_notifier.listener')->replaceArgument(0, new Reference($config['mailer'])); + $definition = $container->getDefinition('elao.error_notifier.listener'); + $definition->replaceArgument(0, new Reference($config['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 4103ccb..05413c1 100644 --- a/Listener/Notifier.php +++ b/Listener/Notifier.php @@ -1,33 +1,40 @@ + */ + namespace Elao\ErrorNotifierBundle\Listener; -use \Swift_Mailer; +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 */ class Notifier { - /** - * @var Swift_Mailer $mailer + * @var Swift_Mailer */ private $mailer; /** - * @var EngineInterface $templating + * @var EngineInterface */ private $templating; @@ -105,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; } } @@ -172,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) { @@ -198,10 +205,10 @@ protected function setErrorHandlers() /** * @see http://php.net/set_error_handler * - * @param integer $level - * @param string $message - * @param string $file - * @param integer $line + * @param int $level + * @param string $message + * @param string $file + * @param int $line * * @throws ErrorException */ @@ -220,7 +227,7 @@ public function handlePhpError($level, $message, $file, $line, $errcontext) return false; } - if(in_array($message, $this->ignoredPhpErrors)) { + if (in_array($message, $this->ignoredPhpErrors)) { return false; } @@ -257,7 +264,7 @@ public function handlePhpFatalErrorAndWarnings() } if (in_array($lastError['type'], $errors) && !in_array(@$lastError['message'], $this->ignoredPhpErrors)) { - $exception = new \ErrorException(sprintf('%s: %s in %s line %d', @$this->getErrorString(@$lastError['type']), @$lastError['message'], @$lastError['file'], @$lastError['line']), @$lastError['type'], @$lastError['type'], @$lastError['file'], @$lastError['line']); + $exception = new \ErrorException(sprintf('%s: %s in %s line %d', @$this->getErrorString(@$lastError['type']), @$lastError['message'], @$lastError['file'], @$lastError['line']), @$lastError['type'], @$lastError['type'], @$lastError['file'], @$lastError['line']); $this->createMailAndSend($exception, $this->request, null, $this->command, $this->commandInput); } } @@ -265,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) @@ -314,12 +322,12 @@ 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) { + if ($this->request) { $subject = '[' . $request->headers->get('host') . '] Error ' . $exception->getStatusCode() . ': ' . $exception->getMessage(); - } elseif($this->command) { + } elseif ($this->command) { $subject = '[' . $this->command->getName() . '] Error ' . $exception->getStatusCode() . ': ' . $exception->getMessage(); } else { $subject = 'Error ' . $exception->getStatusCode() . ': ' . $exception->getMessage(); @@ -344,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); @@ -373,5 +382,4 @@ protected static function _freeMemory() { self::$tmpBuffer = ''; } - } diff --git a/Twig/DumpyTwigFilter.php b/Twig/DumpyTwigFilter.php index 163b8f7..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,18 +110,19 @@ 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) + public function sanitize($value, $maxRecursionDepth = self::MAX_DEPTH, $recursionDepth = 0) { if (is_resource($value)) { return 'Resource'; } if (is_array($value)) { - return $this->sanitizeIterateable ($value, $maxRecursionDepth, $recursionDepth); + return $this->sanitizeIterateable($value, $maxRecursionDepth, $recursionDepth); } if ($value instanceof InvokerException) { @@ -143,12 +153,11 @@ public function sanitize ($value, $maxRecursionDepth = self::MAX_DEPTH, $recursi } 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); + $data['iterateable'] = $this->sanitizeIterateable($value, $maxRecursionDepth, $recursionDepth); } else { $data['accessors'] = array(); foreach ($class->getMethods() as $method) { @@ -160,18 +169,16 @@ public function sanitize ($value, $maxRecursionDepth = self::MAX_DEPTH, $recursi $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, $recursi } 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)) { @@ -205,25 +212,26 @@ public function sanitize ($value, $maxRecursionDepth = self::MAX_DEPTH, $recursi return $value; } - public function sanitizeIterateable ($value, $maxRecursionDepth = self::MAX_DEPTH, $recursionDepth = 0) + 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); $count++; - if($count >= 20) { + if ($count >= 20) { $r[] = sprintf('... and %s more ...', ($arrayCount - $count)); break; } } - + return $r; } else { - $c = count($value); $t = gettype($value); + $c = count($value); + $t = gettype($value); return $c ? "$t of $c" : "empty $t"; } diff --git a/composer.json b/composer.json index b3b5189..2b4e2b8 100644 --- a/composer.json +++ b/composer.json @@ -19,8 +19,8 @@ "require": { "php": ">=5.3.2", - "symfony/framework-bundle": "~2.3", - "symfony/console": "~2.3", + "symfony/framework-bundle": "~2.3|~3.0", + "symfony/console": "~2.3|~3.0", "swiftmailer/swiftmailer": "~5.0" },