Skip to content

Commit 07046a4

Browse files
committed
add description to division diagram.
1 parent 153a717 commit 07046a4

File tree

15 files changed

+229
-48
lines changed

15 files changed

+229
-48
lines changed

Dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
FROM php:8.1-cli
1+
FROM plantuml/plantuml
22

3-
RUN apt-get update && apt-get install -y zip git plantuml fonts-noto-cjk && apt-get clean && rm -rf /var/lib/apt/lists/*
3+
RUN apt-get update && apt-get install -y php8.1-cli php-xml composer fonts-noto-cjk && apt-get clean && rm -rf /var/lib/apt/lists/*
4+
RUN echo '#!/bin/sh' > /usr/bin/plantuml && echo 'java -jar /opt/plantuml.jar "$@"' >> /usr/bin/plantuml && chmod +x /usr/bin/plantuml
45

56
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
7+
ENTRYPOINT []

dogfood-model.png

3.92 KB
Loading

dogfood-package.png

-1.6 KB
Loading

dogfood.png

76.8 KB
Loading

output-division.png

1.79 KB
Loading

src/DiagramElement/Entry.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ public function dumpDivisions($level = 0): array
7878
if ($meta === 'enum') {
7979
$lines[] = sprintf('%scard %s %s [', $indent, $this->class->getClassType()->getName(), DivisionColor::nextColor());
8080
$lines[] = sprintf('%s %s', $indent, $this->class->getClassType()->getName());
81+
$description = $this->class->getDescription();
82+
if (!empty($description)) {
83+
$lines[] = sprintf('%s <b>%s</b>', $indent, $description);
84+
}
8185
$lines[] = sprintf('%s ====', $indent);
8286
$cases = $this->class->getEnumCases();
8387
$lines[] = implode(sprintf("\r\n%s ----\r\n", $indent), array_map(function (PhpEnumCase $x) use($indent) {

src/Php/Doc/PhpDocComment.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Smeghead\PhpClassDiagram\Php\Doc;
6+
7+
use PhpParser\Node\Stmt;
8+
9+
class PhpDocComment
10+
{
11+
private string $text = '';
12+
public function __construct(Stmt $stmt)
13+
{
14+
$doc = $stmt->getDocComment();
15+
if (!empty($doc)) {
16+
$str = $doc->getText();
17+
$str = preg_replace('/\/\*\*?\s*(.*)\s*\*\//s', '$1', $str);
18+
$str = preg_replace('/^\s*\**\s*/m', '', $str);
19+
$this->text = trim($str);
20+
}
21+
}
22+
23+
public function getText(): string
24+
{
25+
return $this->text;
26+
}
27+
28+
public function getDescription(): string
29+
{
30+
$str = $this->text;
31+
$str = str_replace(["\r\n", "\r", "\n"], "\n", $str);
32+
$lines = explode("\n", $str);
33+
return array_shift($lines);
34+
}
35+
36+
public function getVarTypeName(): string {
37+
if (preg_match('/\@var\s+(\S+)\s.*/', $this->text . ' ', $matches)) {
38+
return $matches[1];
39+
}
40+
return '';
41+
}
42+
43+
public function getParamTypeName(string $paramName): string {
44+
if (preg_match(sprintf('/\@param\s+(\S+)\s+\$%s.*/', $paramName), $this->text . ' ', $matches)) {
45+
return $matches[1];
46+
}
47+
return '';
48+
}
49+
50+
public function getReturnTypeName(): string {
51+
if (preg_match('/\@return\s+(\S+)\s+/', $this->text . ' ', $matches)) {
52+
return $matches[1];
53+
}
54+
return '';
55+
}
56+
}

src/Php/PhpClass.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
GroupUse,
2323
Use_,
2424
};
25+
use Smeghead\PhpClassDiagram\Php\Doc\PhpDocComment;
2526

2627
class PhpClass
2728
{
@@ -244,4 +245,9 @@ public function getEnumCases(): array
244245
}
245246
return $cases;
246247
}
248+
249+
public function getDescription(): string {
250+
$doc = new PhpDocComment($this->syntax);
251+
return $doc->getDescription();
252+
}
247253
}

src/Php/PhpEnumCase.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
88
use PhpParser\Node\Stmt\{
99
EnumCase,
1010
};
11+
use Smeghead\PhpClassDiagram\Php\Doc\PhpDocComment;
1112

1213
class PhpEnumCase
1314
{
1415
protected string $name;
15-
protected ?Doc $doc;
16+
protected PhpDocComment $doc;
1617

1718
public function __construct(EnumCase $e)
1819
{
1920
$this->name = $e->name->name;
20-
$this->doc = $e->getDocComment();
21+
$this->doc = new PhpDocComment($e);
2122
}
2223

2324
public function getName(): string
@@ -27,15 +28,7 @@ public function getName(): string
2728

2829
public function getDocString(): string
2930
{
30-
if (empty($this->doc)) {
31-
return '';
32-
}
33-
$str = $this->doc->getText();
34-
$str = preg_replace('/\/\*\*?\s*(.*)\s*\*\//s', '$1', $str);
35-
$str = preg_replace('/^\s*\**\s*/', '', $str);
36-
$str = str_replace(["\r\n", "\r", "\n"], "\n", $str);
37-
$lines = explode("\n", $str);
38-
return trim(array_shift($lines));
31+
return $this->doc->getDescription();
3932
}
4033

4134
}

src/Php/PhpMethod.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ class PhpMethod
2323

2424
public function __construct(ClassMethod $method, PhpClass $class)
2525
{
26-
$docString = '';
27-
$doc = $method->getDocComment();
28-
if (!empty($doc)) {
29-
$docString = $doc->getText();
30-
}
31-
$params = array_map(function (Param $x) use ($class, $docString) {
32-
$type = PhpTypeExpression::buildByMethodParam($x, $class->getNamespace(), $docString, $x->var->name, $class->getUses());
26+
// $docString = '';
27+
// $doc = $method->getDocComment();
28+
// if (!empty($doc)) {
29+
// $docString = $doc->getText();
30+
// }
31+
$params = array_map(function (Param $x) use ($class, $method) {
32+
$type = PhpTypeExpression::buildByMethodParam($x, $class->getNamespace(), $method, $x->var->name, $class->getUses());
3333
return new PhpMethodParameter($x->var->name, $type);
3434
}, $method->getParams());
3535
$this->name = $method->name->toString();

0 commit comments

Comments
 (0)