Browse Source

chore: ConfigInterface - better types (#8244)

Dariusz Rumiński 5 months ago
parent
commit
2e9423c80f

+ 21 - 0
src/Config.php

@@ -25,6 +25,9 @@ use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
  */
 class Config implements ConfigInterface, ParallelAwareConfigInterface
 {
+    /**
+     * @var non-empty-string
+     */
     private string $cacheFile = '.php-cs-fixer.cache';
 
     /**
@@ -41,10 +44,16 @@ class Config implements ConfigInterface, ParallelAwareConfigInterface
 
     private bool $hideProgress = false;
 
+    /**
+     * @var non-empty-string
+     */
     private string $indent = '    ';
 
     private bool $isRiskyAllowed = false;
 
+    /**
+     * @var non-empty-string
+     */
     private string $lineEnding = "\n";
 
     private string $name;
@@ -84,6 +93,9 @@ class Config implements ConfigInterface, ParallelAwareConfigInterface
         }
     }
 
+    /**
+     * @return non-empty-string
+     */
     public function getCacheFile(): string
     {
         return $this->cacheFile;
@@ -163,6 +175,9 @@ class Config implements ConfigInterface, ParallelAwareConfigInterface
         return $this;
     }
 
+    /**
+     * @param non-empty-string $cacheFile
+     */
     public function setCacheFile(string $cacheFile): ConfigInterface
     {
         $this->cacheFile = $cacheFile;
@@ -191,6 +206,9 @@ class Config implements ConfigInterface, ParallelAwareConfigInterface
         return $this;
     }
 
+    /**
+     * @param non-empty-string $indent
+     */
     public function setIndent(string $indent): ConfigInterface
     {
         $this->indent = $indent;
@@ -198,6 +216,9 @@ class Config implements ConfigInterface, ParallelAwareConfigInterface
         return $this;
     }
 
