Skip to content

Commit

Permalink
add method-doc-block-generator
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinDev committed Nov 1, 2023
1 parent a3d7015 commit e188d3a
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/split-monorepo.yaml
Expand Up @@ -18,6 +18,7 @@ jobs:
fail-fast: false
matrix:
package:
- method-doc-block-generator
- crawler
- curl
- extractor
Expand Down
22 changes: 22 additions & 0 deletions packages/method-doc-block-generator/LICENSE
@@ -0,0 +1,22 @@
MIT License
===========

Copyright (c) Robin Delattre https://piedweb.com <contact@robin-d.fr>

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.
71 changes: 71 additions & 0 deletions packages/method-doc-block-generator/README.md
@@ -0,0 +1,71 @@
<p align="center"><a href="https://dev.piedweb.com">
<img src="https://raw.githubusercontent.com/PiedWeb/piedweb-devoluix-theme/master/src/img/logo_title.png" width="200" height="200" alt="Open Source Package" />
</a></p>

# Method Doc Block Generator

[![Latest Version](https://img.shields.io/github/tag/PiedWeb/MethodDocBlockGenerator.svg?style=flat&label=release)](https://github.com/PiedWeb/MethodDocBlockGenerator/tags)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](LICENSE)
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/PiedWeb/MethodDocBlockGenerator/Tests?label=tests)](https://github.com/PiedWeb/PiedWeb/actions)
[![Quality Score](https://img.shields.io/scrutinizer/g/PiedWeb/PiedWeb.svg?style=flat)](https://scrutinizer-ci.com/g/PiedWeb/PiedWeb)
[![Code Coverage](https://codecov.io/gh/PiedWeb/PiedWeb/branch/main/graph/badge.svg)](https://codecov.io/gh/PiedWeb/PiedWeb/branch/main)
[![Type Coverage](https://shepherd.dev/github/PiedWeb/PiedWeb/coverage.svg)](https://shepherd.dev/github/PiedWeb/PiedWeb)
[![Total Downloads](https://img.shields.io/packagist/dt/piedweb/method-doc-block-generator.svg?style=flat)](https://packagist.org/packages/piedweb/method-doc-block-generator)


## Install

Via [Packagist](https://img.shields.io/packagist/dt/piedweb/method-doc-block-generator.svg?style=flat)

```bash
$ composer create-project piedweb/method-doc-block-generator
```

## Usage

### MethodDocBlockGenerator CLI

Example :
```php

include 'vendor/autoload.php';

$output = '<?php

namespace PiedWeb\SeoStatus\Service;

use League\Plates\Template\Template;

/**
* Generated by php script/GenerateDocBlockForPlatesTemplates.php
';
foreach ($extensionList as $className) {
$output .= (new GenerateDocBlockForPlatesTemplate())->run($className);
}
$output .= ' */
class PlatesTemplate extends Template
{
}';

file_put_contents('src/Service/PlatesTemplate.php', $output);
```

## Contributing

Please see [contributing](https://dev.piedweb.com/contributing)

## Credits

- [PiedWeb](https://piedweb.com) ak [Robind4](https://twitter.com/Robind4)
- [All Contributors](https://github.com/PiedWeb/:package_skake/graphs/contributors)

## License

The MIT License (MIT). Please see [License File](LICENSE) for more information.

[![Latest Version](https://img.shields.io/github/tag/PiedWeb/PiedWeb.svg?style=flat&label=release)](https://github.com/PiedWeb/PiedWeb/tags)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](https://github.com/PiedWeb/PiedWeb/blob/master/LICENSE)
[![Build Status](https://img.shields.io/travis/PiedWeb/PiedWeb/master.svg?style=flat)](https://travis-ci.org/PiedWeb/PiedWeb)
[![Quality Score](https://img.shields.io/scrutinizer/g/PiedWeb/PiedWeb.svg?style=flat)](https://scrutinizer-ci.com/g/PiedWeb/PiedWeb)
[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/PiedWeb/PiedWeb.svg?style=flat)](https://scrutinizer-ci.com/g/PiedWeb/PiedWeb/code-structure)
[![Total Downloads](https://img.shields.io/packagist/dt/piedweb/method-doc-block-generator.svg?style=flat)](https://packagist.org/packages/piedweb/method-doc-block-generator)
27 changes: 27 additions & 0 deletions packages/method-doc-block-generator/composer.json
@@ -0,0 +1,27 @@
{
"name": "piedweb/method-doc-block-generator",
"type": "library",
"description": "Method DocBlock Generator",
"homepage": "https://dev.piedweb.com",
"license": "MIT",
"authors": [
{
"name": "Robin D. (ak Pied Web)",
"email": "contact@robin-d.fr",
"homepage": "https://piedweb.com"
}
],
"require": {
"php": ">=8.1"
},
"autoload": {
"psr-4": {
"PiedWeb\\MethodDocBlockGenerator\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"PiedWeb\\MethodDocBlockGenerator\\": "tests"
}
}
}
@@ -0,0 +1,66 @@
<?php

namespace PiedWeb\MethodDocBlockGenerator;

class MethodDocBlockGenerator
{
public function run($extensionClassName)
{
$reflectionClass = new \ReflectionClass($extensionClassName);
$methods = $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC);
$phpDoc = '';

foreach ($methods as $method) {
if (\in_array($method->getName(), ['__construct', 'register'])) {
continue;
}

$paramStrings = [];

foreach ($method->getParameters() as $parameter) {
$paramString = $this->formatType($parameter->getType()).' $'.$parameter->getName();

if ($parameter->isDefaultValueAvailable()) {
$defaultValue = $parameter->getDefaultValue();
$paramString .= ' = '.str_replace(\chr(10), '', var_export($defaultValue, true));
}

$paramStrings[] = $paramString;
}

$returnType = $this->formatType($method->getReturnType());

$phpDoc .= ' * @method '.('' === $returnType ? '' : $returnType.' ').$method->getName().'('.implode(', ', $paramStrings).')';
$phpDoc .= ' // LINK '.$reflectionClass->getFileName().':'.$method->getStartLine();
$phpDoc .= "\n";
}

return $phpDoc;
}

private function formatType(\ReflectionType $returnType): string
{
if (null === $returnType) {

Check failure on line 43 in packages/method-doc-block-generator/src/MethodDocBlockGenerator.php

View workflow job for this annotation

GitHub Actions / psalm

TypeDoesNotContainNull

packages/method-doc-block-generator/src/MethodDocBlockGenerator.php:43:13: TypeDoesNotContainNull: ReflectionType does not contain null (see https://psalm.dev/090)

Check failure on line 43 in packages/method-doc-block-generator/src/MethodDocBlockGenerator.php

View workflow job for this annotation

GitHub Actions / psalm

TypeDoesNotContainNull

packages/method-doc-block-generator/src/MethodDocBlockGenerator.php:43:13: TypeDoesNotContainNull: ReflectionType does not contain null (see https://psalm.dev/090)
return '';
}

if ($returnType instanceof \ReflectionNamedType) {
return false === $returnType?->isBuiltin() ?

Check failure on line 48 in packages/method-doc-block-generator/src/MethodDocBlockGenerator.php

View workflow job for this annotation

GitHub Actions / psalm

TypeDoesNotContainNull

packages/method-doc-block-generator/src/MethodDocBlockGenerator.php:48:20: TypeDoesNotContainNull: Cannot resolve types for $returnType - ReflectionNamedType does not contain null (see https://psalm.dev/090)

Check failure on line 48 in packages/method-doc-block-generator/src/MethodDocBlockGenerator.php

View workflow job for this annotation

GitHub Actions / psalm

TypeDoesNotContainNull

packages/method-doc-block-generator/src/MethodDocBlockGenerator.php:48:20: TypeDoesNotContainNull: Cannot resolve types for $returnType - ReflectionNamedType does not contain null (see https://psalm.dev/090)
(($returnType->allowsNull() ? '?' : '').'\\'.$returnType->getName())
: $returnType;
}

if ($returnType instanceof \ReflectionUnionType) {
$toReturn = [];
foreach ($returnType->getTypes() as $type) {
$toReturn[] = $this->formatType($type);
}

return implode('|', $toReturn);
}

dd($returnType);

throw new \Exception();
}
}

0 comments on commit e188d3a

Please sign in to comment.