Browse Source

Merge branch 'master' into 3.0

* master:
  HeaderCommentFixer - throw exception on invalid header configuration

# Conflicts:
#	src/Fixer/Comment/HeaderCommentFixer.php
SpacePossum 6 years ago
parent
commit
dba297f736

+ 8 - 1
src/Fixer/Comment/HeaderCommentFixer.php

@@ -13,6 +13,7 @@
 namespace PhpCsFixer\Fixer\Comment;
 
 use PhpCsFixer\AbstractFixer;
+use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
 use PhpCsFixer\Fixer\ConfigurableFixerInterface;
 use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
 use PhpCsFixer\FixerConfiguration\AliasedFixerOptionBuilder;
@@ -138,14 +139,20 @@ echo 1;
      */
     protected function createConfigurationDefinition()
     {
+        $fixerName = $this->getName();
+
         return new FixerConfigurationResolver([
             (new FixerOptionBuilder('header', 'Proper header content.'))
                 ->setAllowedTypes(['string'])
-                ->setNormalizer(static function (Options $options, $value) {
+                ->setNormalizer(static function (Options $options, $value) use ($fixerName) {
                     if ('' === trim($value)) {
                         return '';
                     }
 
+                    if (false !== strpos($value, '*/')) {
+                        throw new InvalidFixerConfigurationException($fixerName, 'Cannot use \'*/\' in header.');
+                    }
+
                     return $value;
                 })
                 ->getOption(),

+ 12 - 0
tests/Fixer/Comment/HeaderCommentFixerTest.php

@@ -12,6 +12,7 @@
 
 namespace PhpCsFixer\Tests\Fixer\Comment;
 
+use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
 use PhpCsFixer\Tests\Test\AbstractFixerWithAliasedOptionsTestCase;
 use PhpCsFixer\Tokenizer\Tokens;
 use PhpCsFixer\WhitespacesFixerConfig;
@@ -572,4 +573,15 @@ declare(strict_types=1)?>',
             "<?php\necho 1;"
         );
     }
+
+    public function testInvalidHeaderConfiguration()
+    {
+        $this->expectException(InvalidFixerConfigurationException::class);
+        $this->expectExceptionMessageRegExp('#^\[header_comment\] Cannot use \'\*/\' in header\.$#');
+
+        $this->fixer->configure([
+            'header' => '/** test */',
+            'comment_type' => 'PHPDoc',
+        ]);
+    }
 }