Skip to content

Commit

Permalink
feat: add OrderBehatStepsFixer
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroTroller committed May 26, 2020
1 parent faa0db3 commit fed58b1
Show file tree
Hide file tree
Showing 5 changed files with 638 additions and 2 deletions.
185 changes: 185 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,191 @@ return $config;

# Fixers

## PedroTroller/order_behat_steps

Step definition methods in Behat contexts MUST BE ordered by annotation and method name.


### Available options

- `instanceof` (*optional*): Parent class or interface of your behat context classes.
- default: `Behat\Behat\Context\Context`

### Configuration examples

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

$config = PhpCsFixer\Config::create()
// ...
->setRules([
// ...
'PedroTroller/order_behat_steps' => 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/order_behat_steps')
->getRules()
])
// ...
->registerCustomFixers(new PedroTroller\CS\Fixer\Fixers())
;

return $config;
```

### Fixes

```diff
--- Original // 80 chars
+++ New //
@@ @@ //
} //
//
/** //
- * @Then the response should be received //
+ * @BeforeScenario //
*/ //
- public function theResponseShouldBeReceived() //
+ public function reset() //
{ //
// ... //
} //
//
/** //
- * @When a demo scenario sends a request to :path //
+ * @Given I am on the homepage //
*/ //
- public function aDemoScenarioSendsARequestTo($path) //
+ public function iAmOnTheHomepage() //
{ //
// ... //
} //
//
/** //
- * @Given I am on the homepage //
+ * @When a demo scenario sends a request to :path //
*/ //
- public function iAmOnTheHomepage() //
+ public function aDemoScenarioSendsARequestTo($path) //
{ //
// ... //
} //
//
/** //
- * @BeforeScenario //
+ * @Then the response should be received //
*/ //
- public function reset() //
+ public function theResponseShouldBeReceived() //
{ //
// ... //
} //
} //
//
```
### Configuration examples

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

$config = PhpCsFixer\Config::create()
// ...
->setRules([
// ...
'PedroTroller/order_behat_steps' => [ 'instanceof' => [ 'Behat\Behat\Context\Context' ] ],
// ...
])
// ...
->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/order_behat_steps', [ 'instanceof' => [ 'Behat\Behat\Context\Context' ] ])
->getRules()
])
// ...
->registerCustomFixers(new PedroTroller\CS\Fixer\Fixers())
;

return $config;
```

### Fixes

```diff
--- Original // 80 chars
+++ New //
@@ @@ //
} //
//
/** //
- * @Then the response should be received //
+ * @BeforeScenario //
*/ //
- public function theResponseShouldBeReceived() //
+ public function reset() //
{ //
// ... //
} //
//
/** //
- * @When a demo scenario sends a request to :path //
+ * @Given I am on the homepage //
*/ //
- public function aDemoScenarioSendsARequestTo($path) //
+ public function iAmOnTheHomepage() //
{ //
// ... //
} //
//
/** //
- * @Given I am on the homepage //
+ * @When a demo scenario sends a request to :path //
*/ //
- public function iAmOnTheHomepage() //
+ public function aDemoScenarioSendsARequestTo($path) //
{ //
// ... //
} //
//
/** //
- * @BeforeScenario //
+ * @Then the response should be received //
*/ //
- public function reset() //
+ public function theResponseShouldBeReceived() //
{ //
// ... //
} //
} //
//
```

## PedroTroller/ordered_with_getter_and_setter_first

Class/interface/trait methods MUST BE ordered (accessors at the beginning of the class, ordered following properties order).
Expand Down
6 changes: 4 additions & 2 deletions src/PedroTroller/CS/Fixer/AbstractFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
abstract class AbstractFixer extends PhpCsFixer
{
/**
* {@inheritdoc}
* @param Tokens<Token> $tokens
*
* @return bool
*/
public function isCandidate(Tokens $tokens)
{
Expand All @@ -29,7 +31,7 @@ public function getName()
}

/**
* @return array[]
* @return array<null|array>
*/
public function getSampleConfigurations()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ private function getElements(Tokens $tokens, $startIndex)
break;
}

$possibleCommentIndex = $startIndex + 1;

if (isset($tokens[$possibleCommentIndex]) && $tokens[$possibleCommentIndex]->isComment()) {
$element['comment'] = $tokens[$possibleCommentIndex]->getContent();
} else {
$element['comment'] = null;
}

$elements[] = $element;
$startIndex = $element['end'] + 1;
}
Expand Down
Loading

0 comments on commit fed58b1

Please sign in to comment.