Skip to content

Commit

Permalink
Big 💥
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Aug 3, 2018
0 parents commit 273ca8f
Show file tree
Hide file tree
Showing 17 changed files with 568 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/vendor/
/composer.lock
/.phpcs-cache
/phpcs.xml
/phpstan.neon
/phpunit.xml
32 changes: 32 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
build:
nodes:
analysis:
environment:
php:
version: 7.1
cache:
disabled: false
directories:
- ~/.composer/cache
project_setup:
override: true
tests:
override:
- php-scrutinizer-run
- phpcs-run
dependencies:
override:
- composer install --no-interaction --prefer-dist

checks:
php:
code_rating: true

tools:
external_code_coverage: true

build_failure_conditions:
- 'elements.rating(<= C).new.exists' # No new classes/methods with a rating of C or worse allowed
- 'issues.severity(>= MAJOR).new.exists' # New issues of major or higher severity
- 'project.metric_change("scrutinizer.test_coverage", < 0)' # Code Coverage decreased from previous inspection
- 'patches.label("Unused Use Statements").new.exists' # No new unused imports patches allowed
44 changes: 44 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
dist: trusty
language: php
sudo: false

cache:
directories:
- $HOME/.composer/cache

php:
- 7.1
- 7.2
- nightly

before_install:
- composer self-update
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{,.disabled} || echo "xdebug not available"

install:
- travis_retry composer update --prefer-dist

script: ./vendor/bin/phpunit

jobs:
include:
- stage: Test
env: COVERAGE
php: 7.1
before_script:
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{.disabled,}
- if [[ ! $(php -m | grep -si xdebug) ]]; then echo "xdebug required for coverage"; exit 1; fi
script:
- ./vendor/bin/phpunit --coverage-clover ./build/logs/clover.xml
after_script:
- wget https://github.com/scrutinizer-ci/ocular/releases/download/1.5.2/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml

- stage: Coding Standard
php: 7.1
script: ./vendor/bin/phpcs

- stage: Static Analysis
php: 7.1
script: ./vendor/bin/phpstan analyse

19 changes: 19 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2018 Šimon Podlipský <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# PHP GraphQL Utils for graphql-php

