Skip to content

Commit 2e8d032

Browse files
committed
Initial commit
1 parent e6abf00 commit 2e8d032

8 files changed

+241
-0
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
composer.lock
2+
vendor/

Diff for: .phpunit.result.cache

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":1,"defects":{"TextFromDocCommentTest::testSingleLine":8,"TextFromDocCommentTest::testMultiLine":7,"TextFromDocCommentTest::testMultiParagraph":7,"TextFromDocCommentTest::testEmptyDocBlock":8,"TextFromDocCommentTest::testJoinedMultiLine":7,"TextFromDocCommentTest::testJoinedMultiParagraph":7},"times":{"TextFromDocCommentTest::testEmptyDocBlock":0,"TextFromDocCommentTest::testSingleLine":0,"TextFromDocCommentTest::testMultiLine":0,"TextFromDocCommentTest::testMultiParagraph":0,"TextFromDocCommentTest::testJoinedMultiLine":0,"TextFromDocCommentTest::testJoinedMultiParagraph":0}}

Diff for: README.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Vendimia helper classes
2+
3+
## `Vendimia\Helper\TextFromDocComment`
4+
5+
Process a DocComment, (perhaps obtained with `Reflection*::getDocComment()` method), obtaining only the text with preceding or trailing spaces or empty lines removed. Also remove an initial `*` on each line.
6+
7+
### `public string $text`
8+
9+
Processed text, also obtainable by using the class in a string context.
10+
11+
### `public function __construct(string $doc_comment, bool $merge_lines = true)`
12+
13+
* `string $doc_comment`: Original doc comment.
14+
15+
* `bool $merge_lines`: `true` merges continuous non-empty lines as one, joined by a single empty space. `false` returns the lines as is.
16+
17+
### Example
18+
19+
```php
20+
use Vendimia\Helper\TextFromDocComment;
21+
22+
require __DIR__ . '/vendor/autoload.php';
23+
24+
/**
25+
* This is a dummy function.
26+
*
27+
* Its purpose is to hold a multiline doc comment to test the
28+
* Vendimia\Helper\TextFromDocComment class.
29+
*/
30+
function dummy()
31+
{
32+
33+
}
34+
35+
echo (string)new TextFromDocComment(
36+
new ReflectionFunction(dummy(...))->getDocComment()
37+
);
38+
```
39+
40+
This outputs:
41+
42+
```
43+
This is a dummy function.
44+
Its purpose is to hold a multiline doc comment to test the Vendimia\Helper\TextFromDocComment class.
45+
```
46+
47+
On PHP prior to 8.4, you must enclose `new ReflectionFunction(dummy(...))` on parenthesis.

Diff for: composer.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "vendimia/helper",
3+
"description": "Assorted helper classes",
4+
"license": "GPL-3.0-or-later",
5+
"keywords": ["php", "vendimia", "orm"],
6+
"require": {
7+
"php": ">=8.1"
8+
},
9+
"autoload": {
10+
"psr-4": {
11+
"Vendimia\\Helper\\": "src"
12+
}
13+
},
14+
"authors": [
15+
{
16+
"name": "Oliver Etchebarne",
17+
"email": "[email protected]",
18+
"homepage": "https://drmad.org",
19+
"role": "developer"
20+
}
21+
]
22+
}

Diff for: phpunit.xml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<phpunit bootstrap="tests/bootstrap.php">
3+
<testsuites>
4+
<testsuite name="all">
5+
<directory>tests/</directory>
6+
</testsuite>
7+
</testsuites>
8+
</phpunit>

