Repository: phpDocumentor/TypeResolver Branch: 2.x Commit: b2abba83bb2d Files: 195 Total size: 1.5 MB Directory structure: gitextract_9s3rpj8r/ ├── .editorconfig ├── .git-blame-ignore-revs ├── .gitattributes ├── .github/ │ ├── dependabot.yml │ └── workflows/ │ ├── documentation.yaml │ └── integrate.yaml ├── .gitignore ├── .phpdoc/ │ └── template/ │ └── base.html.twig ├── .yamllint.yaml ├── LICENSE ├── Makefile ├── README.md ├── composer-require-checker.json ├── composer.json ├── docs/ │ ├── generics.rst │ ├── getting-started.rst │ ├── index.rst │ └── upgrade-v1-to-v2.rst ├── examples/ │ ├── 01-resolving-simple-types.php │ ├── 02-resolving-classes.php │ ├── 03-resolving-all-elements.php │ ├── 04-discovering-the-context-using-class-reflection.php │ ├── 05-discovering-the-context-using-method-reflection.php │ ├── 06-discovering-the-context-using-file-contents.php │ └── Classy.php ├── phpbench.json ├── phpcs.xml.dist ├── phpdoc.dist.xml ├── phpstan.neon ├── phpunit.xml.dist ├── psalm.xml ├── src/ │ ├── FqsenResolver.php │ ├── PseudoType.php │ ├── PseudoTypes/ │ │ ├── ArrayKey.php │ │ ├── ArrayShape.php │ │ ├── ArrayShapeItem.php │ │ ├── CallableArray.php │ │ ├── CallableString.php │ │ ├── ClassString.php │ │ ├── ClosedResource.php │ │ ├── Conditional.php │ │ ├── ConditionalForParameter.php │ │ ├── ConstExpression.php │ │ ├── EnumString.php │ │ ├── False_.php │ │ ├── FloatValue.php │ │ ├── Generic.php │ │ ├── HtmlEscapedString.php │ │ ├── IntMask.php │ │ ├── IntMaskOf.php │ │ ├── IntegerRange.php │ │ ├── IntegerValue.php │ │ ├── InterfaceString.php │ │ ├── KeyOf.php │ │ ├── ListShape.php │ │ ├── ListShapeItem.php │ │ ├── List_.php │ │ ├── LiteralString.php │ │ ├── LowercaseString.php │ │ ├── NegativeInteger.php │ │ ├── NeverReturn.php │ │ ├── NeverReturns.php │ │ ├── NoReturn.php │ │ ├── NonEmptyArray.php │ │ ├── NonEmptyList.php │ │ ├── NonEmptyLowercaseString.php │ │ ├── NonEmptyString.php │ │ ├── NonFalsyString.php │ │ ├── NonNegativeInteger.php │ │ ├── NonPositiveInteger.php │ │ ├── NonZeroInteger.php │ │ ├── NumericString.php │ │ ├── Numeric_.php │ │ ├── ObjectShape.php │ │ ├── ObjectShapeItem.php │ │ ├── OffsetAccess.php │ │ ├── OpenResource.php │ │ ├── PositiveInteger.php │ │ ├── PrivatePropertiesOf.php │ │ ├── PropertiesOf.php │ │ ├── ProtectedPropertiesOf.php │ │ ├── PublicPropertiesOf.php │ │ ├── Scalar.php │ │ ├── ShapeItem.php │ │ ├── StringValue.php │ │ ├── TraitString.php │ │ ├── True_.php │ │ ├── TruthyString.php │ │ └── ValueOf.php │ ├── Type.php │ ├── TypeResolver.php │ └── Types/ │ ├── AbstractList.php │ ├── AggregatedType.php │ ├── Array_.php │ ├── Boolean.php │ ├── CallableParameter.php │ ├── Callable_.php │ ├── Compound.php │ ├── Context.php │ ├── ContextFactory.php │ ├── Expression.php │ ├── Float_.php │ ├── Integer.php │ ├── Intersection.php │ ├── Iterable_.php │ ├── Mixed_.php │ ├── Never_.php │ ├── Null_.php │ ├── Nullable.php │ ├── Object_.php │ ├── Parent_.php │ ├── Resource_.php │ ├── Self_.php │ ├── Static_.php │ ├── String_.php │ ├── This.php │ └── Void_.php └── tests/ ├── benchmark/ │ ├── Assets/ │ │ └── mpdf.php │ ├── ContextFactoryBench.php │ ├── TypeResolverBench.php │ └── TypeResolverWithContextBench.php └── unit/ ├── CollectionResolverTest.php ├── FqsenResolverTest.php ├── IntegerRangeResolverTest.php ├── NumericResolverTest.php ├── PseudoTypes/ │ ├── ArrayKeyTest.php │ ├── ArrayShapeItemTest.php │ ├── ArrayShapeTest.php │ ├── CallableArrayTest.php │ ├── CallableStringTest.php │ ├── ClassStringTest.php │ ├── ClosedResourceTest.php │ ├── ConditionalForParameterTest.php │ ├── ConditionalTest.php │ ├── ConstExpressionTest.php │ ├── EnumStringTest.php │ ├── FalseTest.php │ ├── FloatValueTest.php │ ├── GenericTest.php │ ├── HtmlEscapedStringTest.php │ ├── IntMaskOfTest.php │ ├── IntMaskTest.php │ ├── IntegerRangeTest.php │ ├── IntegerValueTest.php │ ├── InterfaceStringTest.php │ ├── KeyOfTest.php │ ├── ListShapeTest.php │ ├── ListTest.php │ ├── LiteralStringTest.php │ ├── LowercaseStringTest.php │ ├── NegativeIntegerTest.php │ ├── NeverReturnTest.php │ ├── NeverReturnsTest.php │ ├── NoReturnTest.php │ ├── NonEmptyArrayTest.php │ ├── NonEmptyListTest.php │ ├── NonEmptyLowercaseStringTest.php │ ├── NonEmptyStringTest.php │ ├── NonFalsyStringTest.php │ ├── NonNegativeIntegerTest.php │ ├── NonPositiveIntegerTest.php │ ├── NonZeroIntegerTest.php │ ├── NumericStringTest.php │ ├── ObjectShapeTest.php │ ├── OffsetAccessTest.php │ ├── OpenResourceTest.php │ ├── PositiveIntegerTest.php │ ├── PrivatePropertiesOfTest.php │ ├── PropertiesOfTest.php │ ├── ProtectedPropertiesOfTest.php │ ├── PublicPropertiesOfTest.php │ ├── ScalarTest.php │ ├── StringValueTest.php │ ├── TraitStringTest.php │ ├── TrueTest.php │ ├── TruthyStringTest.php │ └── ValueOfTest.php ├── TypeResolverTest.php └── Types/ ├── ArrayTest.php ├── BooleanTest.php ├── CallableParameterTest.php ├── CallableTest.php ├── CompoundTest.php ├── ContextFactoryTest.php ├── ContextTest.php ├── IterableTest.php ├── NeverTest.php ├── NullableTest.php ├── ObjectTest.php ├── ParentTest.php ├── ResourceTest.php ├── SelfTest.php ├── StaticTest.php ├── ThisTest.php └── VoidTest.php ================================================ FILE CONTENTS ================================================ ================================================ FILE: .editorconfig ================================================ root = true [*] charset = utf-8 end_of_line = lf insert_final_newline = true indent_style = space indent_size = 4 trim_trailing_whitespace = true [*.yml] indent_size = 2 ================================================ FILE: .git-blame-ignore-revs ================================================ # CS: no whitespace before return type colon 6937c3bf9f57533b310c28c1758f4af6bd777f92 # CS: miscellaneous other whitespace fixes 92a009284c653e8576ee544191f2167f97c93aed # Fix style issues 0339a6b726db84790d94794e36f502c52e0e518a # CS fixes 2e32a6d48972b2c1976ed5d8967145b6cec4a4a9 # CS fixes e64841657859b6a3d1ecf01d6922ce1548ad7382 # fix code style 81dea8effddb6afc070552d95b8f5bbc2589fe10 # Style fixes 0cbfc1c752b9fae8c7a0be740e5d6af496463e1e # Apply doctrine coding standards d0e860a40b8199a0d5166023bc3764eed7af7d83 ================================================ FILE: .gitattributes ================================================ /.github/ export-ignore /examples/ export-ignore /docs/ export-ignore /tests/ export-ignore /.editorconfig export-ignore /.git-blame-ignore-revs export-ignore /.gitattributes export-ignore /.gitignore export-ignore /.yamllint.yaml export-ignore /Makefile export-ignore /composer-require-checker.json export-ignore /composer.lock export-ignore /phpbench.json export-ignore /phpcs.xml.dist export-ignore /phpstan.neon export-ignore /phpunit.xml.dist export-ignore /psalm.xml export-ignore /phpdoc.dist.xml export-ignore /.phpdoc/ export-ignore ================================================ FILE: .github/dependabot.yml ================================================ version: 2 updates: - package-ecosystem: "composer" directory: "/" schedule: interval: "daily" open-pull-requests-limit: 10 - package-ecosystem: "github-actions" directory: "/" schedule: interval: "weekly" ================================================ FILE: .github/workflows/documentation.yaml ================================================ # https://docs.github.com/en/actions name: "Documentation" on: # yamllint disable-line rule:truthy push: branches: - "2.x" workflow_dispatch: null jobs: run: name: "Documentation" uses: "phpDocumentor/.github/.github/workflows/documentation.yml@main" with: deploy: true component: "type-resolver" secrets: token: "${{ secrets.BOT_TOKEN }}" ================================================ FILE: .github/workflows/integrate.yaml ================================================ # https://docs.github.com/en/actions name: "Integrate" on: # yamllint disable-line rule:truthy push: branches: - "1.x" pull_request: null schedule: - cron: "0 14 * * 1" # Allow manually triggering the workflow. workflow_dispatch: null jobs: code-coverage: name: "Code Coverage" uses: "phpDocumentor/.github/.github/workflows/code-coverage.yml@main" with: php-extensions: "none, ctype, dom, json, mbstring, phar, simplexml, tokenizer, xml, xmlwriter, fileinfo, iconv" composer-root-version: "1.x-dev" coding-standards: name: "Coding Standards" uses: "phpDocumentor/.github/.github/workflows/coding-standards.yml@v0.9" with: php-extensions: "none, ctype, dom, json, mbstring, phar, simplexml, tokenizer, xml, xmlwriter, fileinfo, iconv" composer-root-version: "1.x-dev" dependency-analysis: name: "Dependency analysis" uses: "phpDocumentor/.github/.github/workflows/dependency-analysis.yml@v0.9" with: php-extensions: "none, ctype, dom, json, mbstring, phar, simplexml, tokenizer, xml, xmlwriter, fileinfo, iconv" composer-root-version: "1.x-dev" lint-root: name: "Lint root" uses: "phpDocumentor/.github/.github/workflows/lint.yml@main" with: composer-options: "--no-check-publish --ansi" static-analysis: name: "Static analysis" uses: "phpDocumentor/.github/.github/workflows/static-analysis.yml@v0.9" with: php-extensions: "none, ctype, dom, json, mbstring, phar, simplexml, tokenizer, xml, xmlwriter, fileinfo, pcntl, posix, iconv" composer-root-version: "1.x-dev" unit-tests: name: "Unit test" uses: "phpDocumentor/.github/.github/workflows/continuous-integration.yml@v0.9" with: php-extensions: "none, ctype, dom, json, mbstring, phar, simplexml, tokenizer, xml, xmlwriter, fileinfo, iconv" composer-root-version: "1.x-dev" upcoming-releases: true ================================================ FILE: .gitignore ================================================ # IDE Shizzle; it is recommended to use a global .gitignore for this but since this is an OSS project we want to make # it easy to contribute .idea .vscode /nbproject/private/ .buildpath .project .settings # No need to version the binary files of other tools bin/behat* bin/phpcs* bin/phpunit* bin/jsonlint* bin/validate-json* temp/ecs/* # Build folder and vendor folder are generated code; no need to version this build/ tools/ vendor/ *.phar # By default the phpunit.xml.dist is provided; you can override this using a local config file phpunit.xml .phpunit.result.cache ================================================ FILE: .phpdoc/template/base.html.twig ================================================ {% extends 'layout.html.twig' %} {% set topMenu = { "menu": [ { "name": "About", "url": "https://phpdoc.org/"}, { "name": "Components", "url": "https://phpdoc.org/components.html"}, { "name": "Documentation", "url": "https://docs.phpdoc.org/"}, ], "social": [ { "iconClass": "fab fa-mastodon", "url": "https://phpc.social/@phpdoc"}, { "iconClass": "fab fa-github", "url": "https://github.com/phpdocumentor/typeresolver"}, { "iconClass": "fas fa-envelope-open-text", "url": "https://github.com/orgs/phpDocumentor/discussions"} ] } %} ================================================ FILE: .yamllint.yaml ================================================ extends: "default" ignore: | .build/ .notes/ vendor/ rules: braces: max-spaces-inside-empty: 0 max-spaces-inside: 1 min-spaces-inside-empty: 0 min-spaces-inside: 1 brackets: max-spaces-inside-empty: 0 max-spaces-inside: 0 min-spaces-inside-empty: 0 min-spaces-inside: 0 colons: max-spaces-after: 1 max-spaces-before: 0 commas: max-spaces-after: 1 max-spaces-before: 0 min-spaces-after: 1 comments: ignore-shebangs: true min-spaces-from-content: 1 require-starting-space: true comments-indentation: "enable" document-end: present: false document-start: present: false indentation: check-multi-line-strings: false indent-sequences: true spaces: 2 empty-lines: max-end: 0 max-start: 0 max: 1 empty-values: forbid-in-block-mappings: true forbid-in-flow-mappings: true hyphens: max-spaces-after: 2 key-duplicates: "enable" key-ordering: "disable" line-length: "disable" new-line-at-end-of-file: "enable" new-lines: type: "unix" octal-values: forbid-implicit-octal: true quoted-strings: quote-type: "double" trailing-spaces: "enable" truthy: allowed-values: - "false" - "true" yaml-files: - "*.yaml" - "*.yml" ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2010 Mike van Riel Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: Makefile ================================================ .PHONY: help help: ## Displays this list of targets with descriptions @grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[32m%-30s\033[0m %s\n", $$1, $$2}' .PHONY: code-style code-style: docker run -it --rm -v${PWD}:/opt/project -w /opt/project phpdoc/phpcs-ga:latest -d memory_limit=1024M -s .PHONY: fix-code-style fix-code-style: docker run -it --rm -v${PWD}:/opt/project -w /opt/project phpdoc/phpcs-ga:latest phpcbf .PHONY: static-code-analysis static-code-analysis: vendor ## Runs a static code analysis with phpstan/phpstan and vimeo/psalm docker run -it --rm -v${PWD}:/opt/project -w /opt/project php:7.4 vendor/bin/phpstan --configuration=phpstan.neon docker run -it --rm -v${PWD}:/opt/project -w /opt/project php:7.4 vendor/bin/psalm .PHONY: test test: test-unit ## Runs all test suites with phpunit/phpunit docker run -it --rm -v${PWD}:/opt/project -w /opt/project php:7.4 vendor/bin/phpunit .PHONY: test-unit test-unit: ## Runs unit tests with phpunit/phpunit docker run -it --rm -v${PWD}:/opt/project -w /opt/project php:7.4 vendor/bin/phpunit --testsuite=unit .PHONY: dependency-analysis dependency-analysis: vendor ## Runs a dependency analysis with maglnet/composer-require-checker docker run -it --rm -v${PWD}:/opt/project -w /opt/project php:7.4 .phive/composer-require-checker check --config-file=/opt/project/composer-require-checker.json vendor: composer.json composer.lock composer validate --no-check-publish composer install --no-interaction --no-progress .PHONY: benchmark benchmark: docker run -it --rm -v${CURDIR}:/opt/project -w /opt/project php:7.4-cli vendor/bin/phpbench run .PHONY: pre-commit-test pre-commit-test: fix-code-style test code-style static-code-analysis .PHONY: docs docs: ## Generate documentation with phpDocumentor docker run -it --rm -v${CURDIR}:/opt/project -w /opt/project phpdoc/phpdoc:3 ================================================ FILE: README.md ================================================ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) ![](https://github.com/phpdocumentor/typeresolver/workflows/Qa%20workflow/badge.svg?branch=1.x) [![Coveralls Coverage](https://img.shields.io/coveralls/github/phpDocumentor/TypeResolver.svg)](https://coveralls.io/github/phpDocumentor/TypeResolver?branch=1.x) [![Scrutinizer Code Coverage](https://img.shields.io/scrutinizer/coverage/g/phpDocumentor/TypeResolver.svg)](https://scrutinizer-ci.com/g/phpDocumentor/TypeResolver/?branch=1.x) [![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/phpDocumentor/TypeResolver.svg)](https://scrutinizer-ci.com/g/phpDocumentor/TypeResolver/?branch=1.x) ![Packagist Version](https://img.shields.io/packagist/v/phpdocumentor/type-resolver?label=Packagist%20stable) ![Packagist Version](https://img.shields.io/packagist/vpre/phpdocumentor/type-resolver?label=Packagist%20unstable) TypeResolver and FqsenResolver ============================== The specification on types in DocBlocks (PSR-5) describes various keywords and special constructs but also how to statically resolve the partial name of a Class into a Fully Qualified Class Name (FQCN). PSR-5 also introduces an additional way to describe deeper elements than Classes, Interfaces and Traits called the Fully Qualified Structural Element Name (FQSEN). Using this it is possible to refer to methods, properties and class constants but also functions and global constants. This package provides two Resolvers that are capable of 1. Returning a series of Value Object for given expression while resolving any partial class names, and 2. Returning an FQSEN object after resolving any partial Structural Element Names into Fully Qualified Structural Element names. ## Installing The easiest way to install this library is with [Composer](https://getcomposer.org) using the following command: $ composer require phpdocumentor/type-resolver ## Examples Ready to dive in and don't want to read through all that text below? Just consult the [examples](examples) folder and check which type of action that your want to accomplish. ## On Types and Element Names This component can be used in one of two ways 1. To resolve a Type or 2. To resolve a Fully Qualified Structural Element Name The big difference between these two is in the number of things it can resolve. The TypeResolver can resolve: - a php primitive or pseudo-primitive such as a string or void (`@var string` or `@return void`). - a composite such as an array of string (`@var string[]`). - a compound such as a string or integer (`@var string|integer`). - an array expression (`@var (string|TypeResolver)[]`) - an object or interface such as the TypeResolver class (`@var TypeResolver` or `@var \phpDocumentor\Reflection\TypeResolver`) > please note that if you want to pass partial class names that additional steps are necessary, see the > chapter `Resolving partial classes and FQSENs` for more information. Where the FqsenResolver can resolve: - Constant expressions (i.e. `@see \MyNamespace\MY_CONSTANT`) - Function expressions (i.e. `@see \MyNamespace\myFunction()`) - Class expressions (i.e. `@see \MyNamespace\MyClass`) - Interface expressions (i.e. `@see \MyNamespace\MyInterface`) - Trait expressions (i.e. `@see \MyNamespace\MyTrait`) - Class constant expressions (i.e. `@see \MyNamespace\MyClass::MY_CONSTANT`) - Property expressions (i.e. `@see \MyNamespace\MyClass::$myProperty`) - Method expressions (i.e. `@see \MyNamespace\MyClass::myMethod()`) ## Resolving a type In order to resolve a type you will have to instantiate the class `\phpDocumentor\Reflection\TypeResolver` and call its `resolve` method like this: ```php $typeResolver = new \phpDocumentor\Reflection\TypeResolver(); $type = $typeResolver->resolve('string|integer'); ``` In this example you will receive a Value Object of class `\phpDocumentor\Reflection\Types\Compound` that has two elements, one of type `\phpDocumentor\Reflection\Types\String_` and one of type `\phpDocumentor\Reflection\Types\Integer`. The real power of this resolver is in its capability to expand partial class names into fully qualified class names; but in order to do that we need an additional `\phpDocumentor\Reflection\Types\Context` class that will inform the resolver in which namespace the given expression occurs and which namespace aliases (or imports) apply. ### Resolving nullable types Php 7.1 introduced nullable types e.g. `?string`. Type resolver will resolve the original type without the nullable notation `?` just like it would do without the `?`. After that the type is wrapped in a `\phpDocumentor\Reflection\Types\Nullable` object. The `Nullable` type has a method to fetch the actual type. ## Resolving an FQSEN A Fully Qualified Structural Element Name is a reference to another element in your code bases and can be resolved using the `\phpDocumentor\Reflection\FqsenResolver` class' `resolve` method, like this: ```php $fqsenResolver = new \phpDocumentor\Reflection\FqsenResolver(); $fqsen = $fqsenResolver->resolve('\phpDocumentor\Reflection\FqsenResolver::resolve()'); ``` In this example we resolve a Fully Qualified Structural Element Name (meaning that it includes the full namespace, class name and element name) and receive a Value Object of type `\phpDocumentor\Reflection\Fqsen`. The real power of this resolver is in its capability to expand partial element names into Fully Qualified Structural Element Names; but in order to do that we need an additional `\phpDocumentor\Reflection\Types\Context` class that will inform the resolver in which namespace the given expression occurs and which namespace aliases (or imports) apply. ## Resolving partial Classes and Structural Element Names Perhaps the best feature of this library is that it knows how to resolve partial class names into fully qualified class names. For example, you have this file: ```php namespace My\Example; use phpDocumentor\Reflection\Types; class Classy { /** * @var Types\Context * @see Classy::otherFunction() */ public function __construct($context) {} public function otherFunction(){} } ``` Suppose that you would want to resolve (and expand) the type in the `@var` tag and the element name in the `@see` tag. For the resolvers to know how to expand partial names you have to provide a bit of _Context_ for them by instantiating a new class named `\phpDocumentor\Reflection\Types\Context` with the name of the namespace and the aliases that are in play. ### Creating a Context You can do this by manually creating a Context like this: ```php $context = new \phpDocumentor\Reflection\Types\Context( '\My\Example', [ 'Types' => '\phpDocumentor\Reflection\Types'] ); ``` Or by using the `\phpDocumentor\Reflection\Types\ContextFactory` to instantiate a new context based on a Reflector object or by providing the namespace that you'd like to extract and the source code of the file in which the given type expression occurs. ```php $contextFactory = new \phpDocumentor\Reflection\Types\ContextFactory(); $context = $contextFactory->createFromReflector(new ReflectionMethod('\My\Example\Classy', '__construct')); ``` or ```php $contextFactory = new \phpDocumentor\Reflection\Types\ContextFactory(); $context = $contextFactory->createForNamespace('\My\Example', file_get_contents('My/Example/Classy.php')); ``` ### Using the Context After you have obtained a Context it is just a matter of passing it along with the `resolve` method of either Resolver class as second argument and the Resolvers will take this into account when resolving partial names. To obtain the resolved class name for the `@var` tag in the example above you can do: ```php $typeResolver = new \phpDocumentor\Reflection\TypeResolver(); $type = $typeResolver->resolve('Types\Context', $context); ``` When you do this you will receive an object of class `\phpDocumentor\Reflection\Types\Object_` for which you can call the `getFqsen` method to receive a Value Object that represents the complete FQSEN. So that would be `phpDocumentor\Reflection\Types\Context`. > Why is the FQSEN wrapped in another object `Object_`? > > The resolve method of the TypeResolver only returns object with the interface `Type` and the FQSEN is a common type that does not represent a Type. Also: in some cases a type can represent an "Untyped Object", meaning that it is an object (signified by the `object` keyword) but does not refer to a specific element using an FQSEN. Another example is on how to resolve the FQSEN of a method as can be seen with the `@see` tag in the example above. To resolve that you can do the following: ```php $fqsenResolver = new \phpDocumentor\Reflection\FqsenResolver(); $type = $fqsenResolver->resolve('Classy::otherFunction()', $context); ``` Because Classy is a Class in the current namespace its FQSEN will have the `My\Example` namespace and by calling the `resolve` method of the FQSEN Resolver you will receive an `Fqsen` object that refers to `\My\Example\Classy::otherFunction()`. ================================================ FILE: composer-require-checker.json ================================================ { "symbol-whitelist" : [ "null", "true", "false", "static", "self", "parent", "array", "string", "int", "float", "bool", "iterable", "callable", "void", "object", "XSLTProcessor", "PHPStan\\PhpDocParser\\ParserConfig", "T_NAME_QUALIFIED", "T_NAME_FULLY_QUALIFIED" ], "php-core-extensions" : [ "Core", "pcre", "Reflection", "tokenizer", "SPL", "standard" ] } ================================================ FILE: composer.json ================================================ { "name": "phpdocumentor/type-resolver", "description": "A PSR-5 based resolver of Class names, Types and Structural Element Names", "type": "library", "license": "MIT", "authors": [ { "name": "Mike van Riel", "email": "me@mikevanriel.com" } ], "require": { "php": "^7.4 || ^8.0", "phpdocumentor/reflection-common": "^2.0", "phpstan/phpdoc-parser": "^2.0", "doctrine/deprecations": "^1.0" }, "require-dev": { "ext-tokenizer": "*", "phpunit/phpunit": "^9.5", "phpstan/phpstan": "^2.1", "phpstan/phpstan-phpunit": "^2.0", "phpstan/extension-installer": "^1.4", "phpbench/phpbench": "^1.2", "psalm/phar": "^4" }, "autoload": { "psr-4": { "phpDocumentor\\Reflection\\": "src" } }, "autoload-dev": { "psr-4": { "phpDocumentor\\Reflection\\": ["tests/unit", "tests/benchmark"] } }, "extra": { "branch-alias": { "dev-1.x": "1.x-dev", "dev-2.x": "2.x-dev" } }, "config": { "platform": { "php": "7.4.0" }, "allow-plugins": { "phpstan/extension-installer": true } } } ================================================ FILE: docs/generics.rst ================================================ ======== Generics ======== This project is capable of parsing generics notation as used by PHPStan. But it has some limitations, in regards to PHPStan. The main difference is that PHPStan does scan your whole codebase to find out what types are used in generics, while this library only parses the types as they are given to it. This means that if you use a generic type like. .. code:: php namespace MyApp; /** * @template T of Item */ class Collection { /** * @return T[] */ public function getItems() : array { // ... } } The type resolver will not be able to determine what ``T`` is. In fact there is no difference between ``T`` and any other relative used classname like ``Item``. The resolver will handle ``T`` as a normal class name. In this example it will resolve ``T`` to ``\MyApp\T``. ================================================ FILE: docs/getting-started.rst ================================================ =============== Getting started =============== On this page you will find a brief introduction on how to use the TypeResolver in your project. Installation ============ The TypeResolver is available on Packagist and can be installed using Composer: .. code:: bash composer require phpdocumentor/type-resolver General usage =========== After you installed the TypeResolver you can use it in your project. This can be done by creating a new instance of the :php:class:`\phpDocumentor\Reflection\TypeResolver` class and calling :php:method:`\phpDocumentor\Reflection\TypeResolver::resolve()` with the type you want to resolve. .. code:: php $typeResolver = new \phpDocumentor\Reflection\TypeResolver(); $type = $typeResolver->resolve('string'); echo get_class($type); // phpDocumentor\Reflection\Types\String_ The real power of this resolver is in its capability to expand partial class names into fully qualified class names; but in order to do that we need an additional :php:class:`\phpDocumentor\Reflection\Types\Context` class that will inform the resolver in which namespace the given expression occurs and which namespace aliases (or imports) apply. Read more about the Context class in the next section. ================================================ FILE: docs/index.rst ================================================ ============= Type resolver ============= This project part of the phpDocumentor project. It is capable of creating an object structure of the type specifications found in the PHPDoc blocks of a project. This can be useful for static analysis of a project or other behavior that requires knowledge of the types used in a project like automatically build forms. This project aims to cover all types that are available in PHPDoc and PHP itself. And is open for extension by third party developers. .. toctree:: :maxdepth: 2 :hidden: index getting-started generics upgrade-v1-to-v2 ================================================ FILE: docs/upgrade-v1-to-v2.rst ================================================ ==================== Upgrade to Version 2 ==================== Version 2 of the Type Resolver introduces several breaking changes and new features. This guide will help you upgrade your codebase to be compatible with the latest version. The usage of the TypeResolver remains the same. However, some classes have been moved or replaced, and the minimum PHP version requirement has been raised. PHP Version ----------- Version 2 requires PHP 7.4 or higher. We have been supporting PHP 7.3 in version 1, but due to changing constraints in our dependencies, we have had to raise the minimum PHP version. At the moment of writing this, PHP 7.3 is used by 2% of all installations of this package according to Packagist. We believe this is a reasonable trade-off to ensure we can continue to deliver new features and improvements. Moved classes ------------- - ``phpDocumentor\Reflection\Types\InterfaceString`` => :php:class:`phpDocumentor\Reflection\PseudoTypes\InterfaceString` - ``phpDocumentor\Reflection\Types\ClassString`` => :php:class:`phpDocumentor\Reflection\PseudoTypes\ClassString` - ``phpDocumentor\Reflection\Types\ArrayKey`` => :php:class:`phpDocumentor\Reflection\PseudoTypes\ArrayKey` - ``phpDocumentor\Reflection\Types\True_`` => :php:class:`phpDocumentor\Reflection\PseudoTypes\True_` - ``phpDocumentor\Reflection\Types\False_`` => :php:class:`phpDocumentor\Reflection\PseudoTypes\False_` Replaced classes ----------------- - ``phpDocumentor\Reflection\Types\Collection`` => :php:class:`phpDocumentor\Reflection\PseudoTypes\Generic` Since the introduction of generics in PHP this library was not capable of parsing them correctly. The old Collection was blocking the use of generics. The new Generic type is a representation of generics like supported in the eco system. Changed implementations ----------------------- :php:class:`phpDocumentor\Reflection\PseudoTypes\InterfaceString`, :php:class:`phpDocumentor\Reflection\PseudoTypes\ClassString` and :php:class:`phpDocumentor\Reflection\PseudoTypes\TraitString` are no longer returning a :php:class:`phpDocumentor\Reflection\Fqsen` since support for generics these classes can have type arguments like any other generic. ================================================ FILE: examples/01-resolving-simple-types.php ================================================ resolve('string|integer')); // Will return the string "string|int" var_dump((string)$typeResolver->resolve('string|integer')); ================================================ FILE: examples/02-resolving-classes.php ================================================ 'Mockery' ]); var_dump((string)$typeResolver->resolve('Types\Resolver|m\MockInterface', $context)); ================================================ FILE: examples/03-resolving-all-elements.php ================================================ resolve('Types\Resolver::resolveFqsen()', $context)); // Property named: \phpDocumentor\Types\Types\Resolver::$keyWords var_dump((string)$fqsenResolver->resolve('Types\Resolver::$keyWords', $context)); ================================================ FILE: examples/04-discovering-the-context-using-class-reflection.php ================================================ createFromReflector(new ReflectionClass('My\\Example\\Classy')); // Class named: \phpDocumentor\Reflection\Types\Resolver var_dump((string)$typeResolver->resolve('Types\Resolver', $context)); // String var_dump((string)$typeResolver->resolve('string', $context)); // Property named: \phpDocumentor\Reflection\Types\Resolver::$keyWords var_dump((string)$fqsenResolver->resolve('Types\Resolver::$keyWords', $context)); // Class named: \My\Example\string // - Shows the difference between the FqsenResolver and TypeResolver; the FqsenResolver will assume // that the given value is not a type but most definitely a reference to another element. This is // because conflicts between type keywords and class names can exist and if you know a reference // is not a type but an element you can force that keywords are resolved. var_dump((string)$fqsenResolver->resolve('string', $context)); ================================================ FILE: examples/05-discovering-the-context-using-method-reflection.php ================================================ createFromReflector(new ReflectionMethod('My\\Example\\Classy', '__construct')); // Class named: \phpDocumentor\Reflection\Types\Resolver var_dump((string)$typeResolver->resolve('Types\Resolver', $context)); // String var_dump((string)$typeResolver->resolve('string', $context)); // Property named: \phpDocumentor\Reflection\Types\Resolver::$keyWords var_dump((string)$fqsenResolver->resolve('Types\Resolver::$keyWords', $context)); // Class named: \My\Example\string // - Shows the difference between the FqsenResolver and TypeResolver; the FqsenResolver will assume // that the given value is not a type but most definitely a reference to another element. This is // because conflicts between type keywords and class names can exist and if you know a reference // is not a type but an element you can force that keywords are resolved. var_dump((string)$fqsenResolver->resolve('string', $context)); ================================================ FILE: examples/06-discovering-the-context-using-file-contents.php ================================================ createForNamespace('My\Example', file_get_contents(__DIR__ . '/Classy.php')); // Class named: \phpDocumentor\Reflection\Types\Resolver var_dump((string)$typeResolver->resolve('Types\Resolver', $context)); // String var_dump((string)$typeResolver->resolve('string', $context)); // Property named: \phpDocumentor\Reflection\Types\Resolver::$keyWords var_dump((string)$fqsenResolver->resolve('Types\Resolver::$keyWords', $context)); ================================================ FILE: examples/Classy.php ================================================ The coding standard for this library. src tests/unit */tests/unit/Types/ContextFactoryTest\.php */src/*/Abstract*\.php */src/PseudoTypes/False_\.php */src/PseudoTypes/True_\.php */src/Types/ContextFactory\.php */src/Types/ContextFactory\.php ================================================ FILE: phpdoc.dist.xml ================================================ Type Resolver build/docs latest src/ api php template template-extends template-implements extends implements phpDocumentor docs guides