Skip to content

Commit

Permalink
Setup php-cs-fixer (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored Dec 19, 2023
1 parent c648493 commit 4a85e60
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.github export-ignore
/tests export-ignore
.editorconfig export-ignore
.php-cs-fixer.php export-ignore
phpstan.neon export-ignore
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor/
/composer.lock
/test.php
.php-cs-fixer.cache
20 changes: 20 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

$finder = PhpCsFixer\Finder::create()
->files()
->in([
'src',
'tests'
])
->exclude([
'data'
]);

return (new PhpCsFixer\Config())
->setFinder($finder)
->setRules([
'@PSR2' => true,
]
);
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},

"require-dev": {
"friendsofphp/php-cs-fixer": "^3.41",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9 || ^10.5"
},
Expand Down
3 changes: 2 additions & 1 deletion src/GitTagFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace staabm\PHPStanTodoBy;

final class GitTagFetcher implements TagFetcher {
final class GitTagFetcher implements TagFetcher
{
// fetch version of the latest created git tag
public function fetchLatestTagVersion(): string
{
Expand Down
9 changes: 6 additions & 3 deletions src/ReferenceVersionFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

use Version\Version;

final class ReferenceVersionFinder {
final class ReferenceVersionFinder
{
private TagFetcher $fetcher;
private string $referenceVersion;

public function __construct(string $referenceVersion, TagFetcher $fetcher) {
public function __construct(string $referenceVersion, TagFetcher $fetcher)
{
$this->referenceVersion = $referenceVersion;
$this->fetcher = $fetcher;
}
public function find(): string {
public function find(): string
{
if (in_array($this->referenceVersion, ['nextMajor', 'nextMinor', 'nextPatch'], true)) {
$latestTagVersion = $this->fetcher->fetchLatestTagVersion();

Expand Down
2 changes: 1 addition & 1 deletion src/TodoByDateRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function processNode(Node $node, Scope $scope): array
* PREG_SET_ORDER: Make each value of $matches be structured the same as if from preg_match().
*/
if (
preg_match_all(self::PATTERN, $text, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER) === FALSE
preg_match_all(self::PATTERN, $text, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER) === false
|| count($matches) === 0
) {
continue;
Expand Down
8 changes: 5 additions & 3 deletions src/TodoByVersionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function processNode(Node $node, Scope $scope): array
* PREG_SET_ORDER: Make each value of $matches be structured the same as if from preg_match().
*/
if (
preg_match_all(self::PATTERN, $text, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER) === FALSE
preg_match_all(self::PATTERN, $text, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER) === false
|| count($matches) === 0
) {
continue;
Expand Down Expand Up @@ -136,15 +136,17 @@ public function processNode(Node $node, Scope $scope): array
return $errors;
}

private function getReferenceVersion(): string {
private function getReferenceVersion(): string
{
if ($this->referenceVersion === null) {
// lazy get the version, as it might incur subprocess creation
$this->referenceVersion = $this->versionParser->normalize($this->referenceVersionFinder->find());
}
return $this->referenceVersion;
}

private function getVersionComparator(string $version): ?string {
private function getVersionComparator(string $version): ?string
{
$comparator = null;
for($i = 0; $i < strlen($version); $i++) {
if (!in_array($version[$i], self::COMPARATORS)) {
Expand Down
3 changes: 2 additions & 1 deletion tests/AlwaysThrowingTagFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use staabm\PHPStanTodoBy\TagFetcher;

final class AlwaysThrowingTagFetcher implements TagFetcher {
final class AlwaysThrowingTagFetcher implements TagFetcher
{
public function fetchLatestTagVersion(): string
{
throw new \RuntimeException('Could not determine latest git tag');
Expand Down
5 changes: 3 additions & 2 deletions tests/TodoByVersionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function testRule(string $referenceVersion, array $errors): void
/**
* @return iterable<array{string, list<array{0: string, 1: int, 2?: string|null}>}>
*/
static public function provideErrors(): iterable
public static function provideErrors(): iterable
{
yield [
"0.1",
Expand Down Expand Up @@ -103,7 +103,8 @@ public function testSemanticVersions(string $referenceVersion, array $errors): v
/**
* @return iterable<array{string, list<array{0: string, 1: int, 2?: string|null}>}>
*/
static public function provideSemanticVersions(): iterable {
public static function provideSemanticVersions(): iterable
{
yield [
'nextMajor', // we assume this resolves to 1.0
[
Expand Down

0 comments on commit 4a85e60

Please sign in to comment.