Skip to content

Commit

Permalink
Refactor errors section
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera committed May 26, 2021
1 parent ecb7398 commit 13cdd18
Show file tree
Hide file tree
Showing 138 changed files with 4,263 additions and 610 deletions.
1 change: 1 addition & 0 deletions bin/src/Validator/SpecGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function generate($jsonSpec, $rootNamespace, $destination)
$this->generateEntityClass('DescendantTagList', $fileManager);
$this->generateEntityClass('CssRuleset', $fileManager);
$this->generateEntityClass('DocRuleset', $fileManager);
$this->generateEntityClass('Error', $fileManager);
$this->generateEntityClass('Tag', $fileManager);
$this->generateEntityClass('TagWithExtensionSpec', $fileManager, 'interface');
$this->generateEntityClass('ExtensionSpec', $fileManager, 'trait');
Expand Down
4 changes: 4 additions & 0 deletions bin/src/Validator/SpecGenerator/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ public function filterValueStrings($value)
||
strpos($value, 'Element::') === 0
||
strpos($value, 'Error::') === 0
||
strpos($value, 'Error\\') === 0
||
strpos($value, 'ErrorCode::') === 0
||
strpos($value, 'Extension::') === 0
Expand Down
25 changes: 23 additions & 2 deletions bin/src/Validator/SpecGenerator/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function ensureDirectoriesExist()
"{$this->destination}/Spec/DocRuleset",
"{$this->destination}/Spec/DeclarationList",
"{$this->destination}/Spec/DescendantTagList",
"{$this->destination}/Spec/Error",
"{$this->destination}/Spec/Section",
"{$this->destination}/Spec/Tag",
];
Expand Down Expand Up @@ -305,6 +306,10 @@ private function getFullyQualifiedName($class)
return "AmpProject\\Validator\\Spec\\DocSpecRule";
}

if (strpos($class, 'Error\\') === 0) {
return "AmpProject\\Validator\\Spec\\Error";
}

