Skip to content

Commit 40a63de

Browse files
committed
Upd doc
1 parent b3bb957 commit 40a63de

18 files changed

+126
-110
lines changed

bin/plain-to-class-clear

+3-8
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33

44
function deleteDir($dirPath)
55
{
6-
if (!is_dir($dirPath)) {
7-
throw new InvalidArgumentException("$dirPath must be a directory");
8-
}
9-
if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
6+
if (!str_ends_with($dirPath, '/')) {
107
$dirPath .= '/';
118
}
129
$files = glob($dirPath . '*', GLOB_MARK);
@@ -24,9 +21,7 @@ function deleteDir($dirPath)
2421

2522
if (file_exists(__DIR__ . '/../.cache')) {
2623
deleteDir(__DIR__ . '/../.cache');
27-
} else {
28-
if (file_exists(__DIR__ . '/../vendor/yzen.dev/plain-to-class/.cache')) {
29-
deleteDir(__DIR__ . '/../vendor/yzen.dev/plain-to-class/.cache');
30-
}
24+
} elseif (file_exists(__DIR__ . '/../vendor/yzen.dev/plain-to-class/.cache')) {
25+
deleteDir(__DIR__ . '/../vendor/yzen.dev/plain-to-class/.cache');
3126
}
3227
exit(0);

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "yzen.dev/plain-to-class",
3-
"version": "2.0",
3+
"version": "3.0",
44
"description": "Class-transformer to transform your dataset into a structured object",
55
"minimum-stability": "dev",
66
"prefer-stable": true,

composer.lock

+9-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CacheGenerator/CacheGenerator.php

+27-22
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44

55
namespace ClassTransformer\CacheGenerator;
66

7-
use ClassTransformer\Attributes\FieldAlias;
8-
use ClassTransformer\Attributes\WritingStyle;
97
use ReflectionClass;
108
use RuntimeException;
11-
use ReflectionException;
12-
use ReflectionNamedType;
139
use ClassTransformer\HydratorConfig;
10+
use ClassTransformer\Attributes\FieldAlias;
1411
use ClassTransformer\Reflection\CacheReflectionClass;
1512
use ClassTransformer\Exceptions\ClassNotFoundException;
1613
use ClassTransformer\Reflection\CacheReflectionProperty;
@@ -28,15 +25,22 @@
2825
*
2926
* @template TClass
3027
*/
31-
class CacheGenerator
28+
final class CacheGenerator
3229
{
30+
/**
31+
*
32+
*/
3333
private const DIR_PERMISSION = 0777;
3434

35+
/**
36+
* @var HydratorConfig
37+
*/
3538
private HydratorConfig $config;
3639

3740
/** @psalm-param class-string<TClass> $class */
3841
private string $class;
39-
private string $cacheFile;
42+
43+
/** @var string Path to cache file */
4044
private string $path;
4145

4246
/**
@@ -46,12 +50,14 @@ public function __construct(string $class, HydratorConfig $config = null)
4650
{
4751
$this->config = $config ?? new HydratorConfig();
4852
$this->class = $class;
49-
$this->cacheFile = str_replace('\\', '_', $this->class);
50-
$this->path = $this->config->cachePath . DIRECTORY_SEPARATOR . $this->cacheFile . '.cache.php';
53+
$cacheFile = str_replace('\\', '_', $this->class);
54+
$this->path = $this->config->cachePath . DIRECTORY_SEPARATOR . $cacheFile . '.cache.php';
5155
}
5256

5357
/**
54-
* @param class-string<TClass> $class
58+
* @template T
59+
*
60+
* @param class-string<T> $class
5561
*/
5662
public static function create(string $class, HydratorConfig $config = null): CacheGenerator
5763
{
@@ -60,11 +66,8 @@ public static function create(string $class, HydratorConfig $config = null): Cac
6066

6167

6268
/**
63-
* @param string $class
64-
*
6569
* @return CacheReflectionClass
66-
* @throws ReflectionException
67-
* @throws ClassNotFoundException
70+
* @throws ClassNotFoundException|RuntimeException
6871
*/
6972
public function getClass(): CacheReflectionClass
7073
{
@@ -79,12 +82,16 @@ public function getClass(): CacheReflectionClass
7982

8083
/**
8184
* @return array{properties: array<CacheReflectionProperty>}
82-
* @throws ReflectionException|RuntimeException
85+
* @throws ClassNotFoundException|RuntimeException
8386
*/
8487
public function generate(): array
8588
{
8689
$this->makeCacheDir();
8790

91+
if (!class_exists($this->class)) {
92+
throw new ClassNotFoundException("Class $this->class not found. Please check the class path you specified.");
93+
}
94+
8895
$refInstance = new ReflectionClass($this->class);
8996

9097
$properties = $refInstance->getProperties();
@@ -94,6 +101,7 @@ public function generate(): array
94101
];
95102

96103
file_put_contents($this->path, serialize($cache));
104+
97105
return $cache;
98106
}
99107

@@ -119,9 +127,11 @@ private function convertToCacheProperty(RuntimeReflectionProperty $property): Ca
119127
}
120128

121129
/**
122-
* @return array<string>
130+
* @param array $args
131+
*
132+
* @return array<array-key,string>
123133
*/
124-
public function getAliases($args): array
134+
public function getAliases(array $args = []): array
125135
{
126136
$aliases = $args[FieldAlias::class] ?? null;
127137

@@ -169,18 +179,13 @@ public function cacheExists(): bool
169179
}
170180

171181
/**
172-
* @param string|null $path
173-
*
174182
* @return void
175183
* @throws RuntimeException
176184
*/
177185
private function makeCacheDir(): void
178186
{
179187
if (
180-
(
181-
!file_exists($this->config->cachePath) &&
182-
!mkdir($this->config->cachePath, self::DIR_PERMISSION, true)
183-
)
188+
(!file_exists($this->config->cachePath) && !mkdir($concurrentDirectory = $this->config->cachePath, self::DIR_PERMISSION, true) && !is_dir($concurrentDirectory))
184189
||
185190
!is_dir($this->config->cachePath)
186191
) {

src/ClassTransformer.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace ClassTransformer;
66

77
use ClassTransformer\Exceptions\ClassNotFoundException;
8+
use RuntimeException;
89

910
/**
1011
* Class ClassTransformer
@@ -23,7 +24,7 @@ final class ClassTransformer
2324
* @param iterable<mixed>|object ...$args
2425
*
2526
* @return null|T
26-
* @throws ClassNotFoundException
27+
* @throws ClassNotFoundException|RuntimeException
2728
*/
2829
public static function transform(string $className, ...$args): mixed
2930
{

src/Contracts/ReflectionProperty.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ abstract public function getAttributeArguments(string $name): ?array;
3636
* @return string
3737
*/
3838
abstract public function getDocComment(): string;
39-
39+
4040
/**
4141
* @return bool
4242
*/

src/Exceptions/InstantiableClassException.php

+3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace ClassTransformer\Exceptions;
46

57
use LogicException;
68

79
/**
10+
* @psalm-api
811
* @infection-ignore-all
912
*/
1013
class InstantiableClassException extends LogicException

src/Hydrator.php

+20-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ClassTransformer;
44

5+
use RuntimeException;
56
use ReflectionException;
67
use ClassTransformer\CacheGenerator\CacheGenerator;
78
use ClassTransformer\Validators\ClassExistsValidator;
@@ -13,15 +14,19 @@
1314
/**
1415
* Class ClassRepository
1516
*
16-
* @template T of object
17+
* @psalm-api
18+
* @template T
1719
* @author yzen.dev <yzen.dev@gmail.com>
1820
*/
19-
class Hydrator
21+
final class Hydrator
2022
{
23+
/**
24+
* @var HydratorConfig
25+
*/
2126
private HydratorConfig $config;
2227

2328
/**
24-
* @var array<string,ClassRepository[]>
29+
* @var array<string,ClassRepository>
2530
*/
2631
private static array $classRepositoryCache = [];
2732

@@ -33,8 +38,11 @@ public function __construct(HydratorConfig $config = null)
3338
}
3439

3540
/**
41+
* @param HydratorConfig|null $config
42+
*
43+
* @return Hydrator
3644
*/
37-
public static function init(HydratorConfig $config = null)
45+
public static function init(HydratorConfig $config = null): self
3846
{
3947
return new self($config);
4048
}
@@ -46,9 +54,9 @@ public static function init(HydratorConfig $config = null)
4654
* @param iterable<mixed>|object ...$args
4755
*
4856
* @return null|T
49-
* @throws ClassNotFoundException
57+
* @throws ClassNotFoundException|RuntimeException
5058
*/
51-
public function create(string $class, ...$args)
59+
public function create(string $class, ...$args): mixed
5260
{
5361
new ClassExistsValidator($class);
5462

@@ -66,7 +74,7 @@ public function create(string $class, ...$args)
6674
* @param array<iterable<mixed>> $args
6775
*
6876
* @return null|array<null>|array<T>
69-
* @throws ClassNotFoundException
77+
* @throws ClassNotFoundException|ReflectionException
7078
*/
7179
public function createCollection(string $class, array $args): ?array
7280
{
@@ -82,7 +90,7 @@ public function createCollection(string $class, array $args): ?array
8290
* @param array<iterable<mixed>> $args
8391
*
8492
* @return null|array<null>|array<T>
85-
* @throws ClassNotFoundException
93+
* @throws ClassNotFoundException|ReflectionException
8694
*/
8795
public function createMultiple(array $classes, array $args): ?array
8896
{
@@ -95,12 +103,12 @@ public function createMultiple(array $classes, array $args): ?array
95103

96104
/**
97105
* @param class-string<T> $class
98-
* @param ...$args
106+
* @param iterable<mixed>|object ...$args
99107
*
100108
* @return mixed
101-
* @throws ClassNotFoundException
109+
* @throws ClassNotFoundException|RuntimeException
102110
*/
103-
private function getInstance(string $class, ...$args)
111+
private function getInstance(string $class, ...$args): mixed
104112
{
105113
if (method_exists($class, 'transform')) {
106114
$instance = new $class();
@@ -120,7 +128,7 @@ private function getInstance(string $class, ...$args)
120128
* @param class-string<T> $class
121129
*
122130
* @return ClassRepository
123-
* @throws ClassNotFoundException|ReflectionException
131+
* @throws ClassNotFoundException|RuntimeException
124132
*/
125133
private function createClassRepository(string $class): ClassRepository
126134
{

src/HydratorConfig.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@ final class HydratorConfig
1313
{
1414
/** @var bool Cache mode enabled */
1515
public bool $cacheEnabled;
16-
16+
1717
/** @var string Path to the cache directory */
1818
public string $cachePath;
19-
19+
20+
/**
21+
* @param bool|null $cacheEnabled
22+
* @param string|null $cachePath
23+
*/
2024
public function __construct(
2125
?bool $cacheEnabled = null,
2226
?string $cachePath = null
23-
)
24-
{
27+
) {
2528
$this->cacheEnabled = $cacheEnabled ?? false;
2629
$this->cachePath = $cachePath ?? __DIR__ . '/../.cache';
2730
}

src/InstanceBuilder.php

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public function build(): mixed
6060

6161
$caster = new ValueCasting($property, $this->config);
6262
$genericInstance->{$property->name} = $caster->castAttribute($value);
63-
6463
}
6564
return $genericInstance;
6665
}

0 commit comments

Comments
 (0)