Skip to content

Annotation Validation #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions src/AnnotationReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace Igni\Annotation;

class AnnotationReader
{
public function
}
14 changes: 10 additions & 4 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ final class Context
*/
private $imports = [];

/**
* @var string
*/
private $target;

/**
* @var string
*/
private $namespace;

public function __construct(
Expand Down Expand Up @@ -103,7 +109,7 @@ public static function fromReflectionClass(ReflectionClass $class) : self
$instance = new self(
Target::TARGET_CLASS,
$class->getNamespaceName(),
$class->getName()
$class->name
);
$imports = new ReflectorImports($class);
$instance->imports = $imports->getImports();
Expand All @@ -116,7 +122,7 @@ public static function fromReflectionMethod(ReflectionMethod $method) : self
$instance = new self(
Target::TARGET_METHOD,
$method->getDeclaringClass()->getNamespaceName(),
"{$method->getDeclaringClass()->getName()}::{$method->getName()}()"
"{$method->getDeclaringClass()->name}::{$method->name}()"
);
$imports = new ReflectorImports($method);
$instance->imports = $imports->getImports();
Expand All @@ -129,7 +135,7 @@ public static function fromReflectionProperty(ReflectionProperty $property) : se
$instance = new self(
Target::TARGET_PROPERTY,
$property->getDeclaringClass()->getNamespaceName(),
"{$property->getDeclaringClass()->getName()}::\${$property->getName()}"
"{$property->getDeclaringClass()->name}::\${$property->name}"
);
$imports = new ReflectorImports($property);
$instance->imports = $imports->getImports();
Expand All @@ -142,7 +148,7 @@ public static function fromReflectionFunction(ReflectionFunction $function) : se
$instance = new self(
Target::TARGET_FUNCTION,
$function->getNamespaceName(),
"{$function->getName()}()"
"{$function->name}()"
);
$imports = new ReflectorImports($function);
$instance->imports = $imports->getImports();
Expand Down
5 changes: 5 additions & 0 deletions src/Exception/MetaDataException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ public static function forUndefinedAttribute(MetaData $metaData, string $attribu
{
return new self("Annotation class {$metaData->getClass()} defines no attribute {$attribute}.");
}

public static function forUnresolvableFailedAttribute(MetaData $metaData) : self
{
return new self("Could not get last failed attribute from {$metaData->getClass()}");
}
}
20 changes: 15 additions & 5 deletions src/Exception/ParserException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,49 @@
namespace Igni\Annotation\Exception;

use Igni\Annotation\Context;
use Igni\Annotation\MetaData\Attribute;
use Igni\Annotation\Token;
use Igni\Exception\LogicException;
use Throwable;

final class ParserException extends LogicException implements AnnotationException
{
public static function forUnexpectedToken(Token $token, Context $context) : Throwable
public static function forUnexpectedToken(Token $token, Context $context) : self
{
$context = $context->getSymbol() ?: (string) $context;
$message = "Unexpected `{$token}` in {$context} at index: {$token->getPosition()}.";

return new self($message);
}

public static function forUnknownAnnotationClass(string $name, Context $context) : Throwable
public static function forUnknownAnnotationClass(string $name, Context $context) : self
{
$message = "Could not find annotation class {$name} used in {$context}." .
"Please check your composer settings, or use Parser::registerNamespace.";

return new self($message);
}

public static function forUsingNonAnnotationClassAsAnnotation(string $class, Context $context) : Throwable
public static function forUsingNonAnnotationClassAsAnnotation(string $class, Context $context) : self
{
$message = "Used {$class} as annotation - class is not marked as annotation. Used in {$context}." .
"Please add `@Annotation` annotation to mark class as annotation class.";

return new self($message);
}

public static function forUndefinedConstant(Context $context, string $name) : Throwable
public static function forUndefinedConstant(Context $context, string $name) : self
{
$message = "Using undefined constant `{$name}` in {$context}";
return new self($message);
}

public static function forInvalidAttributeValue(Context $context, string $annotationClass, Attribute $failedAttribute) : self
{
return new self("Failed to validate `{$failedAttribute->getName()}` attribute in @{$annotationClass} used in {$context}");
}

public static function forInvalidTarget(Context $context, string $target, string $annotationClass) : self
{
return new self("Invalid target `{$target}`` for annotation `@{$annotationClass}` used in {$context}");
}
}
5 changes: 5 additions & 0 deletions src/MetaData/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public function __construct(string $name, $type = 'mixed', bool $required = true
$this->required = $required;
}

public function getName() : string
{
return $this->name;
}

public function getType() : string
{
if (is_array($this->type)) {
Expand Down
59 changes: 56 additions & 3 deletions src/MetaData/MetaData.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,50 @@ final class MetaData
Enum::class => 1,
NoValidate::class => 1,
];

/**
* @var Parser
*/
private $parser;

/**
* @var Context
*/
private $context;
private $target = [Target::TARGET_ALL];

/**
* @var array
*/
private $validTargets = [Target::TARGET_ALL];

/**
* @var bool
*/
private $validate = true;

/**
* @var bool
*/
private $hasConstructor = false;

/**
* @var bool
*/
private $isAnnotation = true;

/**
* @var string
*/
private $className;

/**
* @var Attribute[]
*/
private $attributes = [];

/**
* @var Attribute|null
*/
private $lastFailedAttribute;

public function __construct(string $class, Parser $parser = null)
Expand Down Expand Up @@ -86,11 +119,17 @@ public function getAttribute(string $name) : Attribute

public function validateTarget(string $target) : bool
{
return in_array(Target::TARGET_ALL, $this->target) || in_array($target, $this->target);
return in_array(Target::TARGET_ALL, $this->validTargets) || in_array($target, $this->validTargets);
}

public function validateAttributes(array $data) : bool
{
if (!$this->validate) {
return true;
}

$this->lastFailedAttribute = null;

foreach ($this->attributes as $name => $attribute) {
if (!isset($data[$name])) {
if ($attribute->isRequired()) {
Expand All @@ -108,6 +147,20 @@ public function validateAttributes(array $data) : bool
return true;
}

public function hasFailedAttribute() : bool
{
return null !== $this->lastFailedAttribute;
}

public function getFailedAttribute() : Attribute
{
if (!$this->hasFailedAttribute()) {
throw MetaDataException::forUnresolvableFailedAttribute($this);
}

return $this->lastFailedAttribute;
}

private function collect(ReflectionClass $class) : void
{
$this->className = $class->getName();
Expand Down Expand Up @@ -138,7 +191,7 @@ private function collectClassMeta(ReflectionClass $class)
$this->context
);
}
$this->target = $annotation->value;
$this->validTargets = $annotation->value;
break;
case NoValidate::class:
$this->validate = false;
Expand Down
10 changes: 8 additions & 2 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,15 @@ private function parseAnnotation(Tokenizer $tokenizer, Context $context, $nested
}

$metaData = $this->getMetaData($annotationClass, $context);
if (!$metaData->hasConstructor()) {
if ($metaData->validateAttributes($arguments)) {

$target = $nested ? Target::TARGET_ANNOTATION : $context->getTarget();
if (!$metaData->validateTarget($target)) {
throw ParserException::forInvalidTarget($context, $target, $annotationClass);
}

if (!$metaData->hasConstructor()) {
if (!$metaData->validateAttributes($arguments)) {
throw ParserException::forInvalidAttributeValue($context, $annotationClass, $metaData->getFailedAttribute());
}
$annotation = new $annotationClass();
$valueArgs = [];
Expand Down
2 changes: 1 addition & 1 deletion tests/MetaDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function testValidateAttributes() : void
{
$meta = new MetaData(SimpleAnnotation::class);
self::assertTrue($meta->validateAttributes([]));
self::assertFalse($meta->validateAttributes(['attribute' => 1]));
self::assertTrue($meta->validateAttributes(['attribute' => 1]));
self::assertTrue($meta->validateAttributes(['attribute' => 'a']));
self::assertTrue($meta->validateAttributes(['attribute' => 'b']));
self::assertTrue($meta->validateAttributes(['attribute' => 'c']));
Expand Down