[![Build Status](https://travis-ci.org/simPod/GraphQL-Utils.svg)](https://travis-ci.org/simPod/GraphQL-Utils)
[![Downloads](https://poser.pugx.org/simpod/graphql-utils/d/total.svg)](https://packagist.org/packages/simpod/graphql-utils)
[![Packagist](https://poser.pugx.org/simpod/graphql-utils/v/stable.svg)](https://packagist.org/packages/simpod/graphql-utils)
[![Licence](https://poser.pugx.org/simpod/graphql-utils/license.svg)](https://packagist.org/packages/simpod/graphql-utils)
[![Quality Score](https://scrutinizer-ci.com/g/simPod/GraphQL-Utils/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/simPod/GraphQL-Utils)
[![Code Coverage](https://scrutinizer-ci.com/g/simPod/GraphQL-Utils/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/simPod/GraphQL-Utils)
[![GitHub Issues](https://img.shields.io/github/issues/simPod/GraphQL-Utils.svg?style=flat-square)](https://github.com/simPod/GraphQL-Utils/issues)


## Installation

Add as [Composer](https://getcomposer.org/) dependency:

```sh
composer require simpod/graphql-utils
```
36 changes: 36 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "simpod/graphql-utils",
"description": "GraphQL Utils for graphql-php",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Simon Podlipsky",
"email": "[email protected]"
}
],
"config": {
"sort-packages": true
},
"require": {
"php": "^7.1",
"webonyx/graphql-php": "^0.11"
},
"require-dev": {
"doctrine/coding-standard": "^4.0",
"phpstan/phpstan": "^0.10",
"phpstan/phpstan-phpunit": "^0.10",
"phpstan/phpstan-strict-rules": "^0.10",
"phpunit/phpunit": "^7.0"
},
"autoload": {
"psr-4": {
"SimPod\\GraphQL\\Utils\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"SimPod\\GraphQL\\Utils\\Tests\\": "tests/"
}
}
}
15 changes: 15 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<ruleset>
<arg name="basepath" value="."/>
<arg name="extensions" value="php"/>
<arg name="parallel" value="80"/>
<arg name="cache" value=".phpcs-cache"/>
<arg name="colors"/>

<!-- Ignore warnings, show progress of the run and show sniff names -->
<arg value="nps"/>

<file>src</file>

<rule ref="Doctrine"/>
</ruleset>
11 changes: 11 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
parameters:
level: max
paths:
- %currentWorkingDirectory%/src
- %currentWorkingDirectory%/tests
tmpDir: %currentWorkingDirectory%/var/phpstan

includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
- vendor/phpstan/phpstan-strict-rules/rules.neon
32 changes: 32 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
beStrictAboutChangesToGlobalState="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
colors="true"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<logging>
<log
showOnlySummary="true"
showUncoveredFiles="true"
target="php://stdout"
type="coverage-text"
/>
<log type="coverage-clover" target="temp/clover.xml"/>
<log type="coverage-html" target="temp/coverage-html"/>
</logging>
</phpunit>
56 changes: 56 additions & 0 deletions src/DefaultFieldResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace SimPod\GraphQL\Utils;

use Closure;
use GraphQL\Type\Definition\ResolveInfo;
use Nette\Utils\Strings;
use function is_array;
use function is_object;
use function method_exists;

class DefaultFieldResolver
{
private const METHOD_PREFIX_GET = 'get';
private const METHOD_PREFIX_IS = 'is';
private const METHOD_PREFIX_HAS = 'has';

/**
* @param mixed $source
* @param mixed[] $arguments
* @param mixed $context
*
* @return mixed|null
*/
public static function resolve($source, array $arguments, $context, ResolveInfo $info)
{
$fieldName = $info->fieldName;
$resolvedValue = null;

if (is_array($source) && isset($source[$fieldName])) {
$resolvedValue = $source[$fieldName];
}
if (is_object($source)) {
if (isset($source->$fieldName)) {
$resolvedValue = $source->$fieldName;
}
foreach ([self::METHOD_PREFIX_GET, self::METHOD_PREFIX_IS, self::METHOD_PREFIX_HAS] as $getterPrefix) {
$getterName = $getterPrefix . Strings::firstUpper($fieldName);
if (! method_exists($source, $getterName)) {
continue;
}

$resolvedValue = $source->$getterName();
}
if (method_exists($source, $fieldName)) {
$resolvedValue = $source->$fieldName();
}
}

return $resolvedValue instanceof Closure
? $resolvedValue($source, $arguments, $context)
: $resolvedValue;
}
}
41 changes: 41 additions & 0 deletions src/EnumBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace SimPod\GraphQL\Utils;

use LogicException;
use function preg_match;

class EnumBuilder extends TypeBuilder
{
/** @var mixed[][] */
private $values = [];

public static function createFromName(string $name) : self
{
return new static($name);
}

public function addValue(string $value) : self
{
if (preg_match(self::VALID_NAME_PATTERN, $value) !== 1) {
throw new LogicException();
}

$this->values[$value] = ['value' => $value];

return $this;
}

/**
* @return mixed[]
*/
public function build() : array
{
$parameters = parent::build();
$parameters['values'] = $this->values;

return $parameters;
}
}
64 changes: 64 additions & 0 deletions src/FieldBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace SimPod\GraphQL\Utils;

use GraphQL\Type\Definition\Type;

class FieldBuilder
{
/** @var mixed[] */
private $parameters;

private function __construct(string $name, Type $type)
{
$this->parameters['name'] = $name;
$this->parameters['type'] = $type;
}

public static function create(string $name, Type $type) : self
{
return new self($name, $type);
}

public function setDescription(string $description) : self
{
$this->parameters['description'] = $description;

return $this;
}

public function addArgument(string $name, Type $type, ?string $description = null) : self
{
$this->parameters['args'][$name] = ['type' => $type];

if ($description !== null) {
$this->parameters['args'][$name]['description'] = $description;
}

return $this;
}

public function setResolver(callable $callback) : self
{
$this->parameters['resolve'] = $callback;

return $this;
}

public function setDeprecationReason(string $reason) : self
{
$this->parameters['deprecationReason'] = $reason;

return $this;
}

/**
* @return mixed[]
*/
public function build() : array
{
return $this->parameters;
}
}
Loading

0 comments on commit 273ca8f

Please sign in to comment.