AbstractFopenFlagFixer.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer;
  13. use PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer;
  14. use PhpCsFixer\Tokenizer\Tokens;
  15. /**
  16. * @internal
  17. */
  18. abstract class AbstractFopenFlagFixer extends AbstractFunctionReferenceFixer
  19. {
  20. public function isCandidate(Tokens $tokens): bool
  21. {
  22. return $tokens->isAllTokenKindsFound([T_STRING, T_CONSTANT_ENCAPSED_STRING]);
  23. }
  24. protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
  25. {
  26. $argumentsAnalyzer = new ArgumentsAnalyzer();
  27. $index = 0;
  28. $end = $tokens->count() - 1;
  29. while (true) {
  30. $candidate = $this->find('fopen', $tokens, $index, $end);
  31. if (null === $candidate) {
  32. break;
  33. }
  34. $index = $candidate[1]; // proceed to '(' of `fopen`
  35. // fetch arguments
  36. $arguments = $argumentsAnalyzer->getArguments(
  37. $tokens,
  38. $index,
  39. $candidate[2]
  40. );
  41. $argumentsCount = \count($arguments); // argument count sanity check
  42. if ($argumentsCount < 2 || $argumentsCount > 4) {
  43. continue;
  44. }
  45. $argumentStartIndex = array_keys($arguments)[1]; // get second argument index
  46. $this->fixFopenFlagToken(
  47. $tokens,
  48. $argumentStartIndex,
  49. $arguments[$argumentStartIndex]
  50. );
  51. }
  52. }
  53. abstract protected function fixFopenFlagToken(Tokens $tokens, int $argumentStartIndex, int $argumentEndIndex): void;
  54. protected function isValidModeString(string $mode): bool
  55. {
  56. $modeLength = \strlen($mode);
  57. if ($modeLength < 1 || $modeLength > 13) { // 13 === length 'r+w+a+x+c+etb'
  58. return false;
  59. }
  60. $validFlags = [
  61. 'a' => true,
  62. 'b' => true,
  63. 'c' => true,
  64. 'e' => true,
  65. 'r' => true,
  66. 't' => true,
  67. 'w' => true,
  68. 'x' => true,
  69. ];
  70. if (!isset($validFlags[$mode[0]])) {
  71. return false;
  72. }
  73. unset($validFlags[$mode[0]]);
  74. for ($i = 1; $i < $modeLength; ++$i) {
  75. if (isset($validFlags[$mode[$i]])) {
  76. unset($validFlags[$mode[$i]]);
  77. continue;
  78. }
  79. if ('+' !== $mode[$i]
  80. || (
  81. 'a' !== $mode[$i - 1] // 'a+','c+','r+','w+','x+'
  82. && 'c' !== $mode[$i - 1]
  83. && 'r' !== $mode[$i - 1]
  84. && 'w' !== $mode[$i - 1]
  85. && 'x' !== $mode[$i - 1]
  86. )
  87. ) {
  88. return false;
  89. }
  90. }
  91. return true;
  92. }
  93. }