承接 voku/simple-php-code-parser 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

voku/simple-php-code-parser

最新稳定版本:0.21.0

Composer 安装命令:

composer require voku/simple-php-code-parser

包简介

Get a simple data structure from your php code.

README 文档

README

Build Status Codacy Badge Latest Stable Version Total Downloads License Donate to this project using Paypal Donate to this project using Patreon

❤ Simple PHP Code Parser

You can simply scan a string, a file or a full directory and you can see a simple data structure from your php code.

  • Classes (PHPClass)
  • Properties (PHPProperty)
  • Constants (PHPConst)
  • Methods (PHPMethod)
  • Interfaces (PHPInterface)
  • Traits (PHPTrait)
  • Enums (PHPEnum)
  • Functions (PHPFunction)
  • Parameter (PHPParameter)
  • Attributes (PHPAttribute)

This code is forked from JetBrains/phpstorm-stubs but you can't use the classes from "phpstorm-stubs" directly, because they are in a test namespace and the autoloader is "autoload-dev", so here is a extended version.

We will use:

Requirements

  • PHP >= 8.1

Supported PHP Features

Runtime support for this library is PHP 8.1+, while the parser test suite validates analyzable source syntax from PHP 5.3 through PHP 8.5.

Legacy source syntax that can still be analyzed

Source Syntax Generation Representative coverage
PHP 5.3 namespaces, closures with use, array() syntax
PHP 5.4 traits, callable, short arrays
PHP 5.5 generators / yield, finally
PHP 5.6 variadics, argument unpacking-ready syntax, constant arrays
PHP 7.0 scalar parameter types, return types, anonymous classes
PHP 7.1 nullable types, iterable, void
PHP 7.2 object type
PHP 7.3 trailing commas in calls
PHP 7.4 typed properties, arrow functions

Modern source syntax coverage

Feature PHP Version Supported
Attributes (class, method, property, parameter, constant) 8.0+
Constructor property promotion 8.0+
Union types 8.0+
Named arguments 8.0+
Match expressions 8.0+
Nullsafe operator 8.0+
Enums (unit, string-backed, int-backed) 8.1+
Readonly properties 8.1+
Intersection types 8.1+
never return type 8.1+
First-class callable syntax 8.1+
Readonly classes 8.2+
DNF types 8.2+
Standalone true, false, null types 8.2+
Trait constants 8.2+
Typed class constants 8.3+
#[\Override] attribute detection 8.3+
Property hooks / asymmetric visibility 8.4+

Install via "composer require"

composer require voku/simple-php-code-parser

Quick Start

Parse a string:

$code = '
<?php
namespace voku\tests;
class SimpleClass {}
$obja = new class() {};
$objb = new class {};
class AnotherClass {}
';
$phpCode = \voku\SimplePhpParser\Parsers\PhpCodeParser::getFromString($code);
$phpClasses = $phpCode->getClasses();

var_dump($phpClasses['voku\tests\SimpleClass']); // "PHPClass"-object

Parse one class:

$phpCode = \voku\SimplePhpParser\Parsers\PhpCodeParser::getFromClassName(Dummy::class);
$phpClasses = $phpCode->getClasses();

var_dump($phpClasses[Dummy::class]); // "PHPClass"-object

var_dump($phpClasses[Dummy::class]->methods); // "PHPMethod[]"-objects

var_dump($phpClasses[Dummy::class]->methods['withoutPhpDocParam']); // "PHPMethod"-object

var_dump($phpClasses[Dummy::class]->methods['withoutPhpDocParam']->parameters); // "PHPParameter[]"-objects

var_dump($phpClasses[Dummy::class]->methods['withoutPhpDocParam']->parameters['useRandInt']); // "PHPParameter"-object

var_dump($phpClasses[Dummy::class]->methods['withoutPhpDocParam']->parameters['useRandInt']->type); // "bool"

Parse one file:

$phpCode = \voku\SimplePhpParser\Parsers\PhpCodeParser::getPhpFiles(__DIR__ . '/Dummy.php');
$phpClasses = $phpCode->getClasses();

var_dump($phpClasses[Dummy::class]); // "PHPClass"-object

var_dump($phpClasses[Dummy::class]->methods); // "PHPMethod[]"-objects

var_dump($phpClasses[Dummy::class]->methods['withoutPhpDocParam']); // "PHPMethod"-object

var_dump($phpClasses[Dummy::class]->methods['withoutPhpDocParam']->parameters); // "PHPParameter[]"-objects

var_dump($phpClasses[Dummy::class]->methods['withoutPhpDocParam']->parameters['useRandInt']); // "PHPParameter"-object

var_dump($phpClasses[Dummy::class]->methods['withoutPhpDocParam']->parameters['useRandInt']->type); // "bool"

Parse many files:

$phpCode = \voku\SimplePhpParser\Parsers\PhpCodeParser::getPhpFiles(__DIR__ . '/src');
$phpClasses = $phpCode->getClasses();

var_dump($phpClasses[Dummy::class]); // "PHPClass"-object

Unified metadata API:

$phpCode = \voku\SimplePhpParser\Parsers\PhpCodeParser::getPhpFiles(__DIR__ . '/src');

$phpClasses = $phpCode->getClasses();
$phpInterfaces = $phpCode->getInterfaces();
$phpTraits = $phpCode->getTraits();
$phpEnums = $phpCode->getEnums();
$phpFunctions = $phpCode->getFunctions();
$phpConstants = $phpCode->getConstants();

$functionInfo = $phpCode->getFunctionsInfo();
$methodInfo = $phpClasses[MyService::class]->getMethodsInfo();
$propertyInfo = $phpClasses[MyService::class]->getPropertiesInfo();

The library is meant to be the simple integration layer that other tools can call instead of wiring together nikic/php-parser, phpstan/phpdoc-parser, phpDocumentor, and native reflection themselves. The test suite validates analyzable PHP source from PHP 5.3 through PHP 8.5, including legacy syntax generations and modern type declarations / metadata such as attributes, enums, readonly constructs, typed constants, and property hooks.

Access enums:

$phpCode = \voku\SimplePhpParser\Parsers\PhpCodeParser::getPhpFiles(__DIR__ . '/src');
$phpEnums = $phpCode->getEnums();
// PHPEnum objects with scalarType, cases, methods, constants, attributes

Access attributes:

$phpCode = \voku\SimplePhpParser\Parsers\PhpCodeParser::getPhpFiles(__DIR__ . '/src');
$phpClasses = $phpCode->getClasses();
$class = $phpClasses['MyClass'];

// Class-level attributes
foreach ($class->attributes as $attr) {
    echo $attr->name;          // e.g. "MyAttribute"
    print_r($attr->arguments); // constructor arguments (array)
}

// Method/property/parameter attributes work the same way
foreach ($class->methods['myMethod']->attributes as $attr) { ... }
foreach ($class->properties['myProp']->attributes as $attr) { ... }
foreach ($class->methods['myMethod']->parameters['param']->attributes as $attr) { ... }

Support

For support and donations please visit GitHub | Issues | PayPal | Patreon.

For status updates and release announcements please visit Releases | Patreon.

For professional support please contact me.

Thanks

  • Thanks to GitHub (Microsoft) for hosting the code and a good infrastructure including Issues-Management, etc.
  • Thanks to IntelliJ as they make the best IDEs for PHP and they gave me an open source license for PhpStorm!
  • Thanks to PHPStan && Psalm for really great Static analysis tools and for discover bugs in the code!

统计信息

  • 总下载量: 7.18k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 48
  • 点击次数: 3
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2020-05-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固