Skip to content

Commit

Permalink
Merge branch 'master' into heredoc-indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
SpacePossum committed Dec 27, 2018
2 parents 5e4e52c + 8e146bc commit 60f5c3a
Show file tree
Hide file tree
Showing 22 changed files with 469 additions and 76 deletions.
1 change: 1 addition & 0 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ install:
before_test:
- cd C:\projects\php-cs-fixer
- php C:\tools\composer.phar update --optimize-autoloader --no-interaction --no-progress --prefer-stable --no-ansi
- php C:\tools\composer.phar info -D | sort

test_script:
- cd C:\projects\php-cs-fixer
Expand Down
5 changes: 4 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ jobs:
- ~/Library/Caches/Homebrew

- run: brew update
- run: brew install php72
- run: brew install php@7.2
- run: brew link --force --overwrite php@7.2
- run: php --version
- run: echo "memory_limit = 512M" > $(brew --prefix)/etc/php/7.2/conf.d/memory.ini
- run: curl -sS https://getcomposer.org/installer | php
- run: php composer.phar global show hirak/prestissimo -q || php composer.phar global require --no-interaction --no-progress --optimize-autoloader hirak/prestissimo
- run: php composer.phar install --optimize-autoloader --no-interaction --no-progress --no-suggest
- run: php composer.phar info -D | sort
- run: vendor/bin/phpunit
- run: PHP_CS_FIXER_FUTURE_MODE=1 php php-cs-fixer --diff --dry-run -v fix
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ jobs:
php: 7.0
install:
# Composer: enforce given Symfony components version
- if [ "$SYMFONY_VERSION" != "" ]; then composer require --no-update symfony/force-lowest:$SYMFONY_VERSION; fi
- if [ "$SYMFONY_VERSION" != "" ]; then composer require --no-update symfony/lts:$(echo $SYMFONY_VERSION | grep -o 'v[0-9]\+') || true; fi
- if [ "$SYMFONY_VERSION" != "" ]; then composer global show symfony/flex -q || travis_retry composer global require $DEFAULT_COMPOSER_FLAGS symfony/flex; fi
- if [ "$SYMFONY_VERSION" != "" ]; then composer config extra.symfony.require $SYMFONY_VERSION || true; fi

- travis_retry composer update $DEFAULT_COMPOSER_FLAGS $COMPOSER_FLAGS
- composer info -D | sort
Expand All @@ -83,7 +83,7 @@ jobs:
<<: *STANDARD_TEST_JOB
stage: Test
php: 7.1
env: SYMFONY_DEPRECATIONS_HELPER=weak PHP_CS_FIXER_TEST_USE_LEGACY_TOKENIZER=1 SYMFONY_VERSION="v4.0"
env: SYMFONY_DEPRECATIONS_HELPER=weak PHP_CS_FIXER_TEST_USE_LEGACY_TOKENIZER=1 SYMFONY_VERSION="~4.1.0"

-
<<: *STANDARD_TEST_JOB
Expand Down
8 changes: 7 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -662,10 +662,16 @@ Choose from the list of available rules:

* **fopen_flags** [@Symfony:risky]

The flags in ``fopen`` calls must contain ``b`` and must omit ``t``.
The flags in ``fopen`` calls must omit ``t``, and ``b`` must be omitted or
included consistently.

*Risky rule: risky when the function ``fopen`` is overridden.*

Configuration options:

- ``b_mode`` (``bool``): the ``b`` flag must be used (``true``) or omitted (``false``);
defaults to ``true``

* **full_opening_tag** [@PSR1, @PSR2, @Symfony]

PHP code must use the long ``<?php`` tags or short-echo ``<?=`` tags and not
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"require-dev": {
"johnkary/phpunit-speedtrap": "^1.1 || ^2.0 || ^3.0",
"justinrainbow/json-schema": "^5.0",
"keradus/cli-executor": "^1.1",
"keradus/cli-executor": "^1.2",
"mikey179/vfsStream": "^1.6",
"php-coveralls/php-coveralls": "^2.1",
"php-cs-fixer/accessible-object": "^1.0",
Expand Down
52 changes: 52 additions & 0 deletions src/AbstractFopenFlagFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,56 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
}

abstract protected function fixFopenFlagToken(Tokens $tokens, $argumentStartIndex, $argumentEndIndex);