Diff for: src/TextFromDocComment.php

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Vendimia\Helper;
4+
5+
use Stringable;
6+
7+
/**
8+
* Extract the text from a DocComment block.
9+
*
10+
* Each
11+
*/
12+
class TextFromDocComment implements Stringable
13+
{
14+
/** Extracted text */
15+
public string $text = '';
16+
17+
public function __construct(
18+
string $doc_comment,
19+
bool $merge_lines = true
20+
)
21+
{
22+
$result = [];
23+
$lines = explode("\n", $doc_comment);
24+
25+
// Si solo hay una línea, removemos los caracteres de ámbos lados
26+
if (count($lines) == 1) {
27+
$this->text = trim($doc_comment, '/* ');
28+
return;
29+
}
30+
31+
$last_line = '';
32+
33+
foreach ($lines as $line) {
34+
$pline = trim($line);
35+
36+
// Si la línea empieza con un '/**', lo eliminamos
37+
if (str_starts_with($pline, '/**')) {
38+
$pline = substr($pline, 3);
39+
40+
// Si el resultado es vacío, lo ignoramos
41+
if (!$pline) {
42+
continue;
43+
}
44+
}
45+
46+
// Si la línea acaba con un */, lo eliminamos
47+
if (str_ends_with($pline, '*/')) {
48+
$pline = substr($pline, 1, -2);
49+
}
50+
51+
// Si la línea empieza con un '*', lo eliminamos
52+
if (str_starts_with($pline, '*')) {
53+
$pline = trim(substr($pline, 1));
54+
}
55+
56+
// Líneas vacías añaden un nuevo párrafo.
57+
58+
if ($merge_lines) {
59+
if ($pline == "") {
60+
$last_line = trim($last_line);
61+
if ($last_line) {
62+
$result[] = $last_line;
63+
$last_line = '';
64+
}
65+
} else {
66+
$last_line .= $pline . ' ';
67+
}
68+
} else {
69+
$result[] = trim($pline);
70+
}
71+
}
72+
$this->text = trim(join("\n", $result));
73+
}
74+
75+
public function __toString(): string
76+
{
77+
return $this->text;
78+
}
79+
}

Diff for: tests/TextFromDocCommentTest.php

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php declare(strict_types=1);
2+
use PHPUnit\Framework\TestCase;
3+
use Vendimia\Helper\TextFromDocComment;
4+
5+
final class TextFromDocCommentTest extends TestCase
6+
{
7+
public function testEmptyDocBlock(): void
8+
{
9+
$this->assertEquals(
10+
(new TextFromDocComment('/** */'))->text,
11+
''
12+
);
13+
}
14+
15+
public function testSingleLine(): void
16+
{
17+
$this->assertEquals(
18+
'Este es un DocComment',
19+
(new TextFromDocComment('/** Este es un DocComment */'))->text,
20+
);
21+
}
22+
23+
public function testJoinedMultiLine(): void
24+
{
25+
$doc_comment = <<<EOF
26+
/**
27+
* Extract the text from a DocComment block, removing asterisk and trailing
28+
* blank lines
29+
*/
30+
EOF;
31+
32+
$this->assertEquals(
33+
"Extract the text from a DocComment block, removing asterisk and trailing blank lines",
34+
(new TextFromDocComment($doc_comment))->text,
35+
);
36+
}
37+
38+
public function testMultiLine(): void
39+
{
40+
$doc_comment = <<<EOF
41+
/**
42+
* Extract the text from a DocComment block, removing asterisk and trailing
43+
* blank lines.
44+
*/
45+
EOF;
46+
47+
$this->assertEquals(
48+
<<<EOF
49+
Extract the text from a DocComment block, removing asterisk and trailing
50+
blank lines.
51+
EOF,
52+
(new TextFromDocComment($doc_comment, merge_lines: false))->text,
53+
);
54+
}
55+
56+
public function testJoinedMultiParagraph(): void
57+
{
58+
$doc_comment = <<<'EOF'
59+
/**
60+
* Extract the text from a DocComment block.
61+
*
62+
* This is a sample paragraph that should be returned
63+
* in one single line.
64+
*
65+
* Other single-line paragraphs should work, also.
66+
*
67+
*/
68+
EOF;
69+
70+
$this->assertEquals(
71+
<<<'EOF'
72+
Extract the text from a DocComment block.
73+
This is a sample paragraph that should be returned in one single line.
74+
Other single-line paragraphs should work, also.
75+
EOF,
76+
(new TextFromDocComment($doc_comment))->text,
77+
);
78+
}
79+
}

Diff for: tests/bootstrap.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';

0 commit comments

Comments
 (0)