Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

Commit

Permalink
Updated cs-fixer rules
Browse files Browse the repository at this point in the history
  • Loading branch information
freost committed Nov 23, 2023
1 parent 8b95bb1 commit 20df17b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 54 deletions.
3 changes: 3 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'concat_space' => ['spacing' => 'one'],
'constant_case' => true,
'dir_constant' => true,
'curly_braces_position' => true,
'elseif' => true,
'encoding' => true,
'full_opening_tag' => true,
Expand Down Expand Up @@ -58,6 +59,7 @@
'object_operator_without_whitespace' => true,
'ordered_imports' => ['imports_order' => ['class', 'function', 'const']],
'ordered_traits' => true,
'ordered_types' => true,
'phpdoc_align' => true,
'phpdoc_annotation_without_dot' => true,
'phpdoc_indent' => true,
Expand All @@ -82,6 +84,7 @@
'single_line_after_imports' => true,
'single_line_comment_style' => ['comment_types' => ['hash']],
'single_quote' => true,
'single_space_around_construct' => true,
'space_after_semicolon' => true,
'standardize_not_equals' => true,
'switch_case_semicolon_to_colon' => true,
Expand Down
69 changes: 23 additions & 46 deletions src/DataClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,48 +41,38 @@ final public function __construct(...$props)
{
// Cache property details

if(array_key_exists(static::class, static::$__cache__) === false)
{
if (array_key_exists(static::class, static::$__cache__) === false) {
static::cachePropDetails();
}

// Check for missing required properties

if(!empty($missing = array_diff_key(static::$__cache__[static::class]['required_props'], $props)))
{
if (!empty($missing = array_diff_key(static::$__cache__[static::class]['required_props'], $props))) {
throw new RuntimeException(vsprintf('Missing required %s: %s.', [count($missing) > 1 ? 'properties' : 'property', implode(',', $missing)]));
}

// Initialize properties

foreach($props as $name => $value)
{
if($value !== null)
{
foreach ($props as $name => $value) {
if ($value !== null) {
$propInfo = static::$__cache__[static::class]['prop_info'][$name];

if($propInfo['dataclass'] !== null)
{
if($propInfo['is_array'])
{
if ($propInfo['dataclass'] !== null) {
if ($propInfo['is_array']) {
$dataclasses = [];

foreach($value as $valueData)
{
foreach ($value as $valueData) {
$dataclasses[] = new $propInfo['dataclass'](...$valueData);
}

$value = $dataclasses;
}
else
{
else {
$value = new $propInfo['dataclass'](...$value);
}
}
else
{
foreach($propInfo['validators'] as $validator)
{
else {
foreach ($propInfo['validators'] as $validator) {
$value = $this->{$validator}($value);
}
}
Expand All @@ -106,8 +96,7 @@ final protected static function cachePropDetails(): void

// Cache property details

foreach($reflection->getProperties(ReflectionProperty::IS_PUBLIC) as $prop)
{
foreach ($reflection->getProperties(ReflectionProperty::IS_PUBLIC) as $prop) {
$propName = $prop->getName();

static::$__cache__[static::class]['prop_info'][$propName] = [
Expand All @@ -118,37 +107,30 @@ final protected static function cachePropDetails(): void

// Cache required properties

if($prop->hasDefaultValue() === false)
{
if ($prop->hasDefaultValue() === false) {
static::$__cache__[static::class]['required_props'][$propName] = $propName;
}

// Cache property information

$type = $prop->getType();

if($type instanceof ReflectionNamedType)
{
if($type->isBuiltin())
{
if($type->getName() === 'array')
{
if ($type instanceof ReflectionNamedType) {
if ($type->isBuiltin()) {
if ($type->getName() === 'array') {
static::$__cache__[static::class]['prop_info'][$propName]['is_array'] = true;

$attributes = $prop->getAttributes(ArrayOf::class);

if(!empty($attributes))
{
if (!empty($attributes)) {
static::$__cache__[static::class]['prop_info'][$propName]['dataclass'] = $attributes[0]->newInstance()->getType();
}
}
}
else
{
else {
$typeName = $type->getName();

if(in_array(self::class, class_parents($typeName)))
{
if (in_array(self::class, class_parents($typeName))) {
static::$__cache__[static::class]['prop_info'][$propName]['dataclass'] = $typeName;
}
}
Expand All @@ -157,12 +139,10 @@ final protected static function cachePropDetails(): void

// Cache property validators

foreach($reflection->getMethods() as $method)
{
foreach ($reflection->getMethods() as $method) {
$attributes = $method->getAttributes(Validator::class);

foreach($attributes as $attribute)
{
foreach ($attributes as $attribute) {
$property = $attribute->newInstance()->getProperty();

static::$__cache__[static::class]['prop_info'][$property]['validators'][] = $method->getName();
Expand All @@ -181,17 +161,14 @@ final public function toArray(): array

$props = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);

foreach($props as $prop)
{
foreach ($props as $prop) {
$name = $prop->getName();

$array[$name] = $this->$name;
}

array_walk_recursive($array, function (&$value): void
{
if($value instanceof self)
{
array_walk_recursive($array, function (&$value): void {
if ($value instanceof self) {
$value = $value->toArray();
}
});
Expand Down
4 changes: 2 additions & 2 deletions src/attributes/ArrayOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ final class ArrayOf
*/
public function __construct(
protected string $type
)
{}
) {
}

/**
* Returns the type.
Expand Down
4 changes: 2 additions & 2 deletions src/attributes/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ final class Validator
*/
public function __construct(
protected string $property
)
{}
) {
}

/**
* Returns the validator target name.
Expand Down
3 changes: 1 addition & 2 deletions tests/classes/Avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ class Avatar extends DataClass
#[Validator('url')]
protected function validateEmail(string $url): string
{
if(str_starts_with($url, 'https://') === false)
{
if (str_starts_with($url, 'https://') === false) {
throw new ValueError("Url must start with 'https://'.");
}

Expand Down
3 changes: 1 addition & 2 deletions tests/classes/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ protected function validateName(string $name): string
#[Validator('email')]
protected function validateEmail(string $email): string
{
if(str_contains($email, '@') === false)
{
if (str_contains($email, '@') === false) {
throw new ValueError("Email must contain an '@'.");
}

Expand Down

0 comments on commit 20df17b

Please sign in to comment.