Skip to content
This repository has been archived by the owner on Nov 21, 2019. It is now read-only.

Start using PSR-2 #2

Merged
merged 22 commits into from
Sep 12, 2018
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Libero PHP coding standard

[![Build Status](https://travis-ci.com/libero/php-coding-standard.svg?branch=master)](https://travis-ci.com/libero/php-coding-standard)

The Libero PHP coding standard is a set of [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) rules applied to all Libero PHP projects. It is based on [PSR-1](https://www.php-fig.org/psr/psr-1/).
The Libero PHP coding standard is a set of [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) rules applied to all Libero PHP projects. It is based on [PSR-2](https://www.php-fig.org/psr/psr-2/).

Getting started
---------------
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"require": {
"php": "^7.2",
"dealerdirect/phpcodesniffer-composer-installer": "^0.4",
"squizlabs/php_codesniffer": "^3.3"
"slevomat/coding-standard": "^4.6.1",
"squizlabs/php_codesniffer": "^3.3.1"
},
"require-dev": {
"ext-mbstring": "*",
Expand Down
24 changes: 23 additions & 1 deletion src/Libero/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,30 @@

<description>The Libero coding standard.</description>

<rule ref="PSR1"/>
<rule ref="PSR2">
<!-- Prefer SlevomatCodingStandard.Namespaces.UseSpacing -->
<exclude name="PSR2.Namespaces.UseDeclaration.SpaceAfterLastUse"/>
</rule>

<rule ref="SlevomatCodingStandard.Namespaces.UseDoesNotStartWithBackslash"/>
<rule ref="SlevomatCodingStandard.Namespaces.UseSpacing"/>
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHintSpacing"/>
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing">
<properties>
<property name="spacesCountBeforeColon" value="1"/>
</properties>
</rule>
<rule ref="Squiz.Classes.ClassFileName"/>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No example for traits yet, waiting for squizlabs/PHP_CodeSniffer#2140 to the merged/released.

<rule ref="Squiz.WhiteSpace.ObjectOperatorSpacing">
<properties>
<property name="ignoreNewlines" value="false"/>
</properties>
</rule>
<rule ref="Squiz.WhiteSpace.OperatorSpacing"/>
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
<properties>
<property name="ignoreBlankLines" value="false"/>
</properties>
</rule>

</ruleset>
27 changes: 22 additions & 5 deletions tests/RulesetTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
use function ini_get;
use function mb_convert_encoding;
use function preg_match_all;
use function preg_replace;
use function sort;
use function str_replace;
use function strpos;
use function token_get_all;
use const TOKEN_PARSE;
Expand Down Expand Up @@ -73,13 +75,16 @@ public function cases() : iterable

$parts = array_combine(array_map('strtolower', $matches[1]), $matches[2]);

$parts['filename'] = $parts['filename'] ?? 'test.php';

if (isset($parts['messages'])) {
$parts['messages'] = array_filter(explode("\n", $parts['messages']));
}

$keys = ['fixed', 'fixed-encoding', 'fixed-line-endings', 'messages'];
if (empty($parts['contents'])) {
throw new LogicException("Couldn't find contents in {$file->getRelativePathname()}");
} elseif (empty(select_keys($parts, $keys = ['fixed', 'fixed-encoding', 'messages']))) {
} elseif (empty(select_keys($parts, $keys))) {
throw new LogicException("Expected one of ".implode(', ', $keys)." in {$file->getRelativePathname()}");
}

Expand All @@ -90,6 +95,11 @@ public function cases() : iterable
throw new LogicException($message, 0, $exception);
}

if (!empty($parts['line-endings'])) {
$parts['line-endings'] = str_replace(['\n', '\r'], ["\n", "\r"], $parts['line-endings']);
$parts['contents'] = preg_replace('~\R~', $parts['line-endings'], $parts['contents']);
}

if (!empty($parts['fixed'])) {
try {
token_get_all($parts['fixed'], TOKEN_PARSE);
Expand All @@ -109,10 +119,17 @@ public function cases() : iterable
$parts['contents'] = mb_convert_encoding($parts['contents'], $parts['encoding']);
}

$parts['fixed'] = $parts['fixed'] ?? $parts['contents'];

if (!empty($parts['fixed-line-endings'])) {
$parts['fixed-line-endings'] = str_replace(['\n', '\r'], ["\n", "\r"], $parts['fixed-line-endings']);
$parts['fixed'] = preg_replace('~\R~', $parts['fixed-line-endings'], $parts['fixed']);
}

yield $file->getRelativePathname() => [
$parts['filename'] ?? 'test.php',
"{$file->getRelativePathname()}/{$parts['filename']}",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some caching inside Slevomat Coding Standard so we need to make sure these are all considered unique.

$parts['contents'],
$parts['fixed'] ?? $parts['contents'],
$parts['fixed'],
$parts['messages'] ?? [],
$parts['description'] ?? null,
$parts['fixed-encoding'] ?? null,
Expand All @@ -127,7 +144,7 @@ private function createFile(string $filename, string $content) : File
}

$file = new DummyFile(
"phpcs_input_file:${filename}\n{$content}",
"phpcs_input_file:before/${filename}\n{$content}",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self::$codeSniffer->ruleset,
self::$codeSniffer->config
);
Expand All @@ -137,7 +154,7 @@ private function createFile(string $filename, string $content) : File
$file->fixer->fixFile();

$file = new DummyFile(
"phpcs_input_file:${filename}\n{$file->fixer->getContents()}",
"phpcs_input_file:after/${filename}\n{$file->fixer->getContents()}",
self::$codeSniffer->ruleset,
self::$codeSniffer->config
);
Expand Down
4 changes: 2 additions & 2 deletions tests/cases/classes/constant-name
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace Vendor;

class Foo
{
const foo = 'bar';
public const bar = 'baz';
}

---MESSAGES---
7:11 Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase
7:18 Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase
---
24 changes: 24 additions & 0 deletions tests/cases/classes/extends
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---DESCRIPTION---
Extends keyword must be on the same line as the class name
---FILENAME---
Foo.php
---CONTENTS---
<?php

namespace Vendor;

class Foo
extends Bar
{
}

---FIXED---
<?php

namespace Vendor;

class Foo extends Bar
{
}

---
24 changes: 24 additions & 0 deletions tests/cases/classes/implements
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---DESCRIPTION---
Implements keyword must be on the same line as the class name
---FILENAME---
Foo.php
---CONTENTS---
<?php

namespace Vendor;

class Foo
implements Bar
{
}

---FIXED---
<?php

namespace Vendor;

class Foo implements Bar
{
}

---
28 changes: 28 additions & 0 deletions tests/cases/classes/method-braces
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---DESCRIPTION---
Method braces must be on separate lines.
---FILENAME---
Foo.php
---CONTENTS---
<?php

namespace Vendor;

class Foo
{
public function bar() : void { $baz = 'qux'; }
}

---FIXED---
<?php

namespace Vendor;

class Foo
{
public function bar() : void
{
$baz = 'qux';
}
}

---
30 changes: 30 additions & 0 deletions tests/cases/classes/method-declaration
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---DESCRIPTION---
Methods must not have unnecessary whitespace
---FILENAME---
Foo.php
---CONTENTS---
<?php

namespace Vendor;

class Foo
{
public function bar ( $baz ,&$qux, bool$quux, ? string $quuz =null , string...$corge ):
void
{
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't include an abstract example yet, waiting for squizlabs/PHP_CodeSniffer#2148 to be merged/released.

}

---FIXED---
<?php

namespace Vendor;

class Foo
{
public function bar($baz, &$qux, bool $quux, ?string $quuz = null, string ...$corge) : void
{
}
}

---
19 changes: 19 additions & 0 deletions tests/cases/classes/method-defaults
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---DESCRIPTION---
Default values must go at the end
---FILENAME---
Foo.php
---CONTENTS---
<?php

namespace Vendor;

class Foo
{
public function bar($baz = [], $qux) : void
{
}
}

---MESSAGES---
7:36 PEAR.Functions.ValidDefaultValue.NotAtEnd
---
33 changes: 33 additions & 0 deletions tests/cases/classes/method-modifiers
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---DESCRIPTION---
Method modifiers must come in the order: final/abstract, visibility, static.
---FILENAME---
Foo.php
---CONTENTS---
<?php

namespace Vendor;

abstract class Foo
{
protected abstract function bar() : void;

static public final function baz() : void
{
}
}

---FIXED---
<?php

namespace Vendor;

abstract class Foo
{
abstract protected function bar() : void;

final public static function baz() : void
{
}
}

---
19 changes: 19 additions & 0 deletions tests/cases/classes/method-underscore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---DESCRIPTION---
Method names must not begin with an underscore to indicate visibility
---FILENAME---
Foo.php
---CONTENTS---
<?php

namespace Vendor;

class Foo
{
private function _bar() : void
{
}
}

---MESSAGES---
7:13 PSR2.Methods.MethodDeclaration.Underscore
---
19 changes: 19 additions & 0 deletions tests/cases/classes/method-visibility
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---DESCRIPTION---
Methods must have visibility declared
---FILENAME---
Foo.php
---CONTENTS---
<?php

namespace Vendor;

class Foo
{
function bar() : void
{
}
}

---MESSAGES---
7:5 Squiz.Scope.MethodScope.Missing
---
17 changes: 17 additions & 0 deletions tests/cases/classes/property-single
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---DESCRIPTION---
One property per declaration
---FILENAME---
Foo.php
---CONTENTS---
<?php

namespace Vendor;

class Foo
{
public $bar, $baz;
}

---MESSAGES---
7:12 PSR2.Classes.PropertyDeclaration.Multiple
---
17 changes: 17 additions & 0 deletions tests/cases/classes/property-underscore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---DESCRIPTION---
Property names must not begin with an underscore to indicate visibility
---FILENAME---
Foo.php
---CONTENTS---
<?php

namespace Vendor;

class Foo
{
private $_bar;
}

---MESSAGES---
7:13 PSR2.Classes.PropertyDeclaration.Underscore
---
18 changes: 18 additions & 0 deletions tests/cases/classes/property-visibility
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---DESCRIPTION---
Class properties must have visibility declared
---FILENAME---
Foo.php
---CONTENTS---
<?php

namespace Vendor;

class Foo
{
var $bar;
}

---MESSAGES---
7:9 PSR2.Classes.PropertyDeclaration.ScopeMissing
7:9 PSR2.Classes.PropertyDeclaration.VarUsed
---
Loading