Skip to content

Commit

Permalink
Publish internal package
Browse files Browse the repository at this point in the history
  • Loading branch information
bastien-phi committed Mar 11, 2022
1 parent 506cb23 commit 37ecdad
Show file tree
Hide file tree
Showing 42 changed files with 866 additions and 161 deletions.
6 changes: 2 additions & 4 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php declare(strict_types=1);

$finder = PhpCsFixer\Finder::create()->in([
'config',
'database',
'src',
'tests',
]);
Expand Down Expand Up @@ -94,7 +92,7 @@
/*
* Class Usage
*/
'date_time_immutable' => true, // risky
'date_time_immutable' => false, // risky
/*
* Comment
*/
Expand Down Expand Up @@ -177,7 +175,7 @@
* Import
*/
'fully_qualified_strict_types' => true,
'global_namespace_import' => ['import_constants' => false, 'import_functions' => true, 'import_classes' => true],
'global_namespace_import' => ['import_constants' => false, 'import_functions' => true, 'import_classes' => null],
'group_import' => false,
'no_leading_import_slash' => true,
'no_unneeded_import_alias' => true,
Expand Down
27 changes: 14 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"keywords": [
"soyhuce",
"laravel",
"phpstan-extension"
"phpstan"
],
"homepage": "https://github.com/soyhuce/phpstan-extension",
"license": "MIT",
Expand All @@ -17,8 +17,9 @@
],
"require": {
"php": "^8.1",
"spatie/laravel-package-tools": "^1.9.2",
"illuminate/contracts": "^9.0"
"illuminate/support": "^9.0",
"nesbot/carbon": "^2.0",
"phpstan/phpstan": "^1.4.5"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.7",
Expand All @@ -34,8 +35,7 @@
},
"autoload": {
"psr-4": {
"Soyhuce\\PhpstanExtension\\": "src",
"Soyhuce\\PhpstanExtension\\Database\\Factories\\": "database/factories"
"Soyhuce\\PhpstanExtension\\": "src"
}
},
"autoload-dev": {
Expand All @@ -55,16 +55,17 @@
]
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true,
"phpstan/extension-installer": true
}
},
"extra": {
"laravel": {
"providers": [
"Soyhuce\\PhpstanExtension\\PhpstanExtensionServiceProvider"
],
"aliases": {
"PhpstanExtension": "Soyhuce\\PhpstanExtension\\Facades\\PhpstanExtension"
}
"phpstan": {
"includes": [
"extension.neon"
]
}
},
"minimum-stability": "dev",
Expand Down
4 changes: 0 additions & 4 deletions config/phpstan-extension.php

This file was deleted.

19 changes: 0 additions & 19 deletions database/factories/ModelFactory.php

This file was deleted.

19 changes: 0 additions & 19 deletions database/migrations/create_phpstan_extension_table.php.stub

This file was deleted.

15 changes: 15 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
services:
- class: Soyhuce\PhpstanExtension\ReturnTypes\DateExtension
tags:
- phpstan.broker.dynamicStaticMethodReturnTypeExtension

- class: Soyhuce\PhpstanExtension\ReturnTypes\NowAndTodayExtension
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

rules:
- Soyhuce\PhpstanExtension\Rules\CarbonCopyRule
- Soyhuce\PhpstanExtension\Rules\NoAliasUseRule
- Soyhuce\PhpstanExtension\Rules\NoMutableDateTimeStaticCallRule
- Soyhuce\PhpstanExtension\Rules\NoMutableDateTimeUseRule
- Soyhuce\PhpstanExtension\Rules\NoNewMutableDateTimeRule
4 changes: 1 addition & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ includes:
- phpstan-baseline.neon

parameters:
level: 8
level: 9
paths:
- src
- config
- database
checkOctaneCompatibility: true
checkModelProperties: true
checkMissingIterableValueType: true
Expand Down
Empty file removed resources/views/.gitkeep
Empty file.
19 changes: 0 additions & 19 deletions src/Commands/PhpstanExtensionCommand.php

This file was deleted.

16 changes: 0 additions & 16 deletions src/Facades/PhpstanExtension.php

This file was deleted.

7 changes: 0 additions & 7 deletions src/PhpstanExtension.php

This file was deleted.

25 changes: 0 additions & 25 deletions src/PhpstanExtensionServiceProvider.php

This file was deleted.

72 changes: 72 additions & 0 deletions src/ReturnTypes/DateExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php declare(strict_types=1);

namespace Soyhuce\PhpstanExtension\ReturnTypes;

use Illuminate\Support\Facades\Date;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function get_class;
use function in_array;

class DateExtension implements DynamicStaticMethodReturnTypeExtension
{
public function getClass(): string
{
return Date::class;
}

public function isStaticMethodSupported(MethodReflection $methodReflection): bool
{
return in_array(
$methodReflection->getName(),
[
'create',
'createFromDate',
'createFromTime',
'createFromTimeString',
'createFromTimestamp',
'createFromTimestampMs',
'createFromTimestampUTC',
'createMidnightDate',
'fromSerialized',
'getTestNow',
'instance',
'isMutable',
'maxValue',
'minValue',
'now',
'parse',
'today',
'tomorrow',
'yesterday',
'createFromFormat',
'createSafe',
'make',
]
);
}

public function getTypeFromStaticMethodCall(
MethodReflection $methodReflection,
StaticCall $methodCall,
Scope $scope,
): Type {
$dateType = new ObjectType(get_class(now()));

if (in_array($methodReflection->getName(), ['createFromFormat', 'createSafe'])) {
return TypeCombinator::union($dateType, new ConstantBooleanType(false));
}

if (in_array($methodReflection->getName(), ['getTestNow', 'make'])) {
return TypeCombinator::addNull($dateType);
}

return $dateType;
}
}
27 changes: 27 additions & 0 deletions src/ReturnTypes/NowAndTodayExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types=1);

namespace Soyhuce\PhpstanExtension\ReturnTypes;

use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use function get_class;

class NowAndTodayExtension implements DynamicFunctionReturnTypeExtension
{
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'now' || $functionReflection->getName() === 'today';
}

public function getTypeFromFunctionCall(
FunctionReflection $functionReflection,
FuncCall $functionCall,
Scope $scope,
): Type {
return new ObjectType(get_class(now()));
}
}
Loading

0 comments on commit 37ecdad

Please sign in to comment.