Skip to content

Commit

Permalink
Repository maintenance, adds static code analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelstolt committed May 14, 2022
1 parent 83edbd6 commit 195226b
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 58 deletions.
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
* text=auto eol=lf

/tests export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.github/ export-ignore
Expand All @@ -9,5 +8,7 @@
CHANGELOG.md export-ignore
CONTRIBUTING.md export-ignore
LICENSE.md export-ignore
phpstan.neon.dist export-ignore
phpunit.xml.dist export-ignore
README.md export-ignore
tests/ export-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ jobs:
fail-fast: true
matrix:
php:
- "7.4"
- "8.0"
- "8.1"

steps:
Expand Down
29 changes: 29 additions & 0 deletions .github/workflows/static-analyse.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Static Analyse

on: push

jobs:
tests:
name: Static Analyse
runs-on: ubuntu-latest

strategy:
fail-fast: true
matrix:
php:
- "8.1"

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: "${{ matrix.php }}"

- name: Install Composer dependencies
run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader

- name: Run static analyse
run: composer run-script static-analyse
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
},
"require-dev": {
"phpunit/phpunit": "8.*",
"friendsofphp/php-cs-fixer": "^3.0"
"friendsofphp/php-cs-fixer": "^3.0",
"phpstan/phpstan": "^1.6"
},
"autoload": {
"psr-0": {
Expand All @@ -32,7 +33,8 @@
"scripts": {
"test": "phpunit",
"cs-fix": "php-cs-fixer fix . -vv || true",
"cs-lint": "php-cs-fixer fix --diff --stop-on-violation --verbose --dry-run"
"cs-lint": "php-cs-fixer fix --diff --stop-on-violation --verbose --dry-run",
"static-analyse": "phpstan analyse --configuration phpstan.neon.dist"
},
"extra": {
"branch-alias": {
Expand Down
14 changes: 14 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
parameters:
level: max
paths:
- src
reportUnmatchedIgnoredErrors: false
checkMissingIterableValueType: false
ignoreErrors:
- '#function json_decode expects string#'
- '#Pointer#'
- '#function count expects#'
- '#Strict comparison using === between int#'
- '#array_splice#'
- '#\$possiblePatchOperation#'
- '#mixed supplied for foreach#'
24 changes: 4 additions & 20 deletions src/Rs/Json/Patch/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ class Document
private $allowedPatchOperations;

/**
* @param string $patchDocument
* @param int $allowedPatchOperations
*
* @throws \Rs\Json\Patch\InvalidOperationException
*/
public function __construct($patchDocument, $allowedPatchOperations = null)
public function __construct(string $patchDocument, int $allowedPatchOperations = null)
{
$defaultPatchOperations = Add::APPLY | Copy::APPLY | Move::APPLY | Remove::APPLY | Replace::APPLY | Test::APPLY;
$this->allowedPatchOperations = null !== $allowedPatchOperations ? $allowedPatchOperations : $defaultPatchOperations;
Expand All @@ -43,7 +40,7 @@ public function getPatchOperations()
}

/**
* @param string $patchDocument The patch document containing the patch operations.
* @param iterable|string $patchDocument The patch document containing the patch operations.
*
* @throws \Rs\Json\Patch\InvalidOperationException
* @return Operation[]
Expand Down Expand Up @@ -71,21 +68,14 @@ private function extractPatchOperations($patchDocument)
return $patchOperations;
}

/**
* @param mixed $patchDocument
*
* @return bool
*/
private function isEmptyPatchDocument($patchDocument)
private function isEmptyPatchDocument(mixed $patchDocument):bool
{
return (empty($patchDocument) || !is_array($patchDocument) || count($patchDocument) === 0);
}

/**
* @param \stdClass $possiblePatchOperation
*
* @throws \Rs\Json\Patch\InvalidOperationException
* @return \Rs\Json\Patch\Operation or null on unsupported patch operation
* @return \Rs\Json\Patch\Operation| null on unsupported patch operation
*/
private function patchOperationFactory(\stdClass $possiblePatchOperation)
{
Expand All @@ -104,42 +94,36 @@ private function patchOperationFactory(\stdClass $possiblePatchOperation)
}

return new Add($possiblePatchOperation);
break;
case 'copy':
if (!(($this->allowedPatchOperations & Copy::APPLY) == Copy::APPLY)) {
return null;
}

return new Copy($possiblePatchOperation);
break;
case 'move':
if (!(($this->allowedPatchOperations & Move::APPLY) == Move::APPLY)) {
return null;
}

return new Move($possiblePatchOperation);
break;
case 'replace':
if (!(($this->allowedPatchOperations & Replace::APPLY) == Replace::APPLY)) {
return null;
}

return new Replace($possiblePatchOperation);
break;
case 'remove':
if (!(($this->allowedPatchOperations & Remove::APPLY) == Remove::APPLY)) {
return null;
}

return new Remove($possiblePatchOperation);
break;
case 'test':
if (!(($this->allowedPatchOperations & Test::APPLY) == Test::APPLY)) {
return null;
}

return new Test($possiblePatchOperation);
break;
default:
return null;
}
Expand Down
10 changes: 3 additions & 7 deletions src/Rs/Json/Patch/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function getName()
}

/**
* @return string|array
* @return string
*/
public function getPath()
{
Expand All @@ -83,19 +83,15 @@ protected function getPointerParts()
);
}

/**
* @param mixed $targetDocument
*
* @return mixed
*/
abstract public function perform($targetDocument);
abstract public function perform(mixed $targetDocument): mixed;

/**
* Guard the mandatory operation properties
*
* @param \stdClass $operation The operation structure.
*
* @throws \Rs\Json\Patch\InvalidOperationException
* @return void
*/
abstract protected function assertMandatories(\stdClass $operation);
}
5 changes: 1 addition & 4 deletions src/Rs/Json/Patch/Operations/Add.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,11 @@ private function getReplacementValue()
}

