Skip to content

Commit

Permalink
Merge pull request #43 from andrey-mokhov/psalm-configure
Browse files Browse the repository at this point in the history
Configure psalm validation
  • Loading branch information
andrey-mokhov committed Dec 5, 2023
2 parents 1619da1 + 3566ba6 commit 3ddd717
Show file tree
Hide file tree
Showing 40 changed files with 432 additions and 71 deletions.
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# editorconfig.org

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[{*.yaml,*.yml}]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
88 changes: 87 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,92 @@ permissions:
contents: read

jobs:
security:
name: Security
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
php: [ '8.3' ] # Note: This workflow requires only the LATEST version of PHP
os: [ ubuntu-latest ]
stability: [prefer-stable]

steps:
# General Steps
- name: Set Git To Use LF
run: |
git config --global core.autocrlf false
git config --global core.eol lf
- name: Checkout
uses: actions/checkout@v3

- name: Validate composer.json
run: composer validate --strict

- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Install dependencies
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress

# Execution
- name: Security Advisories
run: composer require --dev roave/security-advisories:dev-latest
static-analysis:
name: Psalm
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
php: ['8.3'] # Note: This workflow requires only the LATEST version of PHP
os: [ubuntu-latest]
stability: [prefer-stable]

steps:
# General Steps
- name: Set Git To Use LF
run: |
git config --global core.autocrlf false
git config --global core.eol lf
- name: Checkout
uses: actions/checkout@v3

- name: Validate composer.json
run: composer validate --strict

- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Install dependencies
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress

# Execution
- name: Static Analysis
run: |
vendor/bin/psalm --no-cache --shepherd --stats --output-format=checkstyle --php-version=${{ matrix.php }}
test:
name: Build (${{matrix.php}}, ${{ matrix.os }}, ${{ matrix.stability }})
runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -44,7 +130,7 @@ jobs:
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Install dependencies
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
composer.lock
vendor
build
psalm-cache
73 changes: 73 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
preset: psr12
risky: true

version: 8

enabled:
- alpha_ordered_traits
- array_indentation
- array_push
- combine_consecutive_issets
- combine_consecutive_unsets
- combine_nested_dirname
- declare_strict_types
- dir_constant
- fully_qualified_strict_types
- function_to_constant
- is_null
- magic_constant_casing
- magic_method_casing
- method_separation
- modernize_types_casting
- native_function_casing
- native_function_type_declaration_casing
- no_alias_functions
- no_empty_comment
- no_empty_phpdoc
- no_empty_statement
- no_extra_block_blank_lines
- no_short_bool_cast
- no_superfluous_elseif
- no_unneeded_control_parentheses
- no_unneeded_curly_braces
- no_unneeded_final_method
- no_unset_cast
- no_unused_imports
- no_unused_lambda_imports
- no_useless_else
- no_useless_return
- normalize_index_brace
- php_unit_dedicate_assert
- php_unit_dedicate_assert_internal_type
- php_unit_expectation
- php_unit_mock
- php_unit_mock_short_will_return
- php_unit_namespaced
- php_unit_no_expectation_annotation
- phpdoc_no_empty_return
- phpdoc_no_useless_inheritdoc
- phpdoc_order
- phpdoc_property
- phpdoc_scalar
- phpdoc_separation
- phpdoc_singular_inheritdoc
- phpdoc_trim
- phpdoc_trim_consecutive_blank_line_separation
- phpdoc_type_to_var
- phpdoc_types
- phpdoc_types_order
- print_to_echo
- regular_callable_call
- return_assignment
- self_accessor
- self_static_accessor
- set_type_to_cast
- short_array_syntax
- short_list_syntax
- simplified_if_return
- single_quote
- standardize_not_equals
- ternary_to_null_coalescing
- trailing_comma_in_multiline_array
- unalign_double_arrow
- unalign_equals
15 changes: 14 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{
"name": "andi-lab/graphql-php",
"description": "GraphQL library for PHP",
"keywords": [
"andi-lab",
"graphql",
"graphql-php"
],
"type": "library",
"require": {
"php": "^8.1",
Expand Down Expand Up @@ -30,6 +35,14 @@
},
"require-dev": {
"phpunit/phpunit": "^10.2",
"mockery/mockery": "^1.6"
"mockery/mockery": "^1.6",
"vimeo/psalm": "^5.17"
},
"scripts": {
"test": "vendor/bin/phpunit",
"psalm": "vendor/bin/psalm --config=psalm.xml.dist ./src"
},
"config": {
"sort-packages": true
}
}
18 changes: 18 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<psalm
errorLevel="4"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedBaselineEntry="true"
findUnusedCode="false"
cacheDirectory="./psalm-cache"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
3 changes: 2 additions & 1 deletion src/Argument/AbstractArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function getName(): string

public function getDescription(): ?string
{
/** @psalm-suppress RedundantPropertyInitializationCheck */
return $this->description ?? null;
}

Expand All @@ -39,7 +40,7 @@ public function getType(): string

public function getMode(): int
{
return $this->mode ?? 0;
return $this->mode;
}

