Skip to content

4. Examples

Marco Muths edited this page Mar 25, 2017 · 19 revisions

Usage-Graph on package level

This illustrates an Usage-Graph with aggregated dependencies to package level, which includes a cycle violation between PackageA, PackageB and PackageC. Aggregation is reached by visitorOptions configuration. sliceLength leads to aggregate found namespaces of dependencies.

Used configuration

mode: 'usage'
source: './examples'
filePattern: '*.php'
ignore: 'tests'
formatter: 'PhpDA\Writer\Strategy\Svg'
target: './examples/packages.svg'
visitor:
  - PhpDA\Parser\Visitor\TagCollector
  - PhpDA\Parser\Visitor\SuperglobalCollector
visitorOptions:
  PhpDA\Parser\Visitor\Required\DeclaredNamespaceCollector: {minDepth: 2, sliceLength: 1}
  PhpDA\Parser\Visitor\Required\MetaNamespaceCollector: {minDepth: 2, sliceLength: 1}
  PhpDA\Parser\Visitor\Required\UsedNamespaceCollector: {minDepth: 2, sliceLength: 1}
  PhpDA\Parser\Visitor\TagCollector: {minDepth: 2, sliceLength: 1}

Usage-Graph on layer level with ReferenceValidator

It illustrates an Usage-Graph with aggregated dependencies to layer level. Each namespace is grouped to their package. This analysis is performed with a ReferenceValidator (see used ExampleValidator) to verify dependecies. It includes a violation between Controller and Mapper. A controller should not permitted to access a mapper directly. For demonstration purposes it also incudes a violation between PackageB and PackageC which is detected in Service-Layer.

Used configuration

mode: 'usage'
source: './examples/Project/PackageB'
filePattern: '*.php'
ignore: 'tests'
formatter: 'PhpDA\Writer\Strategy\Svg'
target: './examples/layers.svg'
referenceValidator: 'MyDomain\Plugin\ReferenceValidator'
groupLength: 1
visitor:
  - PhpDA\Parser\Visitor\TagCollector
  - PhpDA\Parser\Visitor\SuperglobalCollector
visitorOptions:
  PhpDA\Parser\Visitor\Required\DeclaredNamespaceCollector: {minDepth: 2, sliceLength: 2}
  PhpDA\Parser\Visitor\Required\MetaNamespaceCollector: {minDepth: 2, sliceLength: 2}
  PhpDA\Parser\Visitor\Required\UsedNamespaceCollector: {minDepth: 2, sliceLength: 2}
  PhpDA\Parser\Visitor\TagCollector: {minDepth: 2, sliceLength: 2}
classMap:
  MyDomain\Plugin\ReferenceValidator: '../../../_data/Plugin/ReferenceValidator.php'

Usage-Graph on class level with complex cycle

It illustrates a complex cycle violation inside a module by..

FilterA->FilterB->FilterC->FilterD->FilterA.

You can also see that each filter implements FilterInterface.

Used configuration

mode: 'usage'
source: './examples/Project/PackageC/Filter'
filePattern: '*.php'
formatter: 'PhpDA\Writer\Strategy\Svg'
target: './examples/complex-cycle.svg'
groupLength: 3

Inheritance-Graph

Here you can see an inheritance structure. Each inheritance dependency is drawn by a dashed arrow. Except in case of aggregation of dependencies dashed arrows are always used for an inherit dependency. An inheritance dependency can be an implementation, a class-extending or a trait-use.

Used configuration

mode: 'inheritance'
source: './src/Writer'
filePattern: '*.php'
ignore: 'tests'
formatter: 'PhpDA\Writer\Strategy\Svg'
target: './examples/inheritance.svg'
visitor:
  - PhpDA\Parser\Visitor\TagCollector
  - PhpDA\Parser\Visitor\SuperglobalCollector
visitorOptions:
  PhpDA\Parser\Visitor\Required\DeclaredNamespaceCollector: {minDepth: 2}
  PhpDA\Parser\Visitor\Required\MetaNamespaceCollector: {minDepth: 2}
  PhpDA\Parser\Visitor\TagCollector: {minDepth: 2}

Call-Graph

This graph illustrates only call dependencies. A call can be a method parameter, a method return-value or an instance creation.

Used configuration

mode: 'call'
source: './src/Plugin'
filePattern: '*.php'
ignore: 'tests'
formatter: 'PhpDA\Writer\Strategy\Svg'
target: './examples/call.svg'
visitor:
  - PhpDA\Parser\Visitor\TagCollector
  - PhpDA\Parser\Visitor\SuperglobalCollector

Usage-Graph on class level

It illustrates an Usage-Graph of dependencies on class level. You can see that functions are also to be subject to namespacing.

Used configuration

mode: 'usage'
source: './examples/Project/PackageA/Service'
filePattern: 'Command.php'
ignore: 'tests'
formatter: 'PhpDA\Writer\Strategy\Svg'
target: './examples/class.svg'
visitor:
  - PhpDA\Parser\Visitor\TagCollector
  - PhpDA\Parser\Visitor\SuperglobalCollector

Usage-Graph on package level with a NamespaceFilter

It's like the first example: This illustrates an Usage-Graph with aggregated dependencies to package level, which includes a cycle violation between PackageA, PackageB and PackageC. Aggregation is reached by visitorOptions configuration. sliceLength leads to aggregate found namespaces of dependencies.