/**
* @param string $targetDocument
*
* @throws \Rs\Json\Pointer\InvalidJsonException
* @throws \Rs\Json\Pointer\InvalidPointerException
* @throws \Rs\Json\Pointer\NonWalkableJsonException
* @return string
*/
public function perform($targetDocument)
public function perform(mixed $targetDocument): mixed
{
$pointer = new Pointer($targetDocument);
$rootGet = array();
Expand Down
5 changes: 1 addition & 4 deletions src/Rs/Json/Patch/Operations/Copy.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,13 @@ protected function assertMandatories(\stdClass $operation)
}

/**
* @param string $targetDocument
*
* @throws \Rs\Json\Patch\InvalidOperationException
* @throws \Rs\Json\Pointer\InvalidJsonException
* @throws \Rs\Json\Pointer\InvalidPointerException
* @throws \Rs\Json\Pointer\NonWalkableJsonException
* @throws \RuntimeException
* @return string
*/
public function perform($targetDocument)
public function perform(mixed $targetDocument): mixed
{
$pointer = new Pointer($targetDocument);
try {
Expand Down
3 changes: 1 addition & 2 deletions src/Rs/Json/Patch/Operations/Move.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ protected function assertMandatories(\stdClass $operation)
* @throws \Rs\Json\Pointer\InvalidPointerException
* @throws \Rs\Json\Pointer\NonWalkableJsonException
* @throws \RuntimeException
* @return string
*/
public function perform($targetDocument)
public function perform(mixed $targetDocument):mixed
{
$pointer = new Pointer($targetDocument);
try {
Expand Down
6 changes: 3 additions & 3 deletions src/Rs/Json/Patch/Operations/Remove.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function __construct(\stdClass $operation)
* Guard the mandatory operation properties
*
* @param \stdClass $operation The operation structure.
* @return void
*/
protected function assertMandatories(\stdClass $operation)
{
Expand All @@ -43,9 +44,8 @@ protected function assertMandatories(\stdClass $operation)
* @throws \Rs\Json\Pointer\InvalidPointerException
* @throws \Rs\Json\Pointer\NonWalkableJsonException
* @throws \RuntimeException
* @return string
*/
public function perform($targetDocument)
public function perform(mixed $targetDocument):string|false
{
$pointer = new Pointer($targetDocument);
try {
Expand All @@ -64,7 +64,7 @@ public function perform($targetDocument)
* @param array|\stdClass $json The json_decode'd Json structure.
* @param array $pointerParts The parts of the fed pointer.
*/
private function remove(&$json, array $pointerParts)
private function remove(&$json, array $pointerParts):void
{
$pointerPart = array_shift($pointerParts);

Expand Down
8 changes: 3 additions & 5 deletions src/Rs/Json/Patch/Operations/Replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ public function __construct(\stdClass $operation)
* Guard the mandatory operation property
*
* @param \stdClass $operation The operation structure.
*
* @throws \Rs\Json\Patch\InvalidOperationException
*/
protected function assertMandatories(\stdClass $operation)
protected function assertMandatories(\stdClass $operation):void
{
if (!property_exists($operation, 'value')) {
throw new InvalidOperationException('Mandatory value property not set');
Expand All @@ -49,9 +48,8 @@ protected function assertMandatories(\stdClass $operation)
* @throws \Rs\Json\Pointer\InvalidPointerException
* @throws \Rs\Json\Pointer\NonWalkableJsonException
* @throws \RuntimeException
* @return string
*/
public function perform($targetDocument)
public function perform(mixed $targetDocument): string|false
{
$pointer = new Pointer($targetDocument);
try {
Expand All @@ -76,7 +74,7 @@ public function perform($targetDocument)
* @param array $pointerParts The parts of the fed pointer.
* @param mixed $value The value to replace.
*/
private function replace(&$json, array $pointerParts, $value = null)
private function replace(&$json, array $pointerParts, $value = null):void
{
$pointerPart = array_shift($pointerParts);

Expand Down
11 changes: 3 additions & 8 deletions src/Rs/Json/Patch/Operations/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ public function __construct(\stdClass $operation)
* @throws \Rs\Json\Pointer\InvalidPointerException
* @throws \Rs\Json\Pointer\NonWalkableJsonException
* @throws \RuntimeException
* @return boolean
*/
public function perform($targetDocument)
public function perform(mixed $targetDocument): string|bool
{
$pointer = new Pointer($targetDocument);

Expand Down Expand Up @@ -83,7 +82,7 @@ public function perform($targetDocument)
*
* @throws \Rs\Json\Patch\InvalidOperationException
*/
protected function assertMandatories(\stdClass $operation)
protected function assertMandatories(\stdClass $operation):void
{
if (!property_exists($operation, 'value')) {
throw new InvalidOperationException('Mandatory value property not set');
Expand All @@ -92,12 +91,8 @@ protected function assertMandatories(\stdClass $operation)

/**
* Check if string is a valid JSON string
*
* @param string $string
*
* @return boolean
*/
private function isValidJsonString($string)
private function isValidJsonString(mixed $string):bool
{
if (is_string($string) && strlen($string)) {
// Decode and check last error
Expand Down

0 comments on commit 195226b

Please sign in to comment.