public function hasDefaultValue(): bool
Expand Down
1 change: 1 addition & 0 deletions src/Argument/Argument.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function __construct(

public function getDeprecationReason(): ?string
{
/** @psalm-suppress RedundantPropertyInitializationCheck */
return $this->deprecationReason ?? null;
}

Expand Down
1 change: 1 addition & 0 deletions src/ArgumentResolver/Middleware/Next.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function __construct(

public function resolve(mixed $argument): array
{
/** @psalm-suppress RedundantPropertyInitializationCheck */
if (! isset($this->queue)) {
throw new NextHandlerIsEmptyException('Cannot invoke pipeline resolver more than once');
}
Expand Down
1 change: 1 addition & 0 deletions src/Common/InputObjectFieldNameTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ trait InputObjectFieldNameTrait
private function getInputObjectFieldName(ReflectionMethod $method, ?InputObjectField $attribute): string
{
if ($attribute?->name) {
assert($attribute->name !== null);
return $attribute->name;
}

Expand Down
1 change: 1 addition & 0 deletions src/Common/LazyParserType.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function __invoke(): Webonyx\Type
$type = $this->typeRegistry->get($this->type);

if (TypeAwareInterface::ITEM_IS_REQUIRED === (TypeAwareInterface::ITEM_IS_REQUIRED & $this->mode)) {
assert($type instanceof Webonyx\NullableType);
$type = Webonyx\Type::nonNull($type);
}

Expand Down
1 change: 1 addition & 0 deletions src/Common/LazyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __invoke(): Webonyx\Type
$mode = $this->type->getMode();

if (TypeAwareInterface::ITEM_IS_REQUIRED === (TypeAwareInterface::ITEM_IS_REQUIRED & $mode)) {
assert($type instanceof Webonyx\NullableType);
$type = Webonyx\Type::nonNull($type);
}

Expand Down
17 changes: 10 additions & 7 deletions src/Common/LazyTypeByReflectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function __invoke(): Webonyx\Type

private function getTypeFromReflectionNamedType(): Webonyx\Type
{
assert($this->type instanceof ReflectionNamedType);
if ($this->type->isBuiltin()) {
try {
$type = match ($this->type->getName()) {
Expand All @@ -51,26 +52,28 @@ private function getTypeFromReflectionNamedType(): Webonyx\Type
} else {
$name = $this->type->getName();

/** @psalm-suppress TypeDoesNotContainType */
$type = 'self' === $name || 'static' === $name
? $this->typeRegistry->get($this->selfClassName)
: $this->typeRegistry->get($name);
}

return $this->type->allowsNull()
? $type
: Webonyx\Type::nonNull($type);
return !$this->type->allowsNull() && $type instanceof Webonyx\NullableType
? Webonyx\Type::nonNull($type)
: $type;
}

/**
* @return Webonyx\Type
*/
private function getTypeFromReflectionUnionType(): Webonyx\Type
{
/** @var ReflectionType $type */
$names = [];
$types = [];
$allowsNull = false;
assert($this->type instanceof ReflectionUnionType);
foreach ($this->type->getTypes() as $type) {
assert($type instanceof ReflectionNamedType);
if ($type->isBuiltin()) {
$allowsNull = 'null' === $type->getName()
|| throw new CantResolveGraphQLTypeException('UnionType must contains only ObjectTypes');
Expand Down Expand Up @@ -99,9 +102,9 @@ private function getTypeFromReflectionUnionType(): Webonyx\Type
if ($this->typeRegistry->has($name)) {
$existsType = $this->typeRegistry->get($name);

return $this->type->allowsNull() || $allowsNull
? $existsType
: Webonyx\Type::nonNull($existsType);
return !$this->type->allowsNull() && !$allowsNull && $existsType instanceof Webonyx\NullableType
? Webonyx\Type::nonNull($existsType)
: $existsType;
}

$unionType = new Webonyx\UnionType([
Expand Down
4 changes: 2 additions & 2 deletions src/Common/LazyTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
final class LazyTypeResolver
{
/**
* @var pure-callable(mixed, mixed, Webonyx\ResolveInfo): ?string
* @var callable(mixed, mixed, Webonyx\ResolveInfo): ?string
*/
private readonly mixed $type;

/**
* @param pure-callable(mixed, mixed, Webonyx\ResolveInfo): ?string $type
* @param callable(mixed, mixed, Webonyx\ResolveInfo): ?string $type
* @param TypeRegistryInterface $typeRegistry
*/
public function __construct(
Expand Down
2 changes: 2 additions & 0 deletions src/Field/AbstractInputObjectField.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function getName(): string

public function getDescription(): ?string
{
/** @psalm-suppress RedundantPropertyInitializationCheck */
return $this->description ?? null;
}

Expand All @@ -52,6 +53,7 @@ public function getMode(): int

public function getDeprecationReason(): ?string
{
/** @psalm-suppress RedundantPropertyInitializationCheck */
return $this->deprecationReason ?? null;
}

Expand Down

0 comments on commit 3ddd717

Please sign in to comment.