+    /**
+     * @param non-empty-string $lineEnding
+     */
     public function setLineEnding(string $lineEnding): ConfigInterface
     {
         $this->lineEnding = $lineEnding;

+ 15 - 1
src/ConfigInterface.php

@@ -25,7 +25,7 @@ interface ConfigInterface
     /**
      * Returns the path to the cache file.
      *
-     * @return null|string Returns null if not using cache
+     * @return null|non-empty-string Returns null if not using cache
      */
     public function getCacheFile(): ?string;
 
@@ -50,8 +50,14 @@ interface ConfigInterface
      */
     public function getHideProgress(): bool;
 
+    /**
+     * @return non-empty-string
+     */
     public function getIndent(): string;
 
+    /**
+     * @return non-empty-string
+     */
     public function getLineEnding(): string;
 
     /**
@@ -102,6 +108,8 @@ interface ConfigInterface
 
     /**
      * Sets the path to the cache file.
+     *
+     * @param non-empty-string $cacheFile
      */
     public function setCacheFile(string $cacheFile): self;
 
@@ -114,8 +122,14 @@ interface ConfigInterface
 
     public function setHideProgress(bool $hideProgress): self;
 
+    /**
+     * @param non-empty-string $indent
+     */
     public function setIndent(string $indent): self;
 
+    /**
+     * @param non-empty-string $lineEnding
+     */
     public function setLineEnding(string $lineEnding): self;
 
     /**

+ 12 - 0
src/WhitespacesFixerConfig.php

@@ -19,10 +19,16 @@ namespace PhpCsFixer;
  */
 final class WhitespacesFixerConfig
 {
+    /** @var non-empty-string */
     private string $indent;
 
+    /** @var non-empty-string */
     private string $lineEnding;
 
+    /**
+     * @param non-empty-string $indent
+     * @param non-empty-string $lineEnding
+     */
     public function __construct(string $indent = '    ', string $lineEnding = "\n")
     {
         if (!\in_array($indent, ['  ', '    ', "\t"], true)) {
@@ -37,11 +43,17 @@ final class WhitespacesFixerConfig
         $this->lineEnding = $lineEnding;
     }
 
+    /**
+     * @return non-empty-string
+     */
     public function getIndent(): string
     {
         return $this->indent;
     }
 
+    /**
+     * @return non-empty-string
+     */
     public function getLineEnding(): string
     {
         return $this->lineEnding;

+ 1 - 0
tests/AutoReview/ProjectCodeTest.php

@@ -475,6 +475,7 @@ final class ProjectCodeTest extends TestCase
      * @dataProvider provideDataProviderMethodCases
      *
      * @param class-string<TestCase> $testClassName
+     * @param non-empty-string       $dataProviderName
      */
     public function testDataProvidersAreNonPhpVersionConditional(string $testClassName, string $dataProviderName): void
     {

+ 9 - 0
tests/Console/ConfigurationResolverTest.php

@@ -1373,6 +1373,10 @@ For more info about updating see: https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/b
 
     /**
      * @dataProvider provideGetDirectoryCases
+     *
+     * @param ?non-empty-string $cacheFile
+     * @param non-empty-string  $file
+     * @param non-empty-string  $expectedPathRelativeToFile
      */
     public function testGetDirectory(?string $cacheFile, string $file, string $expectedPathRelativeToFile): void
     {
@@ -1397,6 +1401,11 @@ For more info about updating see: https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/b
         self::assertSame($expectedPathRelativeToFile, $directory->getRelativePathTo($file));
     }
 
+    /**
+     * @param non-empty-string $path
+     *
+     * @return non-empty-string
+     */
     private function normalizePath(string $path): string
     {
         return str_replace('/', \DIRECTORY_SEPARATOR, $path);

+ 3 - 3
tests/Test/AbstractIntegrationCaseFactory.php

@@ -76,7 +76,7 @@ abstract class AbstractIntegrationCaseFactory implements IntegrationCaseFactoryI
     /**
      * Parses the '--CONFIG--' block of a '.test' file.
      *
-     * @return array{indent: string, lineEnding: string}
+     * @return array{indent: non-empty-string, lineEnding: non-empty-string}
      */
     protected function determineConfig(SplFileInfo $file, ?string $config): array
     {
@@ -85,14 +85,14 @@ abstract class AbstractIntegrationCaseFactory implements IntegrationCaseFactoryI
             'lineEnding' => "\n",
         ]);
 
-        if (!\is_string($parsed['indent'])) {
+        if (!\is_string($parsed['indent']) || '' === $parsed['indent']) {
             throw new \InvalidArgumentException(\sprintf(
                 'Expected string value for "indent", got "%s".',
                 \is_object($parsed['indent']) ? \get_class($parsed['indent']) : \gettype($parsed['indent']).'#'.$parsed['indent']
             ));
         }
 
-        if (!\is_string($parsed['lineEnding'])) {
+        if (!\is_string($parsed['lineEnding']) || '' === $parsed['lineEnding']) {
             throw new \InvalidArgumentException(\sprintf(
                 'Expected string value for "lineEnding", got "%s".',
                 \is_object($parsed['lineEnding']) ? \get_class($parsed['lineEnding']) : \gettype($parsed['lineEnding']).'#'.$parsed['lineEnding']

+ 3 - 3
tests/Test/IntegrationCase.php

@@ -24,7 +24,7 @@ use PhpCsFixer\RuleSet\RuleSet;
 final class IntegrationCase
 {
     /**
-     * @var array{indent: string, lineEnding: string}
+     * @var array{indent: non-empty-string, lineEnding: non-empty-string}
      */
     private array $config;
 
@@ -53,7 +53,7 @@ final class IntegrationCase
     /**
      * @param array{checkPriority: bool, deprecations: list<string>, isExplicitPriorityCheck?: bool} $settings
      * @param array{php: int, "php<": int, os: list<string>}                                         $requirements
-     * @param array{indent: string, lineEnding: string}                                              $config
+     * @param array{indent: non-empty-string, lineEnding: non-empty-string}                          $config
      */
     public function __construct(
         string $fileName,
@@ -81,7 +81,7 @@ final class IntegrationCase
     }
 
     /**
-     * @return array{indent: string, lineEnding: string}
+     * @return array{indent: non-empty-string, lineEnding: non-empty-string}
      */
     public function getConfig(): array
     {

+ 5 - 1
tests/WhitespacesFixerConfigTest.php

@@ -26,6 +26,10 @@ use PhpCsFixer\WhitespacesFixerConfig;
 final class WhitespacesFixerConfigTest extends TestCase
 {
     /**
+     * @param non-empty-string  $indent
+     * @param non-empty-string  $lineEnding
+     * @param ?non-empty-string $exceptionRegExp
+     *
      * @dataProvider provideFixCases
      */
     public function testFix(string $indent, string $lineEnding, ?string $exceptionRegExp = null): void
@@ -42,7 +46,7 @@ final class WhitespacesFixerConfigTest extends TestCase
     }
 
     /**
-     * @return iterable<array{0: string, 1: string, 2?: string}>
+     * @return iterable<array{0: non-empty-string, 1: non-empty-string, 2?: non-empty-string}>
      */
     public static function provideFixCases(): iterable
     {