-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSchemaService.php
More file actions
131 lines (119 loc) · 3.57 KB
/
Copy pathSchemaService.php
File metadata and controls
131 lines (119 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
declare(strict_types=1);
namespace ARiddlestone\PHPStanCakePHP2\Service;
use ARiddlestone\PHPStanCakePHP2\ClassReflectionFinder;
use Exception;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionProperty;
use PHPStan\Reflection\ReflectionProvider;
use ReflectionProperty as CoreReflectionProperty;
/**
* Identifies schema files, and uses them to provide information about tables
* and columns in the database.
*
* @phpstan-type table_schema mixed
* @phpstan-type column_schema mixed
*/
final class SchemaService
{
private ReflectionProvider $reflectionProvider;
/**
* @var array<string>
*/
private array $schemaPaths;
/**
* @var array<string, table_schema>
*/
private ?array $tableSchemas = null;
/**
* @param array<string> $schemaPaths
*/
public function __construct(
ReflectionProvider $reflectionProvider,
array $schemaPaths
) {
$this->reflectionProvider = $reflectionProvider;
$this->schemaPaths = $schemaPaths;
}
/**
* @throws Exception
*/
public function hasTable(string $table): bool
{
return array_key_exists($table, $this->getTableSchemas());
}
/**
* @return table_schema|null
*
* @throws Exception
*/
public function getTableSchema(string $table)
{
$tableSchemas = $this->getTableSchemas();
return array_key_exists($table, $tableSchemas)
? $tableSchemas[$table]
: null;
}
/**
* @return array<string, table_schema>
*
* @throws Exception
*/
private function getTableSchemas(): array
{
if (is_array($this->tableSchemas)) {
return $this->tableSchemas;
}
$cakeSchemaPropertyNames = array_map(
static function (ReflectionProperty $reflectionProperty) {
return $reflectionProperty->getName();
},
$this->reflectionProvider->getClass('CakeSchema')
->getNativeReflection()
->getProperties()
);
$this->tableSchemas = [];
$classReflectionFinder = new ClassReflectionFinder(
$this->reflectionProvider
);
$schemaReflections = $classReflectionFinder->getClassReflections(
$this->schemaPaths,
'CakeSchema',
function (string $fileName) {
return $this->fileNameToClassName($fileName);
}
);
foreach ($schemaReflections as $schemaReflection) {
$propertyNames = array_map(
static function (ReflectionProperty $reflectionProperty) {
return $reflectionProperty->getName();
},
$schemaReflection->getNativeReflection()
->getProperties(CoreReflectionProperty::IS_PUBLIC)
);
$tableProperties = array_diff(
$propertyNames,
$cakeSchemaPropertyNames
);
$this->tableSchemas += array_intersect_key(
$schemaReflection->getNativeReflection()
->getDefaultProperties(),
array_fill_keys($tableProperties, null)
);
}
return $this->tableSchemas;
}
private function fileNameToClassName(string $fileName): string
{
return str_replace(
' ',
'',
ucwords(
str_replace(
['_', '-'],
' ',
basename($fileName, '.php')
)
)
) . 'Schema';
}
}