Browse Source

chore: reduce amount of class mutable properties (#8281)

Dariusz Rumiński 3 months ago
parent
commit
236d546062

+ 18 - 17
src/FixerConfiguration/FixerConfigurationResolver.php

@@ -23,13 +23,10 @@ final class FixerConfigurationResolver implements FixerConfigurationResolverInte
 {
     /**
      * @var list<FixerOptionInterface>
+     *
+     * @readonly
      */
-    private array $options = [];
-
-    /**
-     * @var list<string>
-     */
-    private array $registeredNames = [];
+    private array $options;
 
     /**
      * @param iterable<FixerOptionInterface> $options
@@ -37,12 +34,11 @@ final class FixerConfigurationResolver implements FixerConfigurationResolverInte
     public function __construct(iterable $options)
     {
         $fixerOptionSorter = new FixerOptionSorter();
+        $this->validateOptions($options);
 
-        foreach ($fixerOptionSorter->sort($options) as $option) {
-            $this->addOption($option);
-        }
+        $this->options = $fixerOptionSorter->sort($options);
 
-        if (0 === \count($this->registeredNames)) {
+        if (0 === \count($this->options)) {
             throw new \LogicException('Options cannot be empty.');
         }
     }
@@ -131,17 +127,22 @@ final class FixerConfigurationResolver implements FixerConfigurationResolverInte
     }
 
     /**
+     * @param iterable<FixerOptionInterface> $options
+     *
      * @throws \LogicException when the option is already defined
      */
-    private function addOption(FixerOptionInterface $option): void
+    private function validateOptions(iterable $options): void
     {
-        $name = $option->getName();
+        $names = [];
 
-        if (\in_array($name, $this->registeredNames, true)) {
-            throw new \LogicException(\sprintf('The "%s" option is defined multiple times.', $name));
-        }
+        foreach ($options as $option) {
+            $name = $option->getName();
 
-        $this->options[] = $option;
-        $this->registeredNames[] = $name;
+            if (\in_array($name, $names, true)) {
+                throw new \LogicException(\sprintf('The "%s" option is defined multiple times.', $name));
+            }
+
+            $names[] = $name;
+        }
     }
 }

+ 7 - 3
src/RuleSet/RuleSet.php

@@ -22,6 +22,8 @@ use PhpCsFixer\Utils;
  *
  * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  *
+ * @readonly
+ *
  * @internal
  */
 final class RuleSet implements RuleSetInterface
@@ -58,7 +60,7 @@ final class RuleSet implements RuleSetInterface
             }
         }
 
-        $this->resolveSet($set);
+        $this->rules = $this->resolveSet($set);
     }
 
     public function hasRule(string $rule): bool
@@ -88,8 +90,10 @@ final class RuleSet implements RuleSetInterface
      * Resolve input set into group of rules.
      *
      * @param array<string, array<string, mixed>|bool> $rules
+     *
+     * @return array<string, array<string, mixed>|true>
      */
-    private function resolveSet(array $rules): void
+    private function resolveSet(array $rules): array
     {
         $resolvedRules = [];
 
@@ -113,7 +117,7 @@ final class RuleSet implements RuleSetInterface
             static fn ($value): bool => false !== $value
         );
 
-        $this->rules = $resolvedRules;
+        return $resolvedRules;
     }
 
     /**

+ 11 - 2
src/Runner/Parallel/ProcessPool.php

@@ -26,12 +26,21 @@ use React\Socket\ServerInterface;
  */
 final class ProcessPool
 {
+    /**
+     * @readonly
+     */
     private ServerInterface $server;
 
-    /** @var null|(callable(): void) */
+    /**
+     * @var null|(callable(): void)
+     *
+     * @readonly
+     */
     private $onServerClose;
 
-    /** @var array<string, Process> */
+    /**
+     * @var array<string, Process>
+     */
     private array $processes = [];
 
     /**

+ 5 - 5
src/Tokenizer/TokensAnalyzer.php

@@ -36,11 +36,15 @@ final class TokensAnalyzer
      */
     private Tokens $tokens;
 
-    private ?GotoLabelAnalyzer $gotoLabelAnalyzer = null;
+    /**
+     * @readonly
+     */
+    private GotoLabelAnalyzer $gotoLabelAnalyzer;
 
     public function __construct(Tokens $tokens)
     {
         $this->tokens = $tokens;
+        $this->gotoLabelAnalyzer = new GotoLabelAnalyzer();
     }
 
     /**
@@ -452,10 +456,6 @@ final class TokensAnalyzer
 
         // check for goto label
         if ($this->tokens[$nextIndex]->equals(':')) {
-            if (null === $this->gotoLabelAnalyzer) {
-                $this->gotoLabelAnalyzer = new GotoLabelAnalyzer();
-            }
-
             if ($this->gotoLabelAnalyzer->belongsToGoToLabel($this->tokens, $nextIndex)) {
                 return false;
             }

+ 1 - 1
tests/Tokenizer/TokensTest.php

@@ -1753,7 +1753,7 @@ $bar;',
             $sets[$j] = $set;
         }
 
-        yield 'overlapping inserts of bunch of comments ' => [
+        yield 'overlapping inserts of bunch of comments' => [
             Tokens::fromCode(\sprintf("<?php\n%s/* line #1 */\n%s/* line #2 */\n%s/* line #3 */%s", $sets[0]['content'], $sets[1]['content'], $sets[2]['content'], $sets[3]['content'])),
             Tokens::fromCode("<?php\n/* line #1 */\n/* line #2 */\n/* line #3 */"),
             [1 => $sets[0]['tokens'], 3 => $sets[1]['tokens'], 5 => $sets[2]['tokens'], 6 => $sets[3]['tokens']],