Skip to content

Commit

Permalink
Put all rules into PHPCS config
Browse files Browse the repository at this point in the history
- Add XML namespace for completeness
- Remove non-existent `type="int"`
- Add directory rules for improved test coverage and required
  exceptions
- Fix the new upcoming violations
  • Loading branch information
mk-mxp committed Dec 6, 2023
1 parent 44ea217 commit cc97047
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 25 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"squizlabs/php_codesniffer": "^3.6"
},
"scripts": {
"lint:check": "./vendor/bin/phpcs --ignore=*/hello-world/*,*/concept/* --standard=phpcs-php.xml ./exercises",
"lint:fix": "./vendor/bin/phpcbf --standard=phpcs-php.xml ./exercises"
"lint:check": "phpcs --standard=phpcs-php.xml",
"lint:fix": "phpcbf --standard=phpcs-php.xml"
}
}
1 change: 0 additions & 1 deletion exercises/concept/annalyns-infiltration/.meta/exemplar.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,3 @@ public function canLiberate(
);
}
}

8 changes: 4 additions & 4 deletions exercises/concept/city-office/.meta/exemplar/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

class Form
{
function blanks(int $length): string
public function blanks(int $length): string
{
return str_repeat(" ", $length);
}

function letters(string $word): array
public function letters(string $word): array
{
return mb_str_split($word);
}

function checkLength(string $word, int $max_length): bool
public function checkLength(string $word, int $max_length): bool
{
$difference = mb_strlen($word) - $max_length;
return $difference <= 0;
}

function formatAddress(Address $address): string
public function formatAddress(Address $address): string
{
$formatted_street = mb_strtoupper($address->street);
$formatted_postal_code = mb_strtoupper($address->postal_code);
Expand Down
8 changes: 4 additions & 4 deletions exercises/concept/city-office/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

class Form
{
function blanks($length)
public function blanks($length)
{
return str_repeat(" ", $length);
}

function letters($word)
public function letters($word)
{
return mb_str_split($word);
}

function checkLength($word, $max_length)
public function checkLength($word, $max_length)
{
$difference = mb_strlen($word) - $max_length;
return $difference <= 0;
}

function formatAddress($address)
public function formatAddress($address)
{
$formatted_street = mb_strtoupper($address->street);
$formatted_postal_code = mb_strtoupper($address->postal_code);
Expand Down
3 changes: 2 additions & 1 deletion exercises/concept/city-office/ReflectionAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ private function assertMethodParameter(
if (is_null($parameter)) {
$this->fail(
"Method '$parameter_name' missing parameter $parameter_index"
. " named '$parameter_name'");
. " named '$parameter_name'"
);
}

if ($parameter->getName() !== $parameter_name) {
Expand Down
22 changes: 22 additions & 0 deletions exercises/concept/lasagna/Lasagna.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
<?php

/*
* By adding type hints and enabling strict type checking, code can become
* easier to read, self-documenting and reduce the number of potential bugs.
* By default, type declarations are non-strict, which means they will attempt
* to change the original type to match the type specified by the
* type-declaration.
*
* In other words, if you pass a string to a function requiring a float,
* it will attempt to convert the string value to a float.
*
* To enable strict mode, a single declare directive must be placed at the top
* of the file.
* This means that the strictness of typing is configured on a per-file basis.
* This directive not only affects the type declarations of parameters, but also
* a function's return type.
*
* For more info review the Concept on strict type checking in the PHP track
* <link>.
*
* To disable strict typing, comment out the directive below.
*/

declare(strict_types=1);

class Lasagna
Expand Down
6 changes: 4 additions & 2 deletions exercises/concept/lucky-numbers/.meta/exemplar.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ public function isPalindrome(int $number): bool

public function validate(string $input): string
{
if ($input === '')
if ($input === '') {
return 'Required field';
}

if ((int) $input <= 0)
if ((int) $input <= 0) {
return 'Must be a whole number larger than 0';
}

return '';
}
Expand Down
1 change: 0 additions & 1 deletion exercises/concept/sweethearts/.meta/exemplar.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,3 @@ public function pair(
HEART;
}
}

10 changes: 5 additions & 5 deletions exercises/concept/windowing-system/.meta/exemplar.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ class ProgramWindow
public $height;
public $width;

function __construct()
public function __construct()
{
$this->y = 0;
$this->x = 0;
$this->height = 600;
$this->width = 800;
}

function move(Position $position)
public function move(Position $position)
{
$this->y = $position->y;
$this->x = $position->x;
}

function resize(Size $size)
public function resize(Size $size)
{
$this->height = $size->height;
$this->width = $size->width;
Expand All @@ -33,7 +33,7 @@ class Position
public $y;
public $x;

function __construct($y, $x)
public function __construct($y, $x)
{
$this->y = $y;
$this->x = $x;
Expand All @@ -45,7 +45,7 @@ class Size
public $height;
public $width;

function __construct($height, $width)
public function __construct($height, $width)
{
$this->height = $height;
$this->width = $width;
Expand Down
24 changes: 19 additions & 5 deletions phpcs-php.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<?xml version="1.0"?>
<ruleset name="xPHP">
<description>Coding standard for xPHP</description>
<ruleset
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd"
name="Exercism PHP Coding Standard"
>
<description>Coding standard for Exercism PHP exercises</description>

<file>exercises</file>
<file>src</file>

<!-- Include all sniffs in the PSR12 standard except... -->
<rule ref="PSR12">
Expand All @@ -9,10 +16,17 @@
<exclude name="PSR1.Files.SideEffects.FoundWithSymbols" />
</rule>
<rule ref="vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/TypeHints/DeclareStrictTypesSniff.php">
<exclude-pattern>*/exemplar\.php</exclude-pattern>
<exclude-pattern>*/concept/*</exclude-pattern>
<exclude-pattern>*/hello-world/*</exclude-pattern>
<properties>
<property name="newlinesCountBetweenOpenTagAndDeclare" type="int" value="1" />
<property name="spacesCountAroundEqualsSign" type="int" value="0" />
<property name="newlinesCountBetweenOpenTagAndDeclare" value="1" />
<property name="spacesCountAroundEqualsSign" value="0" />
</properties>
</rule>
<rule ref="src/Sniffs/ExplainStrictTypesSniff.php"/>
<rule ref="src/Sniffs/ExplainStrictTypesSniff.php">
<exclude-pattern>*/*Test\.php</exclude-pattern>
<exclude-pattern>*/exemplar\.php</exclude-pattern>
<exclude-pattern>src/*</exclude-pattern>
</rule>
</ruleset>

0 comments on commit cc97047

Please sign in to comment.