Skip to content

Code quality fixes #27

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 2 commits into
base: main
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
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
run: composer run-script phpinsights-github

- name: Run static type analysis
run: composer run-script phpstan
run: composer run-script phpstan-github

- name: Run test suite
run: composer run-script phpunit
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"phpinsights": "phpinsights analyse --no-interaction --ansi --config-path=phpinsights.php --summary src",
"phpinsights-github": "phpinsights analyse --no-interaction --ansi --config-path=phpinsights.php --format=github-action src",
"phpstan": "phpstan",
"phpstan-github": "phpstan --error-format=github",
"phpunit": "phpunit",
"test": [
"@phpunit",
Expand Down
5 changes: 4 additions & 1 deletion src/ClassReflectionFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ private function getClassNamesFromPaths(
}
$classPaths = array_merge($classPaths, $filePaths);
}
$classNames = array_map($pathToClassName ?? [$this, 'getClassNameFromFileName'], $classPaths);
$classNames = array_map(
$pathToClassName ?? [$this, 'getClassNameFromFileName'],
$classPaths
);
return array_filter(
$classNames,
[$this->reflectionProvider, 'hasClass']
Expand Down
24 changes: 15 additions & 9 deletions src/ClassRegistryInitExtension.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace ARiddlestone\PHPStanCakePHP2;

use ARiddlestone\PHPStanCakePHP2\Service\SchemaService;
Expand All @@ -10,22 +12,22 @@
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\BooleanType;
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension as PHPStanExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;

class ClassRegistryInitExtension implements DynamicStaticMethodReturnTypeExtension
final class ClassRegistryInitExtension implements PHPStanExtension
{
private ReflectionProvider $reflectionProvider;

private SchemaService $schemaService;

public function __construct(
ReflectionProvider $reflectionProvider,
SchemaService $schemaService)
{
SchemaService $schemaService
) {
$this->reflectionProvider = $reflectionProvider;
$this->schemaService = $schemaService;
}
Expand All @@ -35,13 +37,17 @@ public function getClass(): string
return 'ClassRegistry';
}

public function isStaticMethodSupported(MethodReflection $methodReflection): bool
{
public function isStaticMethodSupported(
MethodReflection $methodReflection
): bool {
return $methodReflection->getName() === 'init';
}

public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type
{
public function getTypeFromStaticMethodCall(
MethodReflection $methodReflection,
StaticCall $methodCall,
Scope $scope
): ?Type {
$arg1 = $methodCall->getArgs()[0]->value;
$evaluator = new ConstExprEvaluator();
$arg1 = $evaluator->evaluateSilently($arg1);
Expand All @@ -61,7 +67,7 @@ private function getDefaultType(): Type
{
return new UnionType([
new BooleanType(),
new ObjectWithoutClassType()
new ObjectWithoutClassType(),
]);
}
}
21 changes: 14 additions & 7 deletions src/Service/SchemaService.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace ARiddlestone\PHPStanCakePHP2\Service;

use ARiddlestone\PHPStanCakePHP2\ClassReflectionFinder;
Expand Down Expand Up @@ -30,7 +32,6 @@ final class SchemaService
private ?array $tableSchemas = null;

/**
* @param ReflectionProvider $reflectionProvider
* @param array<string> $schemaPaths
*/
public function __construct(
Expand All @@ -50,8 +51,8 @@ public function hasTable(string $table): bool
}

/**
* @param string $table
* @return table_schema|null
*
* @throws Exception
*/
public function getTableSchema(string $table)
Expand All @@ -73,10 +74,12 @@ private function getTableSchemas(): array
return $this->tableSchemas;
}
$cakeSchemaPropertyNames = array_map(
function (ReflectionProperty $reflectionProperty) {
static function (ReflectionProperty $reflectionProperty) {
return $reflectionProperty->getName();
},
$this->reflectionProvider->getClass('CakeSchema')->getNativeReflection()->getProperties()
$this->reflectionProvider->getClass('CakeSchema')
->getNativeReflection()
->getProperties()
);
$this->tableSchemas = [];
$classReflectionFinder = new ClassReflectionFinder(
Expand All @@ -91,15 +94,19 @@ function (string $fileName) {
);
foreach ($schemaReflections as $schemaReflection) {
$propertyNames = array_map(
function (ReflectionProperty $reflectionProperty) {
static function (ReflectionProperty $reflectionProperty) {
return $reflectionProperty->getName();
},
$schemaReflection->getNativeReflection()
->getProperties(CoreReflectionProperty::IS_PUBLIC)
);
$tableProperties = array_diff($propertyNames, $cakeSchemaPropertyNames);
$tableProperties = array_diff(
$propertyNames,
$cakeSchemaPropertyNames
);
$this->tableSchemas += array_intersect_key(
$schemaReflection->getNativeReflection()->getDefaultProperties(),
$schemaReflection->getNativeReflection()
->getDefaultProperties(),
array_fill_keys($tableProperties, null)
);
}
Expand Down