/**
* @param string $mode
*
* @return bool
*/
protected function isValidModeString($mode)
{
$modeLength = \strlen($mode);
if ($modeLength < 1 || $modeLength > 13) { // 13 === length 'r+w+a+x+c+etb'
return false;
}

$validFlags = [
'a' => true,
'b' => true,
'c' => true,
'e' => true,
'r' => true,
't' => true,
'w' => true,
'x' => true,
];

if (!isset($validFlags[$mode[0]])) {
return false;
}

unset($validFlags[$mode[0]]);

for ($i = 1; $i < $modeLength; ++$i) {
if (isset($validFlags[$mode[$i]])) {
unset($validFlags[$mode[$i]]);

continue;
}

if ('+' !== $mode[$i]
|| (
'a' !== $mode[$i - 1] // 'a+','c+','r+','w+','x+'
&& 'c' !== $mode[$i - 1]
&& 'r' !== $mode[$i - 1]
&& 'w' !== $mode[$i - 1]
&& 'x' !== $mode[$i - 1]
)
) {
return false;
}
}

return true;
}
}
1 change: 1 addition & 0 deletions src/Fixer/Alias/NoAliasFunctionsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ final class NoAliasFunctionsFixer extends AbstractFixer implements Configuration
'mbereg_search_setpos' => 'mb_ereg_search_setpos',
'mberegi' => 'mb_eregi',
'mberegi_replace' => 'mb_eregi_replace',
'mbregex_encoding' => 'mb_regex_encoding',
'mbsplit' => 'mb_split',
];

Expand Down
8 changes: 6 additions & 2 deletions src/Fixer/FunctionNotation/FopenFlagOrderFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ protected function fixFopenFlagToken(Tokens $tokens, $argumentStartIndex, $argum
}

$modeLength = \strlen($mode);
if ($modeLength < 2 || $modeLength > 13) { // 13 === length 'r+w+a+x+c+etb'
return; // sanity check to be less risky
if ($modeLength < 2) {
return; // nothing to sort
}

if (false === $this->isValidModeString($mode)) {
return;
}

$split = $this->sortFlags(Preg::split('#([^\+]\+?)#', $mode, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE));
Expand Down
42 changes: 34 additions & 8 deletions src/Fixer/FunctionNotation/FopenFlagsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
namespace PhpCsFixer\Fixer\FunctionNotation;

use PhpCsFixer\AbstractFopenFlagFixer;
use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\Tokenizer\Token;
Expand All @@ -21,21 +24,42 @@
/**
* @author SpacePossum
*/
final class FopenFlagsFixer extends AbstractFopenFlagFixer
final class FopenFlagsFixer extends AbstractFopenFlagFixer implements ConfigurationDefinitionFixerInterface
{
/**
* {@inheritdoc}
*/
public function configure(array $configuration = null)
{
parent::configure($configuration);
}

/**
* {@inheritdoc}
*/
public function getDefinition()
{
return new FixerDefinition(
'The flags in `fopen` calls must contain `b` and must omit `t`.',
'The flags in `fopen` calls must omit `t`, and `b` must be omitted or included consistently.',
[new CodeSample("<?php\n\$a = fopen(\$foo, 'rwt');\n")],
null,
'Risky when the function `fopen` is overridden.'
);
}

/**
* {@inheritdoc}
*/
protected function createConfigurationDefinition()
{
return new FixerConfigurationResolver([
(new FixerOptionBuilder('b_mode', 'The `b` flag must be used (`true`) or omitted (`false`).'))
->setAllowedTypes(['bool'])
->setDefault(true)
->getOption(),
]);
}

/**
* @param Tokens $tokens
* @param int $argumentStartIndex
Expand Down Expand Up @@ -74,15 +98,17 @@ protected function fixFopenFlagToken(Tokens $tokens, $argumentStartIndex, $argum
$mode = substr($content, 1, -1);
}

$modeLength = \strlen($mode);
if ($modeLength < 1 || $modeLength > 13) { // 13 === length 'r+w+a+x+c+etb'
return; // sanity check to be less risky
if (false === $this->isValidModeString($mode)) {
return;
}

$mode = str_replace('t', '', $mode);

if (false === strpos($mode, 'b')) {
$mode .= 'b';
if ($this->configuration['b_mode']) {
if (false === strpos($mode, 'b')) {
$mode .= 'b';
}
} else {
$mode = str_replace('b', '', $mode);
}

$newContent = $binPrefix.$contentQuote.$mode.$contentQuote;
Expand Down
8 changes: 4 additions & 4 deletions src/Linter/ProcessLinterProcessBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public function __construct($executable)
*/
public function build($path)
{
return new Process(sprintf(
'"%s" -l "%s"',
return new Process([
$this->executable,
$path
));
'-l',
$path,
]);
}
}
2 changes: 1 addition & 1 deletion src/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ final class RuleSet implements RuleSetInterface
'ereg_to_preg' => true,
'error_suppression' => true,
'fopen_flag_order' => true,
'fopen_flags' => true,
'fopen_flags' => ['b_mode' => false],
'function_to_constant' => true,
'implode_call' => true,
'is_null' => true,
Expand Down
25 changes: 18 additions & 7 deletions src/Tokenizer/Transformer/SquareBraceTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
* Performed transformations:
* - in `[1, 2, 3]` into CT::T_ARRAY_SQUARE_BRACE_OPEN and CT::T_ARRAY_SQUARE_BRACE_CLOSE,
* - in `[$a, $b, $c] = array(1, 2, 3)` into CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN and CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE.
* - in `[$a, &$b, [$c]] = array(1, 2, array(3))` into CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN and CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE.
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
* @author SpacePossum
Expand Down Expand Up @@ -93,6 +93,19 @@ private function transformIntoDestructuringSquareBrace(Tokens $tokens, $index)

$tokens[$index] = new Token([CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN, '[']);
$tokens[$endIndex] = new Token([CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE, ']']);

$previousMeaningfulIndex = $index;
$index = $tokens->getNextMeaningfulToken($index);

while ($index < $endIndex) {
if ($tokens[$index]->equals('[') && $tokens[$previousMeaningfulIndex]->equalsAny([[CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN], ','])) {
$tokens[$tokens->findBlockEnd(Tokens::BLOCK_TYPE_INDEX_SQUARE_BRACE, $index)] = new Token([CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE, ']']);
$tokens[$index] = new Token([CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN, '[']);
}

$previousMeaningfulIndex = $index;
$index = $tokens->getNextMeaningfulToken($index);
}
}

