定制 cambis/silverstan 二次开发

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

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

cambis/silverstan

Composer 安装命令:

composer require --dev cambis/silverstan

包简介

PHPStan extensions and rules for Silverstripe CMS.

README 文档

README

PHPStan extensions and rules for Silverstripe CMS.

Latest stable version Total downloads Licence Build status

Features ✨

Here are some of the nice features this extension provides:

  • Recognition that configuration properties are always read and written.
  • Correct return type for SilverStripe\Config\Collections\ConfigCollectionInterface::get().
  • Correct return type for SilverStripe\Core\Config\Config_ForClass::get().
  • Resolution of SilverStripe\Core\Extensible magic methods and properties.
  • Type specification for SilverStripe\Core\Extensible::hasExtension() and SilverStripe\Core\Extensible::hasMethod() methods.
  • Correct return types for SilverStripe\Core\Extension::$owner and SilverStripe\Core\Extension::getOwner().
  • Correct return types for SilverStripe\Core\Injector\Injector::get() and SilverStripe\Core\Injector\Injector::create().
  • Correct return type for SilverStripe\ORM\DataObject::dbObject().
  • Type specification for SilverStripe\Model\ModelData::hasField() method.
  • Type specification for SilverStripe\View\ViewableData::hasField() method.
  • Various correct return types for commonly used Silverstripe modules.
  • Customisable rules to help make your application safer.

Installation 👷‍♀️

Install via composer.

composer require --dev cambis/silverstan

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

Manual installation

If you don't want to use phpstan/extension-installer, include extension.neon in your project's PHPStan config:

includes:
    - vendor/cambis/silverstan/extension.neon

Silverstripe 5.2 or greater is recommended

While this extension is not tied to a specific Silverstripe version it is recommended that you are on at least Silverstripe 5.2.

Silverstripe 5.2 introduces generic typehints. These changes allow the module to infer the types of objects without relying on an extension.

To make the best use of this module, make sure that your classes are correctly annotated using a combination of generics, and property/method annotations.

Bleeding edge 🔪

New and experimental features are available via the bleeding edge config. You can opt in by including the relevant config file.

includes:
    - vendor/cambis/silverstan/bleedingEdge.neon

Rules 🚨

Silverstan provides a set of customisable rules that can help make your application safer.

Each rule can be enabled/disabled individually using the configuration options, please refer to the rules overview for the available options.

SilverStripe\Dev\TestOnly 👨‍🔬

Complex analysis of SilverStripe\Dev\TestOnly classes is disabled by default. This is because these classes often contain dependencies that aren't provided by Silverstripe.

To enable complex analysis of these classes, please check the following option in your configuration file:

parameters:
    silverstan:
        includeTestOnly: true

If PHPStan complains about missing classes, be sure to add the corresponding package to your dev dependencies.

SilverStripe\Core\Extensible 🧑‍🔬

Solving magic methods and properties

Silverstan provides support for magic SilverStripe\Core\Extensible methods and properties.

Silverstan will attempt to resolve magic methods/properties by searching for existing annotations in the class ancestry first. If no annotation is found it will access the configuration API in order to resolve the magic method/property.

Using annotations is preferred, as they can often provide more information, have stricter types, and reduce the number of calls to the configuration API.

You can use Silverstripe Rector to create the annotations for you.

Solving SilverStripe\Core\Extensible::hasExtension() and SilverStripe\Core\Extensible::hasMethod()

Silverstan provides type specifying extensions for these cases. However, these extensions can only be applied on a per class basis.

The default configuration applies these extensions to SilverStripe\View\ViewableData and SilverStripe\Model\ModelData only. If you wish to add them to other SilverStripe\Core\Extensible classes that aren't subclasses of the former you can use the following configuration:

services:
    -
        # Solves `Foo::hasExtension()`
        class: Cambis\Silverstan\Type\TypeSpecifyingExtension\ExtensibleHasExtensionTypeSpecifyingExtension
        tags: [phpstan.typeSpecifier.methodTypeSpecifyingExtension]
        arguments:
            className: 'Foo'
    -
        # Solves `Foo::hasMethod()`
        class: Cambis\Silverstan\Type\TypeSpecifyingExtension\ExtensibleHasMethodTypeSpecifyingExtension
        tags: [phpstan.typeSpecifier.methodTypeSpecifyingExtension]
        arguments:
            className: 'Foo'

Solving SilverStripe\Core\Extensible::has_extension()

Warning

Silverstan does not support type specification for SilverStripe\Core\Extensible::has_extension(). If you use this method in your codebase, consider using one of the following examples to help solve errors that may be reported by PHPStan.

In the example below, we are adding a typehint to inform PHPStan of the expected type.

if (\SilverStripe\View\ViewableData::has_extension(Foo::class, FooExtension::class)) {
+  /** @var Foo&FooExtension $foo */
  $foo = Foo::create();
}

In the example below, we are changing the calls to use the dynamic SilverStripe\Core\Extensible::hasExtension() method which is supported by Silverstan.

$foo = Foo::create();

- if ($foo->has_extension(FooExtension::class)) {
+ if ($foo->hasExtension(FooExtension::class)) {
  // ...
}

- if ($foo::has_extension(FooExtension::class)) {
+ if ($foo->hasExtension(FooExtension::class)) {
  // ...
}

SilverStripe\Core\Config\Configurable 🧑‍🏭

The missingType.iterableValue error on configuration properties is ignored by default. This is because adding iterable information isn't useful unless the property is modified or accessed inside the current scope.

You can disable this behaviour in your configuration file.

parameters:
    silverstan:
        ignoreConfigurationPropertyTypeIterableValue: false

SilverStripe\Core\Config\Config_ForClass 👩‍🔬

Warning

Silverstan cannot resolve the type of a property fetch on SilverStripe\Core\Config\Config_ForClass, use SilverStripe\Core\Config\Config_ForClass::get() instead. See the rules overview.

cambis/silverstan 适用场景与选型建议

cambis/silverstan 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 151.34k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2024 年 10 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 cambis/silverstan 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 151.34k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 25
  • 依赖项目数: 104
  • 推荐数: 1

GitHub 信息

  • Stars: 6
  • Watchers: 2
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-21