Skip to content

Commit

Permalink
feat: allow to disable arguments split based on number of arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroTroller committed Oct 23, 2019
1 parent 6104e84 commit 7751926
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 1 deletion.
71 changes: 71 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,77 @@ return $config;

### Fixes

```diff
--- Original // 80 chars
+++ New //
@@ @@ //
return; //
} //
//
- public function fun2($arg1, array $arg2 = [], \ArrayAccess $arg3 = null, bool $bool = true, \Iterator $thisLastArgument = null)
- { //
+ public function fun2( //
+ $arg1, //
+ array $arg2 = [], //
+ \ArrayAccess $arg3 = null, //
+ bool $bool = true, //
+ \Iterator $thisLastArgument = null //
+ ) { //
return; //
} //
//
- public function fun3( //
- $arg1, //
- array $arg2 = [] //
- ) { //
+ public function fun3($arg1, array $arg2 = []) //
+ { //
return; //
} //
} //
//
```
### Configuration

```php
// .php_cs.dist
<?php

$config = PhpCsFixer\Config::create()
// ...
->setRules([
// ...
'PedroTroller/line_break_between_method_arguments' => [ 'max-args' => false, 'max-length' => 120, 'automatic-argument-merge' => true ],
// ...
])
// ...
->registerCustomFixers(new PedroTroller\CS\Fixer\Fixers())
;

return $config;
```

**OR** using my [rule list builder](doc/rule-set-factory.md).

```php
// .php_cs.dist
<?php

$config = PhpCsFixer\Config::create()
// ...
->setRules(PedroTroller\CS\Fixer\RuleSetFactory::create()
->enable('PedroTroller/line_break_between_method_arguments', [ 'max-args' => false, 'max-length' => 120, 'automatic-argument-merge' => true ])
->getRules()
])
// ...
->registerCustomFixers(new PedroTroller\CS\Fixer\Fixers())
;

return $config;
```

### Fixes

```diff
--- Original // 80 chars
+++ New //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function getSampleConfigurations()
'max-length' => 120,
'automatic-argument-merge' => true,
],
[
'max-args' => false,
'max-length' => 120,
'automatic-argument-merge' => true,
],
];
}

Expand Down Expand Up @@ -125,7 +130,7 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
continue;
}

if ($this->analyze($tokens)->getNumberOfArguments($index) > $this->configuration['max-args']) {
if (false !== $this->configuration['max-args'] && $this->analyze($tokens)->getNumberOfArguments($index) > $this->configuration['max-args']) {
$this->splitArgs($tokens, $index);

continue;
Expand Down
100 changes: 100 additions & 0 deletions tests/UseCase/LineBreakBetweenMethodsWithNoSplitOnNumberOfArgs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

namespace tests\UseCase;

use PedroTroller\CS\Fixer\CodingStyle\LineBreakBetweenMethodArgumentsFixer;
use tests\UseCase;

class LineBreakBetweenMethodsWithNoSplitOnNumberOfArgs implements UseCase
{
/**
* {@inheritdoc}
*/
public function getFixer()
{
$fixer = new LineBreakBetweenMethodArgumentsFixer();

$fixer->configure([
'max-args' => false,
'max-length' => 90,
]);

return $fixer;
}

/**
* {@inheritdoc}
*/
public function getRawScript()
{
return <<<'PHP'
<?php
namespace Project\TheNamespace;
class TheClass
{
public function fun1($arg1, array $arg2 = [], \ArrayAccess $arg3 = null, $foo = 'bar')
{
return;
}
public function fun2(
$arg1,
array $arg2 = []
) {
return;
}
public function fun4($foo, $bar, $bar, $boolean = true, $integer = 1, $string = 'string')
{
}
}
PHP;
}

/**
* {@inheritdoc}
*/
public function getExpectation()
{
return <<<'PHP'
<?php
namespace Project\TheNamespace;
class TheClass
{
public function fun1($arg1, array $arg2 = [], \ArrayAccess $arg3 = null, $foo = 'bar')
{
return;
}
public function fun2($arg1, array $arg2 = [])
{
return;
}
public function fun4(
$foo,
$bar,
$bar,
$boolean = true,
$integer = 1,
$string = 'string'
) {
}
}
PHP;
}

/**
* {@inheritdoc}
*/
public function getMinSupportedPhpVersion()
{
return 70100;
}
}

0 comments on commit 7751926

Please sign in to comment.