Skip to content

Commit 2ed00fc

Browse files
committed
support column definition
1 parent 7dcede4 commit 2ed00fc

File tree

3 files changed

+53
-63
lines changed

3 files changed

+53
-63
lines changed

ide.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
{
1414
"classFqn": "Illuminate\\Database\\Schema\\Blueprint",
1515
"mixinFqn": "Tpetry\\PostgresqlEnhanced\\Schema\\Blueprint"
16+
},
17+
{
18+
"classFqn": "Illuminate\\Database\\Schema\\ColumnDefinition",
19+
"mixinFqn": "Tpetry\\PostgresqlEnhanced\\Schema\\ColumnDefinition"
1620
}
1721
]
1822
}

src/Schema/ColumnDefinition.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tpetry\PostgresqlEnhanced\Schema;
6+
7+
use Illuminate\Contracts\Database\Query\Expression;
8+
use Illuminate\Database\Schema\ColumnDefinition as BaseColumnDefinition;
9+
10+
/**
11+
* @internal This class is not used. It only exists to teach Laravel projects using PHPStan or IDEs supporting auto-suggest about added functionality.
12+
*/
13+
class ColumnDefinition extends BaseColumnDefinition
14+
{
15+
/**
16+
* Specify the compression method for TOASTed values (PostgreSQL).
17+
*/
18+
public function compression(string $algorithm): static
19+
{
20+
return $this;
21+
}
22+
23+
/**
24+
* Sets an initial value to the column (PostgreSQL).
25+
*/
26+
public function initial(mixed $value): static
27+
{
28+
return $this;
29+
}
30+
31+
/**
32+
* Specify casting expression when changing the column type (PostgreSQL).
33+
*/
34+
public function using(string|Expression $expression): static
35+
{
36+
return $this;
37+
}
38+
}

src/Support/Phpstan/SchemaColumnDefinitionExtension.php

Lines changed: 11 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,83 +4,31 @@
44

55
namespace Tpetry\PostgresqlEnhanced\Support\Phpstan;
66

7-
use Illuminate\Contracts\Database\Query\Expression as ExpressionContract;
8-
use Illuminate\Database\Schema\ColumnDefinition;
7+
use Illuminate\Database\Schema\ColumnDefinition as BaseColumnDefinition;
98
use PHPStan\Reflection\ClassReflection;
10-
use PHPStan\Reflection\FunctionVariant;
119
use PHPStan\Reflection\MethodReflection;
1210
use PHPStan\Reflection\MethodsClassReflectionExtension;
13-
use PHPStan\Type\Generic\TemplateTypeMap;
14-
use PHPStan\Type\MixedType;
15-
use PHPStan\Type\ObjectType;
16-
use PHPStan\Type\StringType;
17-
use Tpetry\PostgresqlEnhanced\Support\Phpstan\Values\ReflectedMethod;
18-
use Tpetry\PostgresqlEnhanced\Support\Phpstan\Values\ReflectedParameter;
11+
use PHPStan\Reflection\ReflectionProvider;
12+
use Tpetry\PostgresqlEnhanced\Schema\ColumnDefinition;
1913

2014
class SchemaColumnDefinitionExtension implements MethodsClassReflectionExtension
2115
{
22-
/**
23-
* @param 'compression'|'initial'|'using' $methodName
24-
*/
16+
public function __construct(
17+
private ReflectionProvider $reflectionProvider,
18+
) {
19+
}
20+
2521
public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
2622
{
27-
return match ($methodName) {
28-
'initial' => $this->getInitialMethod($classReflection),
29-
'compression' => $this->getCompressionMethod($classReflection),
30-
'using' => $this->getUsingMethod($classReflection),
31-
};
23+
return $this->reflectionProvider->getClass(ColumnDefinition::class)->getNativeMethod($methodName);
3224
}
3325

3426
public function hasMethod(ClassReflection $classReflection, string $methodName): bool
3527
{
36-
if (ColumnDefinition::class !== $classReflection->getName()) {
28+
if (BaseColumnDefinition::class !== $classReflection->getName()) {
3729
return false;
3830
}
3931

40-
return \in_array($methodName, ['compression', 'initial', 'using']);
41-
}
42-
43-
private function getCompressionMethod(ClassReflection $classReflection): MethodReflection
44-
{
45-
$parameters = [new ReflectedParameter('algorithm', new StringType())];
46-
$returnType = new ObjectType(ColumnDefinition::class);
47-
48-
return new ReflectedMethod(
49-
classReflection: $classReflection,
50-
name: 'compression',
51-
variants: [
52-
new FunctionVariant(TemplateTypeMap::createEmpty(), null, $parameters, false, $returnType),
53-
],
54-
);
55-
}
56-
57-
private function getInitialMethod(ClassReflection $classReflection): MethodReflection
58-
{
59-
$parameters = [new ReflectedParameter('value', new MixedType())];
60-
$returnType = new ObjectType(ColumnDefinition::class);
61-
62-
return new ReflectedMethod(
63-
classReflection: $classReflection,
64-
name: 'initial',
65-
variants: [
66-
new FunctionVariant(TemplateTypeMap::createEmpty(), null, $parameters, false, $returnType),
67-
],
68-
);
69-
}
70-
71-
private function getUsingMethod(ClassReflection $classReflection): MethodReflection
72-
{
73-
$parametersExpression = [new ReflectedParameter('expression', new ObjectType(ExpressionContract::class))];
74-
$parametersString = [new ReflectedParameter('expression', new StringType())];
75-
$returnType = new ObjectType(ColumnDefinition::class);
76-
77-
return new ReflectedMethod(
78-
classReflection: $classReflection,
79-
name: 'using',
80-
variants: [
81-
new FunctionVariant(TemplateTypeMap::createEmpty(), null, $parametersExpression, false, $returnType),
82-
new FunctionVariant(TemplateTypeMap::createEmpty(), null, $parametersString, false, $returnType),
83-
],
84-
);
32+
return $this->reflectionProvider->getClass(ColumnDefinition::class)->hasNativeMethod($methodName);
8533
}
8634
}

0 commit comments

Comments
 (0)