Skip to content
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

Phpstan improvements #191

Merged
merged 10 commits into from
Oct 2, 2024
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/.gitignore export-ignore
/.markdownlint.yaml export-ignore
/.markdownlint-cli2.yaml export-ignore
/composer.lock export-ignore
/composer-dependency-analyser.php export-ignore
/composer-require-checker.json export-ignore
/docker-compose.yaml export-ignore
Expand All @@ -20,6 +21,7 @@
/package-lock.json export-ignore
/phpcs.xml export-ignore
/phpstan.neon export-ignore
/phpstan-larastan.neon export-ignore
/phpstan-baseline.neon export-ignore
/phpstan-baseline-well-known.neon export-ignore
/phpunit.xml export-ignore
Expand Down
7 changes: 3 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ jobs:
# (because will install whole "laravel/framework")
composer remove --no-interaction --no-progress --ansi --no-update "orchestra/testbench"
composer remove --no-interaction --no-progress --ansi --no-update --dev "orchestra/testbench"

# (because it used only to run phpstan)
composer remove --no-interaction --no-progress --ansi --no-update --dev "phpstan/phpstan"
- name: Install dependencies
uses: ramsey/composer-install@v3

Expand Down Expand Up @@ -162,10 +165,6 @@ jobs:
uses: actions/checkout@v4
- name: Install PHP
uses: ./.github/actions/php
- name: Install phpstan
uses: ./.github/actions/tool
with:
tool: phpstan
- name: Execute
run: |
composer run-script phpstan -- --error-format=checkstyle | cs2pr
Expand Down
8 changes: 7 additions & 1 deletion composer-dependency-analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@
$path = Path::realpath(getopt('', ['composer-json:'])['composer-json'] ?? 'composer.json');
$root = Path::realpath(dirname(__FILE__).'/composer.json') === $path;