We are using a NamespaceFilter (see used ExampleFilter) to convert Zend namespaces to Framework just for demonstration purpose.

Used configuration

mode: 'usage'
source: '../../Project'
filePattern: '*.php'
ignore: 'tests'
formatter: 'PhpDA\Writer\Strategy\Svg'
target: '../../../_output/svg/namespace-filter.svg'
namespaceFilter: 'MyDomain\Plugin\NamespaceFilter'
visitor:
  - PhpDA\Parser\Visitor\TagCollector
  - PhpDA\Parser\Visitor\SuperglobalCollector
visitorOptions:
  PhpDA\Parser\Visitor\Required\DeclaredNamespaceCollector: {minDepth: 2, sliceLength: 1}
  PhpDA\Parser\Visitor\Required\MetaNamespaceCollector: {minDepth: 2, sliceLength: 1}
  PhpDA\Parser\Visitor\Required\UsedNamespaceCollector: {minDepth: 2, sliceLength: 1}
  PhpDA\Parser\Visitor\TagCollector: {minDepth: 2, sliceLength: 1}
classMap:
  MyDomain\Plugin\NamespaceFilter: '../../../_data/Plugin/NamespaceFilter.php'

Usage-Graph of an entire project

It illustrates a Usage-Graph of dependencies for an entire project. Its possible to perform this but you also see that is not really visitable.

Used configuration

mode: 'usage'
source: './examples'
filePattern: '*.php'
ignore: 'tests'
formatter: 'PhpDA\Writer\Strategy\Svg'
target: './examples/total.svg'
groupLength: 1
visitor:
  - PhpDA\Parser\Visitor\TagCollector
  - PhpDA\Parser\Visitor\SuperglobalCollector

It illustrates the compatibility to the new PHP7 Return Type Declarations feature based on ReturnValue Stub.

Used configuration

mode: 'usage'
source: './examples/Project/tests/stubs'
filePattern: 'ReturnType.php'
formatter: 'PhpDA\Writer\Strategy\Svg'
target: './examples/return-type.svg'

Parsing PHP7 Anonymous Classes

It illustrates the compatibility to the new PHP7 Anonymous Classes feature based on AnonymousClass Stub.

Used configuration

mode: 'usage'
source: './examples/Project/tests/stubs'
filePattern: 'AnonymousClass.php'
formatter: 'PhpDA\Writer\Strategy\Svg'
target: './examples/anonymous-class.svg'

Parsing DocBlock Tags

It illustrates the capabilities of the PhpDA\Parser\Visitor\TagCollector for parsing PhpDocBlocks. Just check the DocBlockTag Stub for this demonstration.

Used configuration

mode: 'usage'
source: './examples/Project/tests/stubs'
filePattern: 'DocBlockTag.php'
formatter: 'PhpDA\Writer\Strategy\Svg'
target: './examples/docblock.svg'
visitor:
  - PhpDA\Parser\Visitor\TagCollector

Superglobals as dependency

Demonstration of the capabilities of the PhpDA\Parser\Visitor\SuperglobalCollector based on the SuperGlobalAccess Stub.

Used configuration

mode: 'usage'
source: './examples/Project/tests/stubs'
filePattern: 'SuperGlobalAccess.php'
formatter: 'PhpDA\Writer\Strategy\Svg'
target: './examples/superglobals.svg'
visitor:
  - PhpDA\Parser\Visitor\SuperglobalCollector

Constants as dependency

Demonstration for collecting Constants as dependencies based on the Constant Stub.

Used configuration

mode: 'usage'
source: './examples/Project/tests/stubs'
filePattern: 'Constant.php'
formatter: 'PhpDA\Writer\Strategy\Svg'
target: './examples/constant.svg'

Namespaced strings as dependency

Demonstration of the capabilities of the PhpDA\Parser\Visitor\NamespacedStringCollector and PhpDA\Parser\Visitor\IocContainerAccessorCollector based on the NamespacedString Stub. Those dependecies are approximated and detected in strings. Of course they are not resolvable to real namespaces, therefore they are yellow colored and must be checked manually.

Used configuration

mode: 'usage'
source: './examples/Project/tests/stubs'
filePattern: 'NamespacedString.php'
formatter: 'PhpDA\Writer\Strategy\Svg'
target: './examples/namespaced-strings.svg'
visitor:
  - PhpDA\Parser\Visitor\NamespacedStringCollector
  - PhpDA\Parser\Visitor\IocContainerAccessorCollector

Collecting unsupported statements

Demonstration of the capabilities of the PhpDA\Parser\Visitor\Unsupported*Collector based on the UnsupportedStatement Stub. All found unsupported statements are red colored, because they are not resolvable and must be checked manually.

Used configuration

mode: 'usage'
source: './examples/Project/tests/stubs'
filePattern: 'UnsupportedStatement.php'
formatter: 'PhpDA\Writer\Strategy\Svg'
target: './examples/unsupported-stmt.svg'
visitor:
  - PhpDA\Parser\Visitor\UnsupportedEvalCollector
  - PhpDA\Parser\Visitor\UnsupportedFuncCollector
  - PhpDA\Parser\Visitor\UnsupportedVarCollector
  - PhpDA\Parser\Visitor\UnsupportedGlobalCollector