Skip to content

Commit

Permalink
Use pint for code style (#161)
Browse files Browse the repository at this point in the history
* Install pint

* Use pint in github actions

* Fix cs

* Apply fixes from StyleCI (#160)

* cleanup

* Apply fixes from StyleCI (#163)

* Upgrade actions

* PHP Linting (Pint)

* Use action/cache@v3

* Remove pint when installing dependencies for tests

---------

Co-authored-by: Adam Campbell <[email protected]>
Co-authored-by: bastien-phi <[email protected]>
  • Loading branch information
3 people authored Jan 17, 2024
1 parent 6b531a9 commit cf33560
Show file tree
Hide file tree
Showing 18 changed files with 187 additions and 337 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ jobs:
name: P${{ matrix.php }} - L${{ matrix.laravel }}

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Validate composer.json and composer.lock
run: composer validate

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-${{ matrix.php }}--${{ matrix.laravel }}--node-${{ hashFiles('**/composer.lock') }}
Expand All @@ -63,6 +63,7 @@ jobs:
- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: |
composer remove --dev laravel/pint --no-interaction --no-update
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --prefer-dist --no-progress --no-suggest
Expand Down
13 changes: 7 additions & 6 deletions .github/workflows/tidy.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
name: Tidy
on:
push:
branches:
- develop
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
lint:
runs-on: ubuntu-latest
steps:

- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 2
ref: ${{ github.head_ref }}

- name: "laravel-pint"
uses: aglipanci/laravel-pint-action@1.0.0
uses: aglipanci/laravel-pint-action@2.3.1
with:
preset: laravel

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: PHP Linting (Pint)
skip_fetch: true
30 changes: 15 additions & 15 deletions _ide_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
/**
* @see \Spectator\Assertions
*
* @method static assertValidRequest()
* @method static assertInvalidRequest()
* @method static assertValidResponse($status = null)
* @method static assertInvalidResponse($status = null)
* @method static assertValidationMessage($expected)
* @method static assertErrorsContain($errors)
* @method static dumpSpecErrors()
* @method static void skipRequestValidation()
* @method static assertValidRequest()
* @method static assertInvalidRequest()
* @method static assertValidResponse($status = null)
* @method static assertInvalidResponse($status = null)
* @method static assertValidationMessage($expected)
* @method static assertErrorsContain($errors)
* @method static dumpSpecErrors()
* @method static void skipRequestValidation()
*/
class TestResponse
{
Expand All @@ -27,13 +27,13 @@ class TestResponse
/**
* @see \Spectator\Assertions
*
* @method static assertValidRequest()
* @method static assertInvalidRequest()
* @method static assertValidResponse($status = null)
* @method static assertInvalidResponse($status = null)
* @method static assertValidationMessage($expected)
* @method static assertErrorsContain($errors)
* @method static void skipRequestValidation()
* @method static assertValidRequest()
* @method static assertInvalidRequest()
* @method static assertValidResponse($status = null)
* @method static assertInvalidResponse($status = null)
* @method static assertValidationMessage($expected)
* @method static assertErrorsContain($errors)
* @method static void skipRequestValidation()
*/
class TestResponse
{
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"opis/json-schema": "^2.0"
},
"require-dev": {
"laravel/pint": "^1.13",
"nunomaduro/collision": "^5.1|^6.0",
"orchestra/testbench": "^6.0|^7.0|^8.0",
"phpunit/phpunit": "^9.5"
Expand Down
12 changes: 6 additions & 6 deletions config/spectator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@

'sources' => [
'local' => [
'source' => 'local',
'source' => 'local',
'base_path' => env('SPEC_PATH'),
],

'remote' => [
'source' => 'remote',
'source' => 'remote',
'base_path' => env('SPEC_PATH'),
'params' => env('SPEC_URL_PARAMS', ''),
'params' => env('SPEC_URL_PARAMS', ''),
],

'github' => [
'source' => 'github',
'source' => 'github',
'base_path' => env('SPEC_GITHUB_PATH'),
'repo' => env('SPEC_GITHUB_REPO'),
'token' => env('SPEC_GITHUB_TOKEN'),
'repo' => env('SPEC_GITHUB_REPO'),
'token' => env('SPEC_GITHUB_TOKEN'),
],
],

Expand Down
2 changes: 1 addition & 1 deletion src/Assertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function assertErrorsContain()
return fn ($errors) => $this->runAssertion(function () use ($errors) {
$matches = 0;

if (!is_array($errors)) {
if (! is_array($errors)) {
$errors = [$errors];
}

Expand Down
4 changes: 2 additions & 2 deletions src/Concerns/HasExpectations.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function expectsFalse()
* @param array $exceptions
* @return void
*/
return function (Throwable $throwable = null, array $exceptions = []) {
return function (?Throwable $throwable = null, array $exceptions = []) {
if ($throwable) {
$class = get_class($throwable);

Expand All @@ -35,7 +35,7 @@ public function expectsTrue()
* @param array $exceptions
* @return void
*/
return function (Throwable $throwable = null, array $exceptions = []) {
return function (?Throwable $throwable = null, array $exceptions = []) {
if ($throwable) {
$class = get_class($throwable);

Expand Down
64 changes: 18 additions & 46 deletions src/Exceptions/SchemaValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,9 @@

abstract class SchemaValidationException extends \Exception implements ExceptionInterface
{
/**
* @var array
*/
protected array $errors = [];

/**
* @param string $message
* @param ValidationError $error
*
* @return static
*/
public static function withError(string $message, ValidationError $error): self
Expand All @@ -31,9 +25,6 @@ public static function withError(string $message, ValidationError $error): self
return $instance;
}

/**
* @param ValidationError $error
*/
protected function setErrors(ValidationError $error)
{
$formatter = new ErrorFormatter();
Expand All @@ -43,8 +34,6 @@ protected function setErrors(ValidationError $error)

/**
* Return the exception errors.
*
* @return array
*/
public function getErrors(): array
{
Expand All @@ -53,8 +42,6 @@ public function getErrors(): array

/**
* Check if the exception has errors.
*
* @return bool
*/
public function hasErrors(): bool
{
Expand All @@ -65,10 +52,8 @@ public function hasErrors(): bool
* Given a schema and ValidationError, returns a helpful error message where the validation
* errors are mapped over a structured schema representation.
*
* @param array|object $schema A JSON schema.
* @param ValidationError $validation_error A validation error generated by Opis\JsonSchema.
*
* @return string
* @param array|object $schema A JSON schema.
* @param ValidationError $validation_error A validation error generated by Opis\JsonSchema.
*/
public static function validationErrorMessage($schema, ValidationError $validation_error): string
{
Expand Down Expand Up @@ -104,7 +89,7 @@ public static function validationErrorMessage($schema, ValidationError $validati
// error alongside the item.
$strings = [];

if (!is_null($schema_formatted)) {
if (! is_null($schema_formatted)) {
foreach ($schema_formatted as $key => $schema_item) {
if (isset($error_location_map[$key])) {
$schema_item = self::colorize($schema_item, Format::TEXT_LIGHT_GREY);
Expand All @@ -127,9 +112,8 @@ public static function validationErrorMessage($schema, ValidationError $validati
/**
* Returns a formatted validation error.
*
* @param ValidationError $validation_error A validation error generated by Opis\JsonSchema.
* @param bool $flat Should the formatted error be flat (a simple array) or structured?
*
* @param ValidationError $validation_error A validation error generated by Opis\JsonSchema.
* @param bool $flat Should the formatted error be flat (a simple array) or structured?
* @return array|string
*/
public static function formatValidationError(ValidationError $validation_error, bool $flat = false)
Expand Down Expand Up @@ -167,13 +151,11 @@ public static function formatValidationError(ValidationError $validation_error,
* status*: string
* message*: string <== The data (integer) must match the type: string
*
* @param array $schema JSON schema represented as an array.
* @param string $location_current The current location within the JSON schema structure.
* @param string $key_current The key at the current location, if one is present.
* @param array $keys_required The keys required at the current location, if provided.
* @param int $indent_level Represents how much newly added values should be indented.
*
* @return array
* @param array $schema JSON schema represented as an array.
* @param string $location_current The current location within the JSON schema structure.
* @param string $key_current The key at the current location, if one is present.
* @param array $keys_required The keys required at the current location, if provided.
* @param int $indent_level Represents how much newly added values should be indented.
*/
public static function formatSchema(array $schema, string $location_current, string $key_current, array $keys_required, int $indent_level): array
{
Expand All @@ -186,7 +168,7 @@ public static function formatSchema(array $schema, string $location_current, str
}, ARRAY_FILTER_USE_KEY);
$polymorphic_keys = array_values(array_flip($polymorphic_keys));

if (!empty($polymorphic_keys)) { // first, check for a polymorphic schema...
if (! empty($polymorphic_keys)) { // first, check for a polymorphic schema...
$polymorphic_key = $polymorphic_keys[0];

// create entry for polymorphic schema
Expand All @@ -203,7 +185,7 @@ public static function formatSchema(array $schema, string $location_current, str
} elseif (isset($schema['type'])) { // otherwise, check for explicit schema type...
// convert "type" to an array (to support single/multiple types)
$types = [];
if (!is_array($schema['type'])) {
if (! is_array($schema['type'])) {
$types = [$schema['type']];
} else {
$types = $schema['type'];
Expand Down Expand Up @@ -300,12 +282,10 @@ public static function formatSchema(array $schema, string $location_current, str
* Applies modifiers to schema's key name and value, if provided. Then returns display string
* for the schema.
*
* @param string $type The schema's type (ex: allOf, object, string).
* @param string $type_modifier A modifier for the type (ex: "++" in "object++").
* @param string $key The schema's key (ex: "name" in "name: string").
* @param string $key_modifier A modifier for the key (ex: "?" in "string?").
*
* @return string
* @param string $type The schema's type (ex: allOf, object, string).
* @param string $type_modifier A modifier for the type (ex: "++" in "object++").
* @param string $key The schema's key (ex: "name" in "name: string").
* @param string $key_modifier A modifier for the key (ex: "?" in "string?").
*/
protected static function schemaItemDisplayString(string $type, string $type_modifier = '', string $key = '', string $key_modifier = ''): string
{
Expand All @@ -318,10 +298,8 @@ protected static function schemaItemDisplayString(string $type, string $type_mod
/**
* Applies indentation to a display string and returns it.
*
* @param string $display_string A string to display.
* @param int $indent_level The level of indentation to apply to the display string.
*
* @return string
* @param string $display_string A string to display.
* @param int $indent_level The level of indentation to apply to the display string.
*/
protected static function indentedDisplayString(string $display_string, int $indent_level = 0): string
{
Expand All @@ -330,12 +308,6 @@ protected static function indentedDisplayString(string $display_string, int $ind

/**
* Colorize text.
*
* @param string $text
* @param string $text_color
* @param string $style
*
* @return string
*/
protected static function colorize(string $text, string $text_color, string $style = ''): string
{
Expand Down
Loading

0 comments on commit cf33560

Please sign in to comment.