定制 saschaegerer/phpstan-typo3 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

saschaegerer/phpstan-typo3

Composer 安装命令:

composer require --dev saschaegerer/phpstan-typo3

包简介

TYPO3 CMS class reflection extension for PHPStan

关键字:

README 文档

README

TYPO3 CMS class reflection extension for PHPStan & framework-specific rules.

Build

This extension provides the following features (!!! not an exhaustive list !!!):

Dynamic Return Type Extensions

  • Provides correct return type for \TYPO3\CMS\Core\Context\Context->getAspect().
  • Provides correct return type for \TYPO3\CMS\Core\Context\DateTimeAspect->get().
  • Provides correct return type for \TYPO3\CMS\Core\Context\UserAspect->get().
  • Provides correct return type for \TYPO3\CMS\Extbase\Property\PropertyMapper->convert().
  • Provides correct return type for \TYPO3\CMS\Core\Utility\MathUtility methods like isIntegerInRange.
  • Provides correct return type for \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv().
  • Provides correct return type for \TYPO3\CMS\Extbase\Persistence\QueryInterface->execute().
  • Provides correct return type for \TYPO3\CMS\Extbase\Persistence\ObjectStorage methods.
  • Provides correct return type for \TYPO3\CMS\Core\Site\Entity\Site->getAttribute().
  • Provides correct return type for \Psr\Http\Message\ServerRequestInterface->getAttribute().
  • Provides correct return type for \TYPO3\CMS\Extbase\Validation\ValidatorResolver->createValidator().
  • Uses under the hood bnf/phpstan-psr-container

All these dynamic return type extensions are necessary to teach PHPStan what type will be returned by the specific method call.

Show me a practical use case. For example PHPStan cannot know innately what type will be returned if you call `\TYPO3\CMS\Core\Utility\MathUtility->forceIntegerInRange(1000, 1, 10)`. It will be an int<10>. With the help of this library PHPStan also knows what´s going up.

Imagine the following situation in your code:

use TYPO3\CMS\Core\Utility\MathUtility;

$integer = MathUtility::forceIntegerInRange(100, 1, 10);

if($integer > 10) {
    throw new \UnexpectedValueException('The integer is too big')
}

PHPStan will tell you that the if condition is superfluous, because the variable $integer will never be higher than 10. Right?

Framework specific rules

  • Validates \TYPO3\CMS\Core\Context\Context->getAspect() aspect names.
  • Validates \Psr\Http\Message\ServerRequestInterface->getAttribute() attribute names.
  • Validates \TYPO3\CMS\Core\Site\Entity\Site->getAttribute() attribute names.
  • Validates \TYPO3\CMS\Extbase\Validation\ValidatorResolver->createValidator() required options.
  • Detects private service access via \Psr\Container\ContainerInterface->get().
  • Detects private service access via \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance().
Show me a practical use case.

For example PHPStan cannot know innately that calling ValidatorResolver->createValidator(RegularExpressionValidator::class) is invalid, because we miss to pass the required option regularExpression. With the help of this library PHPStan now complaints that we have missed to pass the required option. So go ahead and find bugs in your code without running it.

Installation & Configuration

To use this extension, require it in Composer:

composer require --dev saschaegerer/phpstan-typo3

If you also install phpstan/extension-installer then you're all set!

Manual installation

If you don't want to use phpstan/extension-installer, put this into your phpstan.neon config:

includes:
    - vendor/saschaegerer/phpstan-typo3/extension.neon

Custom Context API Aspects

If you use custom aspects for the TYPO3 Context API you can add a mapping so PHPStan knows what type of aspect class is returned by the context API

parameters:
    typo3:
        contextApiGetAspectMapping:
            myCustomAspect: FlowdGmbh\MyProject\Context\MyCustomAspect
// PHPStan will now know that $myCustomAspect is of type FlowdGmbh\MyProject\Context\MyCustomAspect
$myCustomAspect = GeneralUtility::makeInstance(Context::class)->getAspect('myCustomAspect');

Custom Request Attribute

If you use custom PSR-7 request attribute you can add a mapping so PHPStan knows what type of class is returned by Request::getAttribute()

parameters:
    typo3:
        requestGetAttributeMapping:
            myAttribute: FlowdGmbh\MyProject\Http\MyAttribute
            myNullableAttribute: FlowdGmbh\MyProject\Http\MyAttribute|null
// PHPStan will now know that $myAttribute is of type FlowdGmbh\MyProject\Http\MyAttribute
$myAttribute = $request->getAttribute('myAttribute');

Custom Site Attribute

If you use custom attributes for the TYPO3 Site API you can add a mapping so PHPStan knows what type is returned by the site API

parameters:
    typo3:
        siteGetAttributeMapping:
            myArrayAttribute: array
            myIntAttribute: int
            myStringAttribute: string
$site = $this->request->getAttribute('site');

// PHPStan will now know that $myArrayAttribute is of type array<mixed, mixed>
$myArrayAttribute = $site->getAttribute('myArrayAttribute');

// PHPStan will now know that $myIntAttribute is of type int
$myIntAttribute = $site->getAttribute('myIntAttribute');

// PHPStan will now know that $myStringAttribute is of type string
$myStringAttribute = $site->getAttribute('myStringAttribute');

Check for private Services

You have to provide a path to App_KernelDevelopmentDebugContainer.xml or similar XML file describing your container. This is generated by ssch/typo3-debug-dump-pass in your /var/cache/{TYPO3_CONTEXT}/ folder.

parameters:
    typo3:
        containerXmlPath: var/cache/development/App_KernelDevelopmentDebugContainer.xml

Development

Running Tests and Quality Checks

The following composer scripts are available for development:

Command Description
composer test Run all quality checks (lint, cs, phpstan, phpunit)
composer test:php-lint Run PHP syntax linting using parallel-lint
composer test:php-cs Run code style checks using PHP_CodeSniffer
composer test:phpstan Run static analysis using PHPStan
composer test:phpunit Run PHPUnit tests
composer fix:php-cs Fix code style issues using PHP Code Beautifier

saschaegerer/phpstan-typo3 适用场景与选型建议

saschaegerer/phpstan-typo3 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.86M 次下载、GitHub Stars 达 50, 最近一次更新时间为 2017 年 10 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「static analysis」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 saschaegerer/phpstan-typo3 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 saschaegerer/phpstan-typo3 我们能提供哪些服务?
定制开发 / 二次开发

基于 saschaegerer/phpstan-typo3 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.86M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 50
  • 点击次数: 29
  • 依赖项目数: 350
  • 推荐数: 0

GitHub 信息

  • Stars: 50
  • Watchers: 6
  • Forks: 24
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-10-18