Skip to content

Commit

Permalink
format source files.
Browse files Browse the repository at this point in the history
  • Loading branch information
smeghead committed Mar 1, 2023
1 parent f7615ec commit 8dde776
Show file tree
Hide file tree
Showing 20 changed files with 415 additions and 224 deletions.
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"name": "smeghead/php-class-diagram",
"description": "A CLI tool that parses the PHP source directory and outputs PlantUML scripts.",
"type": "library",
"keywords": [
"classdiagram",
"PlantUML"
],
"require": {
"php" : ">=8.0",
"symfony/finder": "^5.3",
Expand Down
41 changes: 27 additions & 14 deletions src/Config/Options.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Smeghead\PhpClassDiagram\Config;

class Options {
class Options
{
private array $opt;

const PHP5 = 'php5';
Expand All @@ -12,11 +16,13 @@ class Options {
const DIAGRAM_PACKAGE = 'package';
const DIAGRAM_JIG = 'jig';

public function __construct(array $opt) {
public function __construct(array $opt)
{
$this->opt = $opt;
}

public function help(): bool {
public function help(): bool
{
if (isset($this->opt['h'])) {
return true;
}
Expand All @@ -26,7 +32,8 @@ public function help(): bool {
return false;
}

public function diagram(): string {
public function diagram(): string
{
if (isset($this->opt['class-diagram'])) {
return self::DIAGRAM_CLASS;
}
Expand All @@ -40,7 +47,8 @@ public function diagram(): string {
return self::DIAGRAM_CLASS;
}

public function classProperties(): bool {
public function classProperties(): bool
{
if (isset($this->opt['enable-class-properties'])) {
return true;
}
Expand All @@ -51,7 +59,8 @@ public function classProperties(): bool {
return true;
}

public function classMethods(): bool {
public function classMethods(): bool
{
if (isset($this->opt['enable-class-methods'])) {
return true;
}
Expand All @@ -62,7 +71,8 @@ public function classMethods(): bool {
return true;
}

public function phpVersion(): string {
public function phpVersion(): string
{
if (isset($this->opt['php5'])) {
return self::PHP5;
}
Expand All @@ -79,11 +89,12 @@ public function phpVersion(): string {
/**
* @return string[] specified headers
*/
public function headers(): array {
public function headers(): array
{
$headers = [];
if (isset($this->opt['header'])) {
$headers = $this->opt['header'];
if ( ! is_array($headers)) {
if (!is_array($headers)) {
$headers = [$headers];
}
}
Expand All @@ -93,11 +104,12 @@ public function headers(): array {
/**
* @return string[] specified includes
*/
public function includes(): array {
public function includes(): array
{
$includes = [];
if (isset($this->opt['include'])) {
$includes = $this->opt['include'];
if ( ! is_array($includes)) {
if (!is_array($includes)) {
$includes = [$includes];
}
} else {
Expand All @@ -109,11 +121,12 @@ public function includes(): array {
/**
* @return string[] specified excludes
*/
public function excludes(): array {
public function excludes(): array
{
$excludes = [];
if (isset($this->opt['exclude'])) {
$excludes = $this->opt['exclude'];
if ( ! is_array($excludes)) {
if (!is_array($excludes)) {
$excludes = [$excludes];
}
}
Expand Down
27 changes: 17 additions & 10 deletions src/DiagramElement/Arrow.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Smeghead\PhpClassDiagram\DiagramElement;

use Smeghead\PhpClassDiagram\Php\ {
PhpType,
PhpClass,
use Smeghead\PhpClassDiagram\Php\{
PhpType,
PhpClass,
};

abstract class Arrow {
abstract class Arrow
{
protected string $figure = '..>';
private PhpClass $from;
private PhpType $to;

public function __construct(PhpClass $from, PhpType $to) {
public function __construct(PhpClass $from, PhpType $to)
{
$this->from = $from;
$this->to = $to;
}

public function getFrom(): PhpClass {
return $this->from;
public function getFrom(): PhpClass
{
return $this->from;
}

public function getTo(): PhpType {
return $this->to;
public function getTo(): PhpType
{
return $this->to;
}

abstract public function toString(PhpClass $toClass): string;
Expand Down
11 changes: 8 additions & 3 deletions src/DiagramElement/ArrowDependency.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Smeghead\PhpClassDiagram\DiagramElement;

use Smeghead\PhpClassDiagram\Php\PhpClass;

class ArrowDependency extends Arrow {
class ArrowDependency extends Arrow
{
protected string $figure = '..>';

public function toString(PhpClass $toClass): string {
public function toString(PhpClass $toClass): string
{
if (strpos($this->getTo()->getName(), '[]') === false) {
return sprintf(' %s %s %s', $this->getFrom()->getClassNameAlias(), $this->figure, $toClass->getClassNameAlias());
}
Expand Down
11 changes: 8 additions & 3 deletions src/DiagramElement/ArrowInheritance.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Smeghead\PhpClassDiagram\DiagramElement;

use Smeghead\PhpClassDiagram\Php\PhpClass;

class ArrowInheritance extends Arrow {
class ArrowInheritance extends Arrow
{
protected string $figure = '<|--';

public function toString(PhpClass $toClass): string {
public function toString(PhpClass $toClass): string
{
return sprintf(' %s %s %s', $toClass->getClassNameAlias(), $this->figure, $this->getFrom()->getClassNameAlias());
}
}
39 changes: 25 additions & 14 deletions src/DiagramElement/Entry.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,46 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Smeghead\PhpClassDiagram\DiagramElement;

use Smeghead\PhpClassDiagram\Config\Options;
use Smeghead\PhpClassDiagram\Php\ {
use Smeghead\PhpClassDiagram\Php\{
PhpClass,
PhpAccessModifier,
PhpMethodParameter,
};

class Entry {
class Entry
{
private Options $options;
private string $directory;
private PhpClass $class;

public function __construct(string $directory, PhpClass $class, Options $options) {
public function __construct(string $directory, PhpClass $class, Options $options)
{
$this->directory = $directory;
$this->class = $class;
$this->options = $options;
}

public function getOptions(): Options {
public function getOptions(): Options
{
return $this->options;
}

public function getDirectory(): string {
public function getDirectory(): string
{
return $this->directory;
}

public function getClass(): PhpClass {
public function getClass(): PhpClass
{
return $this->class;
}

public function dump($level = 0): array {
public function dump($level = 0): array
{
$indent = str_repeat(' ', $level);
$lines = [];
// $meta = $this->class->getClassType()->getMeta() === 'Stmt_Interface' ? 'interface' : 'class';
Expand All @@ -45,7 +54,7 @@ public function dump($level = 0): array {
}
if ($this->options->classMethods()) {
foreach ($this->class->getMethods() as $m) {
$params = array_map(function(PhpMethodParameter $x){
$params = array_map(function (PhpMethodParameter $x) {
return $x->getName();
}, $m->getParams());
$lines[] = sprintf(' %s%s%s(%s)', $indent, $this->modifier($m->getAccessModifier()), $m->getName(), implode(', ', $params));
Expand All @@ -58,7 +67,8 @@ public function dump($level = 0): array {
return $lines;
}

private function modifier(PhpAccessModifier $modifier): string {
private function modifier(PhpAccessModifier $modifier): string
{
$expressions = [];
if ($modifier->isStatic()) {
$expressions[] = '{static}';
Expand All @@ -75,10 +85,11 @@ private function modifier(PhpAccessModifier $modifier): string {
if ($modifier->isPrivate()) {
$expressions[] = '-';
}
return implode(' ' , $expressions);
return implode(' ', $expressions);
}

public function getArrows(): array {
public function getArrows(): array
{
$arrows = [];
//フィールド変数の型に対しての依存をArrowとして追加する。
foreach ($this->class->getProperties() as $p) {
Expand All @@ -87,7 +98,7 @@ public function getArrows(): array {
}
}
foreach ($this->class->getMethods() as $m) {
if ( ! $m->getAccessModifier()->isPublic()) {
if (!$m->getAccessModifier()->isPublic()) {
continue;
}
foreach ($m->getParams() as $p) {
Expand All @@ -100,7 +111,7 @@ public function getArrows(): array {
}
}
$extends = $this->class->getExtends();
if ( ! empty($extends)) {
if (!empty($extends)) {
//継承先に対してArrowを追加する。
foreach ($extends as $extend) {
$arrows[] = new ArrowInheritance($this->class, $extend);
Expand Down
Loading

0 comments on commit 8dde776

Please sign in to comment.