Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/.phpcs.xml.dist export-ignore
/box.json export-ignore
/.github export-ignore
/tests export-ignore
30 changes: 30 additions & 0 deletions .github/workflows/qa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,33 @@ jobs:
uses: docker://rhysd/actionlint:latest
with:
args: -color

phpcs:
name: 'PHPCS'
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false

- name: Install PHP
uses: shivammathur/setup-php@accd6127cb78bee3e8082180cb391013d204ef9f # 2.37.0
with:
php-version: 'latest'
coverage: none
tools: cs2pr

# Install dependencies and handle caching in one go.
# @link https://github.com/marketplace/actions/install-composer-dependencies
- name: Install Composer dependencies
uses: "ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda" # 4.0.0

# Check the code-style consistency of the PHP files.
- name: Check PHP code style
continue-on-error: true
run: composer checkcs -- --report-full --report-checkstyle=./phpcs-report.xml

- name: Show PHPCS results in PR
run: cs2pr ./phpcs-report.xml
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ tests/cache
vendor/
composer.phar
.DS_Store
.phpcs.xml
phpcs.xml

# Ignore PhpStorm project related files.
.idea
Expand Down
99 changes: 99 additions & 0 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
name="Patchwork"
xsi:noNamespaceSchemaLocation="./vendor/squizlabs/php_codesniffer/phpcs.xsd">

<description>Patchwork rules for PHP_CodeSniffer</description>

<!--
#############################################################################
COMMAND LINE ARGUMENTS
https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Annotated-Ruleset
#############################################################################
-->

<!-- Scan all files. -->
<file>.</file>

<!-- Third party files and build files don't need to comply with these coding standards. -->
<exclude-pattern>*/vendor/*</exclude-pattern>

<!-- Test fixtures don't need to comply with these coding standards
as we may be specifically testing a variation in how syntax can be written.
-->
<exclude-pattern>*/tests/includes/*</exclude-pattern>

<!-- Only check PHP files.
Note: at this time PHPCS can not scan .phpt files.
There is a feature request open to change this.
https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/1363
-->
<arg name="extensions" value="php"/>

<!-- Show progress, show the error codes for each message (source). -->
<arg value="ps"/>

<!-- Strip the filepaths down to the relevant bit. -->
<arg name="basepath" value="./"/>

<!-- Check up to 8 files simultaneously. -->
<arg name="parallel" value="8"/>


<!--
#############################################################################
CHECK FOR PHP CROSS-VERSION COMPATIBILITY
#############################################################################
-->

<config name="testVersion" value="7.1-"/>
<rule ref="PHPCompatibility"/>


<!--
#############################################################################
FOLLOW PSR12 for CODING STYLE
#############################################################################
-->

<rule ref="PSR12">
<!-- Re-organising the files should be a separate consideration and needs discussion
about whether or not to adopt PSR-4 or another convention.
-->
<exclude name="PSR1.Files.SideEffects"/>
<exclude name="PSR1.Classes.ClassDeclaration.MultipleClasses"/>
</rule>


<!--
#############################################################################
SNIFF SPECIFIC CONFIGURATION
#############################################################################
-->

<!-- Allow for slightly longer line length than PSR12 (120) allows for. -->
<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="140" />
</properties>
</rule>


<!--
#############################################################################
SELECTIVE EXCLUSIONS
Exclude specific files for specific sniffs.
#############################################################################
-->

<!-- Renaming these methods from snake_case to camelCaps would be a BC-break. -->
<rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
<exclude-pattern>/src/CodeManipulation/Stream\.php$</exclude-pattern>
</rule>

<!-- Renaming these constants from camelCaps to ALL_CAPPS would be a BC-break. -->
<rule ref="Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase">
<exclude-pattern>/src/CodeManipulation/Actions/RedefinitionOfNew\.php$</exclude-pattern>
</rule>

</ruleset>
30 changes: 28 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,40 @@
"email": "ignas.rudaitis@gmail.com"
}
],
"minimum-stability": "stable",
"require": {
"php": ">=7.1.0"
},
"require-dev": {
"phpunit/phpunit": ">=4"
"phpcompatibility/php-compatibility": "^10.0.0@alpha",
"phpunit/phpunit": ">=4",
"squizlabs/php_codesniffer": "^3.13.5 || ^4.0"
},
"minimum-stability": "alpha",
"prefer-stable": true,
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
},
"lock": false
},
"scripts": {
"checkcs": [
"@php ./vendor/squizlabs/php_codesniffer/bin/phpcs"
],
"fixcs": [
"@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf"
],
"test": [
"@php ./vendor/phpunit/phpunit/phpunit --no-coverage"
],
"coverage": [
"@php ./vendor/phpunit/phpunit/phpunit"
]
},
"scripts-descriptions": {
"checkcs": "Check the PHP files for code style violations and best practices.",
"fixcs": "Auto-fix code style violations in the PHP files.",
"test": "Run the unit tests without code coverage.",
"coverage": "Run the unit tests with code coverage."
}
}
1 change: 1 addition & 0 deletions src/CallRerouting.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ function connectOnHHVM($function, Handle $handle)
} elseif (is_object($obj)) {
$calledClass = get_class($obj);
}
// phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection -- Verified okay.
$frame = count(debug_backtrace(0)) - 1;
$result = null;
$done = dispatch($class, $calledClass, $method, $frame, $result, $args);
Expand Down
5 changes: 5 additions & 0 deletions src/CodeManipulation/Actions/Namespaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function getNamespaceAt(Source $s, $pos)

function collectNamespaceBoundaries(Source $s)
{
// phpcs:disable PHPCompatibility.FunctionDeclarations.NewClosure.ThisFoundOutsideClass -- Using $this is okay. Source::cache() binds the closure.
return $s->cache([], function () {
if (!$this->has(T_NAMESPACE)) {
return ['' => [[0, INF]]];
Expand All @@ -71,6 +72,7 @@ function collectNamespaceBoundaries(Source $s)
}
return $result;
});
// phpcs:enable
}

function collectUseDeclarations(Source $s, $begin)
Expand All @@ -84,6 +86,8 @@ function collectUseDeclarations(Source $s, $begin)
}
}
}

// phpcs:disable PHPCompatibility.FunctionDeclarations.NewClosure.ThisFoundOutsideClass -- Using $this is okay. Source::cache() binds the closure.
return $s->cache([$begin], function ($begin) {
$result = ['class' => [], 'function' => [], 'const' => []];
# only tokens that are siblings bracket-wise are considered,
Expand All @@ -98,6 +102,7 @@ function collectUseDeclarations(Source $s, $begin)
}
return $result;
});
// phpcs:enable
}

function parseUseDeclaration(Source $s, $pos, array &$aliases, $prefix = '', $type = 'class')
Expand Down
1 change: 1 addition & 0 deletions src/CodeManipulation/Actions/RedefinitionOfNew.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

const STATIC_INSTANTIATION_REPLACEMENT = '\Patchwork\CallRerouting\getInstantiator(\'%s\', %s)->instantiate(%s)';
const DYNAMIC_INSTANTIATION_REPLACEMENT = '\Patchwork\CallRerouting\getInstantiator(%s, %s)->instantiate(%s)';
// phpcs:ignore Generic.Files.LineLength.TooLong
const CALLED_CLASS = '((__CLASS__ && __FUNCTION__ !== (__NAMESPACE__ ? __NAMESPACE__ . "\\\\{closure}" : "\\\\{closure}")) ? \get_called_class() : null)';

const spliceAllInstantiations = 'Patchwork\CodeManipulation\Actions\RedefinitionOfNew\spliceAllInstantiations';
Expand Down