if (strpos($class, 'AttributeList\\') === 0) {
return "AmpProject\\Validator\\Spec\\AttributeList";
}
Expand All @@ -320,7 +325,15 @@ private function getFullyQualifiedName($class)
if (
in_array(
$class,
['AttributeList', 'CssRuleset', 'DocRuleset', 'DeclarationList', 'DescendantTagList', 'SpecRule'],
[
'AttributeList',
'CssRuleset',
'DocRuleset',
'DeclarationList',
'DescendantTagList',
'Error',
'SpecRule'
],
true
)
) {
Expand All @@ -330,7 +343,15 @@ private function getFullyQualifiedName($class)
if (
in_array(
$class,
['AttributeLists', 'CssRulesets', 'DocRulesets', 'DeclarationLists', 'DescendantTagLists', 'Tags'],
[
'AttributeLists',
'CssRulesets',
'DocRulesets',
'DeclarationLists',
'DescendantTagLists',
'Errors',
'Tags'
],
true
)
) {
Expand Down
16 changes: 0 additions & 16 deletions bin/src/Validator/SpecGenerator/Section/AttributeLists.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use AmpProject\Tooling\Validator\SpecGenerator\ClassNames;
use AmpProject\Tooling\Validator\SpecGenerator\ConstantNames;
use AmpProject\Tooling\Validator\SpecGenerator\Dumper;
use AmpProject\Tooling\Validator\SpecGenerator\FileManager;
use AmpProject\Tooling\Validator\SpecGenerator\Section;
use AmpProject\Tooling\Validator\SpecGenerator\Template;
Expand All @@ -16,21 +15,6 @@ final class AttributeLists implements Section
use ClassNames;
use ConstantNames;

/**
* Dumper instance to use.
*
* @var Dumper
*/
private $dumper;

/**
* AttributeLists constructor.
*/
public function __construct()
{
$this->dumper = new Dumper();
}

/**
* Process a section.
*
Expand Down
18 changes: 0 additions & 18 deletions bin/src/Validator/SpecGenerator/Section/CssRulesets.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace AmpProject\Tooling\Validator\SpecGenerator\Section;

use AmpProject\Tooling\Validator\SpecGenerator\ArrayKeyFirstPolyfill;
use AmpProject\Tooling\Validator\SpecGenerator\ClassNames;
use AmpProject\Tooling\Validator\SpecGenerator\ConstantNames;
use AmpProject\Tooling\Validator\SpecGenerator\Dumper;
use AmpProject\Tooling\Validator\SpecGenerator\FileManager;
use AmpProject\Tooling\Validator\SpecGenerator\Section;
use AmpProject\Tooling\Validator\SpecGenerator\Template;
Expand All @@ -14,25 +12,9 @@

final class CssRulesets implements Section
{
use ArrayKeyFirstPolyfill;
use ClassNames;
use ConstantNames;

/**
* Dumper instance to use.
*
* @var Dumper
*/
private $dumper;

/**
* CssRulesets constructor.
*/
public function __construct()
{
$this->dumper = new Dumper();
}

/**
* Process a section.
*
Expand Down
16 changes: 0 additions & 16 deletions bin/src/Validator/SpecGenerator/Section/DeclarationLists.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use AmpProject\Tooling\Validator\SpecGenerator\ClassNames;
use AmpProject\Tooling\Validator\SpecGenerator\ConstantNames;
use AmpProject\Tooling\Validator\SpecGenerator\Dumper;
use AmpProject\Tooling\Validator\SpecGenerator\FileManager;
use AmpProject\Tooling\Validator\SpecGenerator\Section;
use AmpProject\Tooling\Validator\SpecGenerator\Template;
Expand All @@ -16,21 +15,6 @@ final class DeclarationLists implements Section
use ClassNames;
use ConstantNames;

/**
* Dumper instance to use.
*
* @var Dumper
*/
private $dumper;

/**
* DeclarationLists constructor.
*/
public function __construct()
{
$this->dumper = new Dumper();
}

/**
* Process a section.
*
Expand Down
16 changes: 0 additions & 16 deletions bin/src/Validator/SpecGenerator/Section/DescendantTagLists.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use AmpProject\Tooling\Validator\SpecGenerator\ClassNames;
use AmpProject\Tooling\Validator\SpecGenerator\ConstantNames;
use AmpProject\Tooling\Validator\SpecGenerator\Dumper;
use AmpProject\Tooling\Validator\SpecGenerator\FileManager;
use AmpProject\Tooling\Validator\SpecGenerator\Section;
use AmpProject\Tooling\Validator\SpecGenerator\Template;
Expand All @@ -16,21 +15,6 @@ final class DescendantTagLists implements Section
use ClassNames;
use ConstantNames;

/**
* Dumper instance to use.
*
* @var Dumper
*/
private $dumper;

/**
* DescendantTagLists constructor.
*/
public function __construct()
{
$this->dumper = new Dumper();
}

/**
* Process a section.
*
Expand Down
18 changes: 0 additions & 18 deletions bin/src/Validator/SpecGenerator/Section/DocRulesets.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace AmpProject\Tooling\Validator\SpecGenerator\Section;

use AmpProject\Tooling\Validator\SpecGenerator\ArrayKeyFirstPolyfill;
use AmpProject\Tooling\Validator\SpecGenerator\ClassNames;
use AmpProject\Tooling\Validator\SpecGenerator\ConstantNames;
use AmpProject\Tooling\Validator\SpecGenerator\Dumper;
use AmpProject\Tooling\Validator\SpecGenerator\FileManager;
use AmpProject\Tooling\Validator\SpecGenerator\Section;
use AmpProject\Tooling\Validator\SpecGenerator\Template;
Expand All @@ -14,25 +12,9 @@

final class DocRulesets implements Section
{
use ArrayKeyFirstPolyfill;
use ClassNames;
use ConstantNames;

/**
* Dumper instance to use.
*
* @var Dumper
*/
private $dumper;

/**
* DocRulesets constructor.
*/
public function __construct()
{
$this->dumper = new Dumper();
}

/**
* Process a section.
*
Expand Down
88 changes: 57 additions & 31 deletions bin/src/Validator/SpecGenerator/Section/Errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,19 @@

namespace AmpProject\Tooling\Validator\SpecGenerator\Section;

use AmpProject\Tooling\Validator\SpecGenerator\ClassNames;
use AmpProject\Tooling\Validator\SpecGenerator\ConstantNames;
use AmpProject\Tooling\Validator\SpecGenerator\Dumper;
use AmpProject\Tooling\Validator\SpecGenerator\FileManager;
use AmpProject\Tooling\Validator\SpecGenerator\Section;
use AmpProject\Tooling\Validator\SpecGenerator\Template;
use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\PhpNamespace;

final class Errors implements Section
{
use ClassNames;
use ConstantNames;

/**
* Dumper instance to use.
*
* @var Dumper
*/
private $dumper;

/**
* Errors constructor.
*/
public function __construct()
{
$this->dumper = new Dumper();
}

/**
* Process a section.
*
Expand All @@ -39,29 +26,68 @@ public function __construct()
*/
public function process(FileManager $fileManager, $spec, PhpNamespace $namespace, ClassType $class)
{
$propertyName = lcfirst($class->getName());
$namespace->addUse('AmpProject\Exception\InvalidListName');
$namespace->addUse("{$fileManager->getRootNamespace()}\\Spec");
$namespace->addUse("{$fileManager->getRootNamespace()}\\Spec\\Error");
$namespace->addUse("{$fileManager->getRootNamespace()}\\Spec\\IterableSection");
$namespace->addUse("{$fileManager->getRootNamespace()}\\Spec\\Iteration");

$namespace->addUse('AmpProject\\Validator\\ErrorCode');
$class->addImplement("{$fileManager->getRootNamespace()}\\Spec\\IterableSection");
$class->addTrait(
"{$fileManager->getRootNamespace()}\\Spec\\Iteration",
['Iteration::current as parentCurrent']
);

$class->addProperty($propertyName)
->addComment('@var array');

$errorData = [];
$class->addProperty('errors')
->setValue([])
->setPrivate()
->addComment("Cache of instantiated Error objects.\n\n@var array<Spec\\Error>");

$errors = [];
foreach ($spec as $key => $value) {
$key = $this->getErrorCodeConstant($this->getConstantName($key));
$errorData[$key] = $value;
$className = $this->generateErrorSpecificClass($key, $value, $fileManager);

$errors["Error\\{$className}::CODE"] = "Error\\{$className}::class";
}
$class->addConstant('ERRORS', $errors)
->addComment("Mapping of error code to error implementation.\n\n@var array<string>");

ksort($errorData);
$errorsTemplateClass = ClassType::withBodiesFrom(Template\Errors::class);
foreach ($errorsTemplateClass->getMethods() as $method) {
$class->addMember($method);
}
}

$constructor = $class->addMethod('__construct');
$constructor->addBody('$this->? = [', [$propertyName]);
/**
* Generate the Error-specific class file.
*
* @param string $errorCode Code of the error to generate the class for.
* @param array $jsonSpec Array of spec data for the error.
* @param FileManager $fileManager File manager instance to use.
* @return string Short name of the class that was generated.
*/
private function generateErrorSpecificClass($errorCode, $jsonSpec, FileManager $fileManager)
{
list($file, $namespace) = $fileManager->createNewNamespacedFile('Spec\\Error');

foreach ($errorData as $key => $value) {
$constructor->addBody(" {$this->dumper->dumpWithKey($key, $value, 1)},");
}
$className = $this->getClassNameFromId($errorCode);

$namespace->addUse("{$fileManager->getRootNamespace()}\\Spec\\SpecRule");
$namespace->addUse("{$fileManager->getRootNamespace()}\\Spec\\Error");

/** @var ClassType $class */
$class = $namespace->addClass($className)
->setFinal()
->addExtend('AmpProject\Validator\Spec\Error');

$class->addConstant('CODE', $errorCode)
->addComment("Code of the error.\n\n@var string");

$class->addConstant('SPEC', $jsonSpec)
->addComment("Array of spec data.\n\n@var array<array>");

$fileManager->saveFile($file, "Spec/Error/{$className}.php");

$constructor->addBody('];');
return $className;
}
}
18 changes: 0 additions & 18 deletions bin/src/Validator/SpecGenerator/Section/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace AmpProject\Tooling\Validator\SpecGenerator\Section;

use AmpProject\Tooling\Validator\SpecGenerator\ArrayKeyFirstPolyfill;
use AmpProject\Tooling\Validator\SpecGenerator\ClassNames;
use AmpProject\Tooling\Validator\SpecGenerator\ConstantNames;
use AmpProject\Tooling\Validator\SpecGenerator\Dumper;
use AmpProject\Tooling\Validator\SpecGenerator\FileManager;
use AmpProject\Tooling\Validator\SpecGenerator\Section;
use AmpProject\Tooling\Validator\SpecGenerator\Template;
Expand All @@ -14,25 +12,9 @@

final class Tags implements Section
{
use ArrayKeyFirstPolyfill;
use ClassNames;
use ConstantNames;

/**
* Dumper instance to use.
*
* @var Dumper
*/
private $dumper;

/**
* Tags constructor.
*/
public function __construct()
{
$this->dumper = new Dumper();
}

/**
* Process a section.
*
Expand Down
Loading

0 comments on commit 13cdd18

Please sign in to comment.