if (!$root) {
if ($root) {
$config
->ignoreErrorsOnPackage('phpstan/phpstan-mockery', [ErrorType::UNUSED_DEPENDENCY])
->ignoreErrorsOnPackage('phpstan/phpstan-phpunit', [ErrorType::UNUSED_DEPENDENCY])
->ignoreErrorsOnPackage('phpstan/phpstan-strict-rules', [ErrorType::UNUSED_DEPENDENCY])
->ignoreErrorsOnPackage('spaze/phpstan-disallowed-calls', [ErrorType::UNUSED_DEPENDENCY]);
} else {
$config->disableReportingUnmatchedIgnores();
}

Expand Down
28 changes: 25 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,17 @@
"illuminate/testing": "^10.34.0|^11.0.0",
"illuminate/translation": "^10.34.0|^11.0.0",
"illuminate/validation": "^10.34.0|^11.0.0",
"larastan/larastan": "^2.8.1",
"league/commonmark": "^2.5.1",
"league/config": "^1.1.1",
"mockery/mockery": "^1.6.5",
"nette/neon": "^3.4",
"nikic/php-parser": "^5.0",
"nuwave/lighthouse": "^6.5.0",
"opis/json-schema": "^2.3.0",
"phpdocumentor/type-resolver": "^1.7",
"phpstan/phpdoc-parser": "^1.25",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^10.1.0|^11.0.0",
"psr/http-message": "^1.0.0|^2.0.0",
"sebastian/comparator": "^5.0|^6.0.0",
Expand All @@ -85,6 +88,7 @@
"symfony/property-info": "^6.3.9|^7.0.0",
"symfony/psr-http-message-bridge": "^2.0.0|^6.4.0|^7.0.0",
"symfony/serializer": "^6.4.0|^7.0.0",
"symfony/var-dumper": "^6.3.0|^7.0.0",
"webonyx/graphql-php": "^15.4.0"
},
"require-dev": {
Expand All @@ -95,7 +99,12 @@
"illuminate/cache": "^10.34.0|^11.0.0",
"illuminate/config": "^10.34.0|^11.0.0",
"laravel/scout": "^9.8.0|^10.0.0",
"orchestra/testbench": "^8.0.0|^9.0.0"
"orchestra/testbench": "^8.0.0|^9.0.0",
"phpstan/phpstan": "1.12.5",
"phpstan/phpstan-mockery": "^1.1",
"phpstan/phpstan-phpunit": "^1.3",
"phpstan/phpstan-strict-rules": "^1.5",
"spaze/phpstan-disallowed-calls": "^3.0"
},
"autoload": {
"files": [
Expand All @@ -104,6 +113,7 @@
],
"psr-4": {
"LastDragon_ru\\LaraASP\\Core\\": "packages/core/src/",
"LastDragon_ru\\LaraASP\\Dev\\": "packages/dev/src/",
"LastDragon_ru\\LaraASP\\Documentator\\": "packages/documentator/src/",
"LastDragon_ru\\LaraASP\\Eloquent\\": "packages/eloquent/src/",
"LastDragon_ru\\LaraASP\\Formatter\\": "packages/formatter/src/",
Expand All @@ -118,6 +128,7 @@
"autoload-dev": {
"exclude-from-classmap": [
"packages/core/docs/",
"packages/dev/docs/",
"packages/documentator/docs/",
"packages/eloquent/docs/",
"packages/formatter/docs/",
Expand All @@ -130,6 +141,7 @@
],
"psr-4": {
"LastDragon_ru\\LaraASP\\Core\\Docs\\": "packages/core/docs",
"LastDragon_ru\\LaraASP\\Dev\\Docs\\": "packages/dev/docs",
"LastDragon_ru\\LaraASP\\Documentator\\Docs\\": "packages/documentator/docs",
"LastDragon_ru\\LaraASP\\Eloquent\\Docs\\": "packages/eloquent/docs",
"LastDragon_ru\\LaraASP\\Formatter\\Docs\\": "packages/formatter/docs",
Expand Down Expand Up @@ -164,6 +176,7 @@
"laravel": {
"providers": [
"LastDragon_ru\\LaraASP\\Core\\Provider",
"LastDragon_ru\\LaraASP\\Dev\\Provider",
"LastDragon_ru\\LaraASP\\Documentator\\Provider",
"LastDragon_ru\\LaraASP\\Eloquent\\Provider",
"LastDragon_ru\\LaraASP\\Formatter\\Provider",
Expand All @@ -175,6 +188,15 @@
}
},
"scripts": {
"post-install-cmd": [
"composer run-script dump:larastan"
],
"post-update-cmd": [
"composer run-script dump:larastan"
],
"dump:larastan": [
"LastDragon_ru\\LaraASP\\Dev\\PhpStan\\Larastan\\Extension::dump"
],
"validate:commit": [
"@phpstan",
"@phpunit",
Expand All @@ -192,7 +214,7 @@
"jq --indent 2 '.' composer.json > composer.tmp && mv composer.tmp composer.json"
],
"phpstan": [
"./vendor-bin/phpstan/vendor/bin/phpstan analyse -v"
"./vendor/bin/phpstan analyse -v"
],
"phpunit": [
"./vendor/bin/phpunit"
Expand Down Expand Up @@ -237,8 +259,8 @@
},
"replace": {
"lastdragon-ru/lara-asp-core": "self.version",
"lastdragon-ru/lara-asp-documentator": "self.version",
"lastdragon-ru/lara-asp-dev": "self.version",
"lastdragon-ru/lara-asp-documentator": "self.version",
"lastdragon-ru/lara-asp-eloquent": "self.version",
"lastdragon-ru/lara-asp-formatter": "self.version",
"lastdragon-ru/lara-asp-graphql": "self.version",
Expand Down
1 change: 0 additions & 1 deletion monorepo-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

return static function (MBConfig $config): void {
$config->packageDirectories([__DIR__.'/packages']);
$config->packageDirectoriesExcludes(['dev', 'src']);
};
2 changes: 1 addition & 1 deletion packages/core/src/Helpers/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected function translate(array|string $variants, Closure $callback): string
$translated = array_splice($variants, -1);
$translated = (string) end($translated);

if ($variants) {
if ($variants !== []) {
foreach ($variants as $variant) {
$result = $callback($variant);

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/Utils/ConfigMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected function process(array &$target, array $config, string $path): void {
// Merge
foreach ($config as $key => &$value) {
// Current path
$current = $path ? "{$path}.{$key}" : "{$key}";
$current = $path !== '' ? "{$path}.{$key}" : "{$key}";

// Only scalars/nulls and arrays of them allowed
if (!is_scalar($value) && !is_null($value) && !is_array($value)) {
Expand All @@ -106,11 +106,11 @@ protected function process(array &$target, array $config, string $path): void {
throw new InvalidArgumentException('Array cannot be replaced by scalar/null value.');
}

if ($target[$key][static::Replace] ?? false) {
if (isset($target[$key][static::Replace]) && $target[$key][static::Replace] === true) {
$target[$key] = [static::Replace => true] + (array) $value;
} elseif (is_string(key($target[$key]))) {
$this->process($target[$key], (array) $value, $current);
} elseif (!$target[$key]) {
} elseif ($target[$key] === []) {
$target[$key] = (array) $value;
} else {
$target[$key] = array_values((array) $value);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Utils/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function register(Schedule $schedule, string $class): bool {
$settings = $this->getSettings($class, $instance);

// Enabled?
if (!($settings['enabled'] ?? true) || !$settings['cron']) {
if (!($settings['enabled'] ?? true) || $settings['cron'] === '') {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/dev/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"extra": {
"laravel": {
"providers": [
"LastDragon_ru\\LaraASP\\Dev\\App\\Provider"
"LastDragon_ru\\LaraASP\\Dev\\Provider"
]
}
},
Expand Down
2 changes: 2 additions & 0 deletions packages/dev/docs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
4 changes: 2 additions & 2 deletions packages/dev/src/App/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Dumper {
public function __construct() {
$this->cloner = new VarCloner([
'*' => static function (object $obj, array $a, Stub $stub, bool $isNested, int $filter = 0): array {
return $filter ? Caster::filter($a, $filter) : $a;
return $filter > 0 ? Caster::filter($a, $filter) : $a;
},
]);
$this->dumper = new CliDumper(
Expand All @@ -50,7 +50,7 @@ public function dump(mixed $value, ?string $expression): void {

public function raw(Stringable|string $value, ?string $expression, string $type): void {
$dump = trim((string) $value);
$dump = ($expression ? "The `{$expression}` is:\n\n" : '')."```{$type}\n{$dump}\n```\n";
$dump = ($expression !== null ? "The `{$expression}` is:\n\n" : '')."```{$type}\n{$dump}\n```\n";
$this->dumps[] = $dump;
}
}
10 changes: 5 additions & 5 deletions packages/dev/src/App/Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use LastDragon_ru\LaraASP\Core\Utils\ConfigMerger;
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\File;
use LastDragon_ru\LaraASP\Documentator\Processor\Tasks\Preprocess\Instructions\IncludeExample\Contracts\Runner;
use LastDragon_ru\LaraASP\Documentator\Utils\Text;
use LogicException;
use Override;
use PhpParser\ErrorHandler\Collecting;
Expand All @@ -25,7 +26,6 @@
use function debug_backtrace;
use function end;
use function implode;
use function preg_split;
use function sprintf;
use function str_contains;
use function trim;
Expand All @@ -44,7 +44,7 @@ public function __construct(
}

protected static function getDumper(): Dumper {
if (!self::$dumper) {
if (self::$dumper === null) {
throw new LogicException(
sprintf(
'The `%s` can be called only within example context.',
Expand Down Expand Up @@ -79,7 +79,7 @@ public function __invoke(File $file): ?string {
$dumps = self::$dumper->getDumps();
$output = implode("\n\n", array_map(trim(...), $dumps));

if ($output) {
if ($output !== '') {
$result = "<markdown>{$output}</markdown>";
}
} finally {
Expand All @@ -100,7 +100,7 @@ public static function raw(Stringable|string $value, string $type, ?string $expr
}

protected static function app(): Application {
if (!self::$app) {
if (self::$app === null) {
throw new LogicException(
sprintf(
'The `%s` can be called only within example context.',
Expand Down Expand Up @@ -136,7 +136,7 @@ private static function getExpression(string $method): ?string {
}

// Extract first arg
$lines = preg_split('/\R/u', self::$file?->getContent() ?? '') ?: [];
$lines = Text::getLines(self::$file?->getContent() ?? '');
$code = implode("\n", array_slice($lines, $context['line'] - 1));
$parser = (new ParserFactory())->createForNewestSupportedVersion();
$stmts = (array) $parser->parse("<?php\n{$code}", new Collecting());
Expand Down
2 changes: 1 addition & 1 deletion packages/dev/src/PhpStan/ClassMustBeInternal/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private function mustBeIsInstanceOf(ClassReflection $reflection): bool {
private function mustBeIsTestInternal(ClassReflection $reflection): bool {
$classname = $reflection->getNativeReflection()->getShortName();
$filename = pathinfo((string) $reflection->getFileName(), PATHINFO_FILENAME);
$mustBe = $filename
$mustBe = $filename !== ''
&& str_ends_with($filename, 'Test')
&& str_starts_with($classname, "{$filename}_");

Expand Down
11 changes: 6 additions & 5 deletions packages/dev/src/PhpStan/Larastan/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use function array_filter;
use function array_keys;
use function array_values;
use function dirname;
use function file_get_contents;
use function file_put_contents;
use function getcwd;
Expand All @@ -27,13 +28,13 @@
class Extension {
/**
* Removes unwanted/conflicting services from `larastan/extension.neon` and
* dump remaining into `larastan.neon` (that should be used instead of the
* original file).
* dump remaining into `phpstan-larastan.neon` (that should be used instead
* of the original file).
*/
public static function dump(): void {
// Prepare
$origin = Path::join(self::getLarastanPath(), 'extension.neon');
$target = Path::getPath(self::getRootPath(), 'larastan.neon');
$target = Path::getPath(self::getRootPath(), 'phpstan-larastan.neon');

// Load
$extension = Neon::decode((string) file_get_contents($origin));
Expand Down Expand Up @@ -68,7 +69,7 @@ private static function updateBootstrapFiles(string $path, array $extension): ar
// Update
$source = self::getLarastanPath();
$files = (array) ($extension['parameters']['bootstrapFiles'] ?? []);
$root = Path::getDirname($path);
$root = dirname($path);

foreach ($files as $index => $file) {
if (!is_string($file)) {
Expand Down Expand Up @@ -118,7 +119,7 @@ private static function updateServices(string $path, array $extension): array {
// Unused?
$unused = array_keys(array_filter($disabled));

if ($unused) {
if ($unused !== []) {
throw new Exception(
sprintf(
'The following services is unknown: `%s`',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Dev\App;
namespace LastDragon_ru\LaraASP\Dev;

use Illuminate\Support\ServiceProvider;
use LastDragon_ru\LaraASP\Dev\App\Example;
use LastDragon_ru\LaraASP\Documentator\Processor\Tasks\Preprocess\Instructions\IncludeExample\Contracts\Runner;
use Override;

Expand Down
Loading
Loading