Skip to content

Commit 273ca8f

Browse files
committed
Big 💥
0 parents  commit 273ca8f

17 files changed

+568
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/vendor/
2+
/composer.lock
3+
/.phpcs-cache
4+
/phpcs.xml
5+
/phpstan.neon
6+
/phpunit.xml

.scrutinizer.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
build:
2+
nodes:
3+
analysis:
4+
environment:
5+
php:
6+
version: 7.1
7+
cache:
8+
disabled: false
9+
directories:
10+
- ~/.composer/cache
11+
project_setup:
12+
override: true
13+
tests:
14+
override:
15+
- php-scrutinizer-run
16+
- phpcs-run
17+
dependencies:
18+
override:
19+
- composer install --no-interaction --prefer-dist
20+
21+
checks:
22+
php:
23+
code_rating: true
24+
25+
tools:
26+
external_code_coverage: true
27+
28+
build_failure_conditions:
29+
- 'elements.rating(<= C).new.exists' # No new classes/methods with a rating of C or worse allowed
30+
- 'issues.severity(>= MAJOR).new.exists' # New issues of major or higher severity
31+
- 'project.metric_change("scrutinizer.test_coverage", < 0)' # Code Coverage decreased from previous inspection
32+
- 'patches.label("Unused Use Statements").new.exists' # No new unused imports patches allowed

.travis.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
dist: trusty
2+
language: php
3+
sudo: false
4+
5+
cache:
6+
directories:
7+
- $HOME/.composer/cache
8+
9+
php:
10+
- 7.1
11+
- 7.2
12+
- nightly
13+
14+
before_install:
15+
- composer self-update
16+
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{,.disabled} || echo "xdebug not available"
17+
18+
install:
19+
- travis_retry composer update --prefer-dist
20+
21+
script: ./vendor/bin/phpunit
22+
23+
jobs:
24+
include:
25+
- stage: Test
26+
env: COVERAGE
27+
php: 7.1
28+
before_script:
29+
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{.disabled,}
30+
- if [[ ! $(php -m | grep -si xdebug) ]]; then echo "xdebug required for coverage"; exit 1; fi
31+
script:
32+
- ./vendor/bin/phpunit --coverage-clover ./build/logs/clover.xml
33+
after_script:
34+
- wget https://github.com/scrutinizer-ci/ocular/releases/download/1.5.2/ocular.phar
35+
- php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml
36+
37+
- stage: Coding Standard
38+
php: 7.1
39+
script: ./vendor/bin/phpcs
40+
41+
- stage: Static Analysis
42+
php: 7.1
43+
script: ./vendor/bin/phpstan analyse
44+