/**
Expand All @@ -105,6 +118,10 @@ private function transformIntoDestructuringSquareBrace(Tokens $tokens, $index)
*/
private function isShortArray(Tokens $tokens, $index)
{
if (!$tokens[$index]->equals('[')) {
return false;
}

static $disallowedPrevTokens = [
')',
']',
Expand All @@ -120,12 +137,6 @@ private function isShortArray(Tokens $tokens, $index)
[CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE],
];

$token = $tokens[$index];

if (!$token->equals('[')) {
return false;
}

$prevToken = $tokens[$tokens->getPrevMeaningfulToken($index)];
if ($prevToken->equalsAny($disallowedPrevTokens)) {
return false;
Expand Down
28 changes: 22 additions & 6 deletions src/Tokenizer/Transformer/TypeAlternationTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,29 @@ public function process(Tokens $tokens, Token $token, $index)
return;
}

$prevIndex = $tokens->getPrevMeaningfulToken($prevIndex);
$prevToken = $tokens[$prevIndex];
do {
$prevIndex = $tokens->getPrevMeaningfulToken($prevIndex);
if (null === $prevIndex) {
break;
}

if (!$prevToken->equalsAny(['(', [CT::T_TYPE_ALTERNATION]])) {
return;
}
$prevToken = $tokens[$prevIndex];

if ($prevToken->isGivenKind([T_NS_SEPARATOR, T_STRING])) {
continue;
}

if (
$prevToken->isGivenKind(CT::T_TYPE_ALTERNATION)
|| (
$prevToken->equals('(')
&& $tokens[$tokens->getPrevMeaningfulToken($prevIndex)]->isGivenKind(T_CATCH)
)
) {
$tokens[$index] = new Token([CT::T_TYPE_ALTERNATION, '|']);
}

$tokens[$index] = new Token([CT::T_TYPE_ALTERNATION, '|']);
break;
} while (true);
}
}
2 changes: 1 addition & 1 deletion tests/Console/Output/ErrorOutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function testLintingExceptionOutputsAppliedFixersAndDiff()
*/
private function createStreamOutput($verbosityLevel)
{
$output = new StreamOutput(fopen('php://memory', 'wb', false));
$output = new StreamOutput(fopen('php://memory', 'w', false));
$output->setDecorated(false);
$output->setVerbosity($verbosityLevel);

Expand Down

0 comments on commit 60f5c3a

Please sign in to comment.