Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/generator/.php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ return PhpCsFixer\Config::create()
->setFinder($finder)
->setRules([
'@PSR2' => true,
'@PHP71Migration' => true,
'@PHP71Migration:risky' => true,
'single_blank_line_before_namespace' => true,
'ordered_imports' => true,
'concat_space' => ['spacing' => 'none'],
Expand All @@ -38,7 +40,6 @@ return PhpCsFixer\Config::create()
'general_phpdoc_annotation_remove' => ['author', 'category', 'copyright', 'created', 'license', 'package', 'since', 'subpackage', 'version'],
'native_function_invocation' => true,
'fully_qualified_strict_types' => true,
'native_constant_invocation' => true,
'header_comment' => ['header' => $header],
])
;
2 changes: 1 addition & 1 deletion lib/generator/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"bin-dir": "bin"
},
"require": {
"php": ">=5.6",
"php": ">=7.1",
"webonyx/graphql-php": "^0.11.2|^0.12.0"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion lib/generator/src/ClassUtils.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

/*
* This file is part of the OverblogGraphQLPhpGenerator package.
Expand Down
85 changes: 42 additions & 43 deletions lib/generator/src/Generator/AbstractClassGenerator.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

/*
* This file is part of the OverblogGraphQLPhpGenerator package.
Expand All @@ -12,12 +12,15 @@
namespace Overblog\GraphQLGenerator\Generator;

use Overblog\GraphQLGenerator\ClassUtils;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

abstract class AbstractClassGenerator
{
const SKELETON_FILE_PREFIX = '.php.skeleton';
public const MODE_DRY_RUN = 1;
public const MODE_MAPPING_ONLY = 2;
public const MODE_WRITE = 4;
public const MODE_OVERRIDE = 8;

protected const SKELETON_FILE_PREFIX = '.php.skeleton';

/**
* The namespace that contains all classes.
Expand Down Expand Up @@ -66,18 +69,14 @@ public function getClassNamespace()
return $this->classNamespace;
}

public function setClassNamespace($classNamespace)
public function setClassNamespace($classNamespace): self
{
$this->classNamespace = ClassUtils::cleanClasseName($classNamespace);

return $this;
}

/**
* @param string[]|string $skeletonDirs
* @return $this
*/
public function setSkeletonDirs($skeletonDirs)
public function setSkeletonDirs($skeletonDirs): self
{
$this->skeletonDirs = [];

Expand All @@ -98,7 +97,7 @@ public function setSkeletonDirs($skeletonDirs)
return $this;
}

public function getSkeletonDirs($withDefault = true)
public function getSkeletonDirs(bool $withDefault = true): array
{
$skeletonDirs = $this->skeletonDirs ;

Expand All @@ -109,9 +108,9 @@ public function getSkeletonDirs($withDefault = true)
return $skeletonDirs;
}

public function addSkeletonDir($skeletonDir)
public function addSkeletonDir($skeletonDir): self
{
if (!\is_string($skeletonDir) && !\is_object($skeletonDir) && !\is_callable($skeletonDir, '__toString')) {
if (!\is_string($skeletonDir) && !\is_object($skeletonDir) && !\is_callable([$skeletonDir, '__toString'])) {
throw new \InvalidArgumentException(
\sprintf('Skeleton dir must be string or object implementing __toString, "%s" given.', \gettype($skeletonDir))
);
Expand All @@ -135,15 +134,15 @@ public function addSkeletonDir($skeletonDir)
*
* @return self
*/
public function setNumSpaces($numSpaces)
public function setNumSpaces(int $numSpaces): self
{
$this->spaces = \str_repeat(' ', $numSpaces);
$this->numSpaces = $numSpaces;

return $this;
}

public function addTrait($trait)
public function addTrait(string $trait): self
{
$cleanTrait = $this->shortenClassName($trait, false);
if (!\in_array($cleanTrait, $this->traits)) {
Expand All @@ -153,14 +152,14 @@ public function addTrait($trait)
return $this;
}

public function clearTraits()
public function clearTraits(): self
{
$this->traits = [];

return $this;
}

public function addImplement($implement)
public function addImplement(string $implement): self
{
$cleanImplement = $this->shortenClassName($implement, false);
if (!\in_array($cleanImplement, $this->implements)) {
Expand All @@ -170,14 +169,14 @@ public function addImplement($implement)
return $this;
}

public function clearImplements()
public function clearImplements(): self
{
$this->implements = [];

return $this;
}

public function addUseStatement($useStatement)
public function addUseStatement(string $useStatement): self
{
$cleanUse = ClassUtils::cleanClasseName($useStatement);
if (!\in_array($cleanUse, $this->useStatements)) {
Expand All @@ -187,14 +186,14 @@ public function addUseStatement($useStatement)
return $this;
}

public function clearUseStatements()
public function clearUseStatements(): self
{
$this->useStatements = [];

return $this;
}

public function getSkeletonContent($skeleton, $withDefault = true)
public function getSkeletonContent(string $skeleton, bool $withDefault = true)
{
$skeletonDirs = $this->getSkeletonDirs($withDefault);

Expand Down Expand Up @@ -223,22 +222,22 @@ public function getSkeletonContent($skeleton, $withDefault = true)
);
}

protected function addInternalUseStatement($use)
protected function addInternalUseStatement(string $use): void
{
$cleanUse = ClassUtils::cleanClasseName($use);
if (!\in_array($cleanUse, $this->internalUseStatements)) {
$this->internalUseStatements[] = $cleanUse;
}
}

protected function clearInternalUseStatements()
protected function clearInternalUseStatements(): self
{
$this->internalUseStatements = [];

return $this;
}

protected function shortenClassName($definition, $isInternal = true)
protected function shortenClassName(string $definition, bool $isInternal = true): string
{
$shortName = ClassUtils::shortenClassName($definition);

Expand All @@ -252,7 +251,7 @@ protected function shortenClassName($definition, $isInternal = true)
return $shortName;
}

protected function shortenClassFromCode($code)
protected function shortenClassFromCode(?string $code): string
{
$codeParsed = ClassUtils::shortenClassFromCode(
$code,
Expand All @@ -264,7 +263,7 @@ function ($matches) {
return $codeParsed;
}

protected function processPlaceHoldersReplacements(array $placeHolders, $content, array $values)
protected function processPlaceHoldersReplacements(array $placeHolders, string $content, array $values): string
{
$replacements = [];

Expand All @@ -288,7 +287,7 @@ protected function processPlaceHoldersReplacements(array $placeHolders, $content
return \strtr($content, $replacements);
}

protected function processTemplatePlaceHoldersReplacements($template, array $values, array $skip = [])
protected function processTemplatePlaceHoldersReplacements(string $template, array $values, array $skip = []): string
{
$code = $this->getSkeletonContent($template);
$placeHolders = $this->getPlaceHolders($code);
Expand All @@ -297,11 +296,11 @@ protected function processTemplatePlaceHoldersReplacements($template, array $val
return $code;
}

protected function getPlaceHolders($content)
protected function getPlaceHolders(string $content): array
{
\preg_match_all('@<([\w]+)>@i', $content, $placeHolders);

return isset($placeHolders[1]) ? $placeHolders[1] : [];
return $placeHolders[1] ?? [];
}

/**
Expand All @@ -310,7 +309,7 @@ protected function getPlaceHolders($content)
*
* @return string
*/
protected function prefixCodeWithSpaces($code, $num = 1)
protected function prefixCodeWithSpaces(string $code, int $num = 1): string
{
$lines = \explode("\n", $code);

Expand All @@ -323,17 +322,17 @@ protected function prefixCodeWithSpaces($code, $num = 1)
return \implode("\n", $lines);
}

protected function generateSpaces()
protected function generateSpaces(): string
{
return $this->spaces;
}

protected function generateNamespace()
protected function generateNamespace(): ?string
{
return null !== $this->classNamespace ? 'namespace '.$this->classNamespace.';' : null;
}

protected function generateUseStatement(array $config)
protected function generateUseStatement(array $config): string
{
$statements = \array_merge($this->internalUseStatements, $this->useStatements);
\sort($statements);
Expand All @@ -343,24 +342,24 @@ protected function generateUseStatement(array $config)
return $useStatements;
}

protected function generateClassType()
protected function generateClassType(): string
{
return 'final ';
}

protected function generateImplements()
protected function generateImplements(): ?string
{
return \count($this->implements) ? ' implements '.\implode(', ', $this->implements) : null;
}

protected function generateTraits()
protected function generateTraits(): ?string
{
$traits = $this->tokenizeUseStatements($this->traits, '<spaces>');

return $traits ? $traits."\n" : $traits;
}

protected function tokenizeUseStatements(array $useStatements, $prefix = '')
protected function tokenizeUseStatements(array $useStatements, $prefix = ''): ?string
{
if (empty($useStatements)) {
return null;
Expand All @@ -380,20 +379,20 @@ protected function tokenizeUseStatements(array $useStatements, $prefix = '')
*
* @param array $configs raw configs
* @param string $outputDirectory
* @param int|bool $mode
* @param int $mode
*
* @return array classes map [[FQCLN => classPath], [FQCLN => classPath], ...]
*/
abstract public function generateClasses(array $configs, $outputDirectory, $mode = false);
abstract public function generateClasses(array $configs, ?string $outputDirectory, int $mode = self::MODE_WRITE): array;

/**
* Generates a class file.
*
* @param array $config
* @param $outputDirectory
* @param bool $mode
* @param array $config
* @param string $outputDirectory
* @param int $mode
*
* @return array classes map [FQCLN => classPath]
*/
abstract public function generateClass(array $config, $outputDirectory, $mode = false);
abstract public function generateClass(array $config, ?string $outputDirectory, int $mode = self::MODE_WRITE): array;
}
Loading