AbstractFopenFlagFixer.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. * @author SpacePossum
  19. */
  20. abstract class AbstractFopenFlagFixer extends AbstractFunctionReferenceFixer
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function isCandidate(Tokens $tokens): bool
  26. {
  27. return $tokens->isAllTokenKindsFound([T_STRING, T_CONSTANT_ENCAPSED_STRING]);
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
  33. {
  34. $argumentsAnalyzer = new ArgumentsAnalyzer();
  35. $index = 0;
  36. $end = $tokens->count() - 1;
  37. while (true) {
  38. $candidate = $this->find('fopen', $tokens, $index, $end);
  39. if (null === $candidate) {
  40. break;
  41. }
  42. $index = $candidate[1]; // proceed to '(' of `fopen`
  43. // fetch arguments
  44. $arguments = $argumentsAnalyzer->getArguments(
  45. $tokens,
  46. $index,
  47. $candidate[2]
  48. );
  49. $argumentsCount = \count($arguments); // argument count sanity check
  50. if ($argumentsCount < 2 || $argumentsCount > 4) {
  51. continue;
  52. }
  53. $argumentStartIndex = array_keys($arguments)[1]; // get second argument index
  54. $this->fixFopenFlagToken(
  55. $tokens,
  56. $argumentStartIndex,
  57. $arguments[$argumentStartIndex]
  58. );
  59. }
  60. }
  61. abstract protected function fixFopenFlagToken(Tokens $tokens, int $argumentStartIndex, int $argumentEndIndex): void;
  62. protected function isValidModeString(string $mode): bool
  63. {
  64. $modeLength = \strlen($mode);
  65. if ($modeLength < 1 || $modeLength > 13) { // 13 === length 'r+w+a+x+c+etb'
  66. return false;
  67. }
  68. $validFlags = [
  69. 'a' => true,
  70. 'b' => true,
  71. 'c' => true,
  72. 'e' => true,
  73. 'r' => true,
  74. 't' => true,
  75. 'w' => true,
  76. 'x' => true,
  77. ];
  78. if (!isset($validFlags[$mode[0]])) {
  79. return false;
  80. }
  81. unset($validFlags[$mode[0]]);
  82. for ($i = 1; $i < $modeLength; ++$i) {
  83. if (isset($validFlags[$mode[$i]])) {
  84. unset($validFlags[$mode[$i]]);
  85. continue;
  86. }
  87. if ('+' !== $mode[$i]
  88. || (
  89. 'a' !== $mode[$i - 1] // 'a+','c+','r+','w+','x+'
  90. && 'c' !== $mode[$i - 1]
  91. && 'r' !== $mode[$i - 1]
  92. && 'w' !== $mode[$i - 1]
  93. && 'x' !== $mode[$i - 1]
  94. )
  95. ) {
  96. return false;
  97. }
  98. }
  99. return true;
  100. }
  101. }