LICENCE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2018 Šimon Podlipský <[email protected]>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of
4+
this software and associated documentation files (the "Software"), to deal in
5+
the Software without restriction, including without limitation the rights to
6+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7+
of the Software, and to permit persons to whom the Software is furnished to do
8+
so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# PHP GraphQL Utils for graphql-php
2+
3+
[![Build Status](https://travis-ci.org/simPod/GraphQL-Utils.svg)](https://travis-ci.org/simPod/GraphQL-Utils)
4+
[![Downloads](https://poser.pugx.org/simpod/graphql-utils/d/total.svg)](https://packagist.org/packages/simpod/graphql-utils)
5+
[![Packagist](https://poser.pugx.org/simpod/graphql-utils/v/stable.svg)](https://packagist.org/packages/simpod/graphql-utils)
6+
[![Licence](https://poser.pugx.org/simpod/graphql-utils/license.svg)](https://packagist.org/packages/simpod/graphql-utils)
7+
[![Quality Score](https://scrutinizer-ci.com/g/simPod/GraphQL-Utils/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/simPod/GraphQL-Utils)
8+
[![Code Coverage](https://scrutinizer-ci.com/g/simPod/GraphQL-Utils/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/simPod/GraphQL-Utils)
9+
[![GitHub Issues](https://img.shields.io/github/issues/simPod/GraphQL-Utils.svg?style=flat-square)](https://github.com/simPod/GraphQL-Utils/issues)
10+
11+
12+
## Installation
13+
14+
Add as [Composer](https://getcomposer.org/) dependency:
15+
16+
```sh
17+
composer require simpod/graphql-utils
18+
```

composer.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "simpod/graphql-utils",
3+
"description": "GraphQL Utils for graphql-php",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Simon Podlipsky",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"config": {
13+
"sort-packages": true
14+
},
15+
"require": {
16+
"php": "^7.1",
17+
"webonyx/graphql-php": "^0.11"
18+
},
19+
"require-dev": {
20+
"doctrine/coding-standard": "^4.0",
21+
"phpstan/phpstan": "^0.10",
22+
"phpstan/phpstan-phpunit": "^0.10",
23+
"phpstan/phpstan-strict-rules": "^0.10",
24+
"phpunit/phpunit": "^7.0"
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"SimPod\\GraphQL\\Utils\\": "src/"
29+
}
30+
},
31+
"autoload-dev": {
32+
"psr-4": {
33+
"SimPod\\GraphQL\\Utils\\Tests\\": "tests/"
34+
}
35+
}
36+
}

phpcs.xml.dist

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<ruleset>
3+
<arg name="basepath" value="."/>
4+
<arg name="extensions" value="php"/>
5+
<arg name="parallel" value="80"/>
6+
<arg name="cache" value=".phpcs-cache"/>
7+
<arg name="colors"/>
8+
9+
<!-- Ignore warnings, show progress of the run and show sniff names -->
10+
<arg value="nps"/>
11+
12+
<file>src</file>
13+
14+
<rule ref="Doctrine"/>
15+
</ruleset>

phpstan.neon.dist

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
parameters:
2+
level: max
3+
paths:
4+
- %currentWorkingDirectory%/src
5+
- %currentWorkingDirectory%/tests
6+
tmpDir: %currentWorkingDirectory%/var/phpstan
7+
8+
includes:
9+
- vendor/phpstan/phpstan-phpunit/extension.neon
10+
- vendor/phpstan/phpstan-phpunit/rules.neon
11+
- vendor/phpstan/phpstan-strict-rules/rules.neon

phpunit.xml.dist

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
4+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
6+
beStrictAboutChangesToGlobalState="true"
7+
beStrictAboutOutputDuringTests="true"
8+
beStrictAboutTodoAnnotatedTests="true"
9+
colors="true"
10+
bootstrap="tests/bootstrap.php"
11+
>
12+
<testsuites>
13+
<testsuite name="Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist processUncoveredFilesFromWhitelist="true">
19+
<directory suffix=".php">src</directory>
20+
</whitelist>
21+
</filter>
22+
<logging>
23+
<log
24+
showOnlySummary="true"
25+
showUncoveredFiles="true"
26+
target="php://stdout"
27+
type="coverage-text"
28+
/>
29+
<log type="coverage-clover" target="temp/clover.xml"/>
30+
<log type="coverage-html" target="temp/coverage-html"/>
31+
</logging>
32+
</phpunit>

src/DefaultFieldResolver.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimPod\GraphQL\Utils;
6+
7+
use Closure;
8+
use GraphQL\Type\Definition\ResolveInfo;
9+
use Nette\Utils\Strings;
10+
use function is_array;
11+
use function is_object;
12+
use function method_exists;
13+
14+
class DefaultFieldResolver
15+
{
16+
private const METHOD_PREFIX_GET = 'get';
17+
private const METHOD_PREFIX_IS = 'is';
18+
private const METHOD_PREFIX_HAS = 'has';
19+
20+
/**
21+
* @param mixed $source
22+
* @param mixed[] $arguments
23+
* @param mixed $context
24+
*
25+
* @return mixed|null
26+
*/
27+
public static function resolve($source, array $arguments, $context, ResolveInfo $info)
28+
{
29+
$fieldName = $info->fieldName;
30+
$resolvedValue = null;
31+
32+
if (is_array($source) && isset($source[$fieldName])) {
33+
$resolvedValue = $source[$fieldName];
34+
}
35+
if (is_object($source)) {
36+
if (isset($source->$fieldName)) {
37+
$resolvedValue = $source->$fieldName;
38+
}
39+
foreach ([self::METHOD_PREFIX_GET, self::METHOD_PREFIX_IS, self::METHOD_PREFIX_HAS] as $getterPrefix) {
40+
$getterName = $getterPrefix . Strings::firstUpper($fieldName);
41+
if (! method_exists($source, $getterName)) {
42+
continue;
43+
}
44+
45+
$resolvedValue = $source->$getterName();
46+
}
47+
if (method_exists($source, $fieldName)) {
48+
$resolvedValue = $source->$fieldName();
49+
}
50+
}
51+
52+
return $resolvedValue instanceof Closure
53+
? $resolvedValue($source, $arguments, $context)
54+
: $resolvedValue;
55+
}
56+
}

0 commit comments

Comments
 (0)