Skip to content

Commit

Permalink
also track class usages
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalkaoz committed Jun 12, 2014
1 parent f8c9427 commit 5abdd49
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/YumlPhp/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class Application extends BaseApplication
private $container;

/**
* @param \Pimple $container
* @param Container $container
*/
public function __construct(\Pimple $container)
public function __construct(Container $container)
{
parent::__construct('yuml-php', '@package_version@');

Expand Down
2 changes: 2 additions & 0 deletions src/YumlPhp/Builder/HttpBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public function request(array $request)
'<info>PNG</info> http://yuml.me/' . $file,
'<info>URL</info> http://yuml.me/edit/' . str_replace('.png', '', $file),
'<info>PDF</info> http://yuml.me/' . str_replace('.png', '.pdf', $file),
'<info>JSON</info> http://yuml.me/' . str_replace('.png', '.json', $file),
'<info>SVG</info> http://yuml.me/' . str_replace('.png', '.svg', $file),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/YumlPhp/Command/ClassesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function getBuilderConfig(BuilderInterface $builder, InputInterface $i
//scruffy, nofunky, plain
//dir: LR TB RL
//scale: 180 120 100 80 60
$style = $input->getOption('style') ? : 'plain;dir:LR;scale:80;';
$style = $input->getOption('style') ? : 'plain;dir:TB;scale:80;';
$type = $builder->getType();

return array(
Expand Down
38 changes: 35 additions & 3 deletions src/YumlPhp/Request/ClassesRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use TokenReflection\Exception\BaseException;
use TokenReflection\IReflectionClass;
use TokenReflection\IReflectionMethod;
use TokenReflection\IReflectionParameter;
use TokenReflection\IReflectionProperty;

/**
Expand Down Expand Up @@ -85,11 +86,17 @@ protected function buildName(IReflectionClass $class, $prefix = '<<', $suffix =
{
$name = $this->prepare($class);

if ($class->isInterface()) {
$name = $prefix . $this->prepare($class) . $suffix;
if ($class->isInterface() || substr($name, -strlen('Interface')) === 'Interface') {
$suffix = $suffix.'{bg:orange}';
} elseif ($class->isAbstract()) {
$prefix = '';
$suffix = '{bg:blue}';
} else {
$prefix = '';
$suffix = '';
}

return $name;
return $prefix . $this->prepare($class) . $suffix;
}

/**
Expand Down Expand Up @@ -178,6 +185,31 @@ protected function buildMethods(IReflectionClass $class, $public = '+', $private
return $methods;
}

/**
* extracts usages to other classes
*
* @param IReflectionClass $class
* @return IReflectionClass[]
*/
protected function buildUsages(IReflectionClass $class)
{
$usages = [];
foreach ($class->getMethods() as $method) {
/** @var IReflectionMethod $method */
if ($method->getDeclaringClass() !== $class) {
continue;
}
foreach ($method->getParameters() as $parameter) {
/** @var IReflectionParameter $parameter */
if ($parameter->getClass()) {
$usages[$parameter->getClassName()] = $parameter->getClass();
}
};
}

return array_unique($usages);
}

/**
* collects all interfaces for current class (only self implemented ones)
*
Expand Down
16 changes: 9 additions & 7 deletions src/YumlPhp/Request/Http/ClassesRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,14 @@ public function build()
foreach ($this->getClasses() as $class) {
$name = $this->buildName($class);
$parent = $this->buildParent($class, '[', ']^');
$interfaces = $this->buildInterfaces($class, '<<', '>>]^-.-[');
$interfaces = $this->buildInterfaces($class, '<<', '>>{bg:orange}]^-.-[');
$props = $this->buildProperties($class);
$methods = $this->buildMethods($class);
$usages = $this->buildUsages($class);
$prefix = null;
$suffix = null;
$pattern = "%s[%s%s%s%s]";

if ($class->isInterface()) {
$prefix = '';
$suffix = '{bg:orange}';
}

//rebuild pattern
if (count($methods) || count($props)) {
$pattern = "%s[%s%s|%s%s]";
Expand All @@ -39,7 +35,13 @@ public function build()
$pattern = "%s[%s%s|%s|%s]";
}

$line = sprintf($pattern, $parent, $prefix . join(';', $interfaces), $name, join(';', $props), join(';', $methods) . $suffix);
$line = sprintf($pattern, $parent, $prefix . join(';', $interfaces), $name, join(';', $props), join(';', $methods));

if (!$class->isInterface()) {
foreach ($usages as $usage) {
$request[] = sprintf('[%s]-.->[%s]', $name, $this->buildName($usage));
}
}

if ($class->isInterface()) {
array_unshift($request, $line);
Expand Down
6 changes: 6 additions & 0 deletions tests/Integration/YumlApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public function testYuml(BuilderInterface $builder, $fixture, $config)
case '<info>URL</info>' :
$contentType = 'text/html; charset=utf-8';
break;
case '<info>JSON</info>' :
$contentType = 'application/json';
break;
case '<info>SVG</info>' :
$contentType = 'image/svg+xml';
break;
}
$this->assertEquals($contentType, $response->getHeader('Content-Type'));
}
Expand Down
4 changes: 3 additions & 1 deletion tests/YumlPhp/Tests/Builder/HttpBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public function testRequest()
$this->assertEquals(array(
'<info>PNG</info> http://yuml.me/foo',
'<info>URL</info> http://yuml.me/edit/foo',
'<info>PDF</info> http://yuml.me/foo'
'<info>PDF</info> http://yuml.me/foo',
'<info>JSON</info> http://yuml.me/foo',
'<info>SVG</info> http://yuml.me/foo'
), $result);
}
}
12 changes: 6 additions & 6 deletions tests/YumlPhp/Tests/Fixtures/classes-full.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
YumlPhp/Tests/Fixtures/BarWithExternal Symfony/Component/Console/Input/StringInput,
+bar;-foo,
+bar();-foo(),
YumlPhp/Tests/Fixtures/BarWithInterface <<YumlPhp/Tests/Fixtures/BarInterface>>,
<<YumlPhp/Tests/Fixtures/BarWithInterface>>{bg:orange} <<YumlPhp/Tests/Fixtures/BarInterface>>,
+bar;-foo,
+bar();-foo(),
YumlPhp/Tests/Fixtures/Bar,
+bar;-foo,
+bar();-foo(),
YumlPhp/Tests/Fixtures/Bazz,
YumlPhp/Tests/Fixtures/FooBazzWithInterface YumlPhp/Tests/Fixtures/Bazz <<YumlPhp/Tests/Fixtures/BazzInterface>>,
<<YumlPhp/Tests/Fixtures/FooBazzWithInterface>>{bg:orange} YumlPhp/Tests/Fixtures/Bazz <<YumlPhp/Tests/Fixtures/BazzInterface>>,
+bar;-foo,
+bar();-foo(),
YumlPhp/Tests/Fixtures/Foo YumlPhp/Tests/Fixtures/Bazz,
+bar;-foo,
+bar();-foo(),
YumlPhp/Tests/Fixtures/FooWithInterface <<YumlPhp/Tests/Fixtures/BazzInterface>> <<YumlPhp/Tests/Fixtures/FooInterface>>,
<<YumlPhp/Tests/Fixtures/FooWithInterface>>{bg:orange} <<YumlPhp/Tests/Fixtures/BazzInterface>> <<YumlPhp/Tests/Fixtures/FooInterface>>,
+bar();+foo(),
<<YumlPhp/Tests/Fixtures/BarInterface>>,
<<YumlPhp/Tests/Fixtures/BazzInterface>>,
<<YumlPhp/Tests/Fixtures/FooInterface>> <<YumlPhp/Tests/Fixtures/BazzInterface>>
<<YumlPhp/Tests/Fixtures/BarInterface>>{bg:orange},
<<YumlPhp/Tests/Fixtures/BazzInterface>>{bg:orange},
<<YumlPhp/Tests/Fixtures/FooInterface>>{bg:orange} <<YumlPhp/Tests/Fixtures/BazzInterface>>
12 changes: 6 additions & 6 deletions tests/YumlPhp/Tests/Fixtures/classes-methods.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
YumlPhp/Tests/Fixtures/BarWithExternal Symfony/Component/Console/Input/StringInput,
+bar();-foo(),
YumlPhp/Tests/Fixtures/BarWithInterface <<YumlPhp/Tests/Fixtures/BarInterface>>,
<<YumlPhp/Tests/Fixtures/BarWithInterface>>{bg:orange} <<YumlPhp/Tests/Fixtures/BarInterface>>,
+bar();-foo(),
YumlPhp/Tests/Fixtures/Bar,
+bar();-foo(),
YumlPhp/Tests/Fixtures/Bazz,
YumlPhp/Tests/Fixtures/FooBazzWithInterface YumlPhp/Tests/Fixtures/Bazz <<YumlPhp/Tests/Fixtures/BazzInterface>>,
<<YumlPhp/Tests/Fixtures/FooBazzWithInterface>>{bg:orange} YumlPhp/Tests/Fixtures/Bazz <<YumlPhp/Tests/Fixtures/BazzInterface>>,
+bar();-foo(),
YumlPhp/Tests/Fixtures/Foo YumlPhp/Tests/Fixtures/Bazz,
+bar();-foo(),
YumlPhp/Tests/Fixtures/FooWithInterface <<YumlPhp/Tests/Fixtures/BazzInterface>> <<YumlPhp/Tests/Fixtures/FooInterface>>,
<<YumlPhp/Tests/Fixtures/FooWithInterface>>{bg:orange} <<YumlPhp/Tests/Fixtures/BazzInterface>> <<YumlPhp/Tests/Fixtures/FooInterface>>,
+bar();+foo(),
<<YumlPhp/Tests/Fixtures/BarInterface>>,
<<YumlPhp/Tests/Fixtures/BazzInterface>>,
<<YumlPhp/Tests/Fixtures/FooInterface>> <<YumlPhp/Tests/Fixtures/BazzInterface>>
<<YumlPhp/Tests/Fixtures/BarInterface>>{bg:orange},
<<YumlPhp/Tests/Fixtures/BazzInterface>>{bg:orange},
<<YumlPhp/Tests/Fixtures/FooInterface>>{bg:orange} <<YumlPhp/Tests/Fixtures/BazzInterface>>
12 changes: 6 additions & 6 deletions tests/YumlPhp/Tests/Fixtures/classes-props.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
YumlPhp/Tests/Fixtures/BarWithExternal Symfony/Component/Console/Input/StringInput,
+bar;-foo,
YumlPhp/Tests/Fixtures/BarWithInterface <<YumlPhp/Tests/Fixtures/BarInterface>>,
<<YumlPhp/Tests/Fixtures/BarWithInterface>>{bg:orange} <<YumlPhp/Tests/Fixtures/BarInterface>>,
+bar;-foo,
YumlPhp/Tests/Fixtures/Bar,
+bar;-foo,
YumlPhp/Tests/Fixtures/Bazz,
YumlPhp/Tests/Fixtures/FooBazzWithInterface YumlPhp/Tests/Fixtures/Bazz <<YumlPhp/Tests/Fixtures/BazzInterface>>,
<<YumlPhp/Tests/Fixtures/FooBazzWithInterface>>{bg:orange} YumlPhp/Tests/Fixtures/Bazz <<YumlPhp/Tests/Fixtures/BazzInterface>>,
+bar;-foo,
YumlPhp/Tests/Fixtures/Foo YumlPhp/Tests/Fixtures/Bazz,
+bar;-foo,
YumlPhp/Tests/Fixtures/FooWithInterface <<YumlPhp/Tests/Fixtures/BazzInterface>> <<YumlPhp/Tests/Fixtures/FooInterface>>,
<<YumlPhp/Tests/Fixtures/BarInterface>>,
<<YumlPhp/Tests/Fixtures/BazzInterface>>,
<<YumlPhp/Tests/Fixtures/FooInterface>> <<YumlPhp/Tests/Fixtures/BazzInterface>>
<<YumlPhp/Tests/Fixtures/FooWithInterface>>{bg:orange} <<YumlPhp/Tests/Fixtures/BazzInterface>> <<YumlPhp/Tests/Fixtures/FooInterface>>,
<<YumlPhp/Tests/Fixtures/BarInterface>>{bg:orange},
<<YumlPhp/Tests/Fixtures/BazzInterface>>{bg:orange},
<<YumlPhp/Tests/Fixtures/FooInterface>>{bg:orange} <<YumlPhp/Tests/Fixtures/BazzInterface>>
12 changes: 6 additions & 6 deletions tests/YumlPhp/Tests/Fixtures/classes.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
YumlPhp/Tests/Fixtures/BarWithExternal Symfony/Component/Console/Input/StringInput,
YumlPhp/Tests/Fixtures/BarWithInterface <<YumlPhp/Tests/Fixtures/BarInterface>>,
<<YumlPhp/Tests/Fixtures/BarWithInterface>>{bg:orange} <<YumlPhp/Tests/Fixtures/BarInterface>>,
YumlPhp/Tests/Fixtures/Bar,
YumlPhp/Tests/Fixtures/Bazz,
YumlPhp/Tests/Fixtures/FooBazzWithInterface YumlPhp/Tests/Fixtures/Bazz <<YumlPhp/Tests/Fixtures/BazzInterface>>,
<<YumlPhp/Tests/Fixtures/FooBazzWithInterface>>{bg:orange} YumlPhp/Tests/Fixtures/Bazz <<YumlPhp/Tests/Fixtures/BazzInterface>>,
YumlPhp/Tests/Fixtures/Foo YumlPhp/Tests/Fixtures/Bazz,
YumlPhp/Tests/Fixtures/FooWithInterface <<YumlPhp/Tests/Fixtures/BazzInterface>> <<YumlPhp/Tests/Fixtures/FooInterface>>,
<<YumlPhp/Tests/Fixtures/BarInterface>>,
<<YumlPhp/Tests/Fixtures/BazzInterface>>,
<<YumlPhp/Tests/Fixtures/FooInterface>> <<YumlPhp/Tests/Fixtures/BazzInterface>>
<<YumlPhp/Tests/Fixtures/FooWithInterface>>{bg:orange} <<YumlPhp/Tests/Fixtures/BazzInterface>> <<YumlPhp/Tests/Fixtures/FooInterface>>,
<<YumlPhp/Tests/Fixtures/BarInterface>>{bg:orange},
<<YumlPhp/Tests/Fixtures/BazzInterface>>{bg:orange},
<<YumlPhp/Tests/Fixtures/FooInterface>>{bg:orange} <<YumlPhp/Tests/Fixtures/BazzInterface>>
12 changes: 6 additions & 6 deletions tests/YumlPhp/Tests/Request/ConsoleClassesRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,24 @@ public function testBuild()
'<info>YumlPhp/Tests/Fixtures/BarWithExternal</info><comment> Symfony/Component/Console/Input/StringInput</comment>',
"\t<info>+</info>bar;<question>-</question>foo",
"\t<info>+</info>bar();<question>-</question>foo()",
'<info>YumlPhp/Tests/Fixtures/BarWithInterface</info><comment></comment> <<YumlPhp/Tests/Fixtures/BarInterface>>',
'<info><<YumlPhp/Tests/Fixtures/BarWithInterface>>{bg:orange}</info><comment></comment> <<YumlPhp/Tests/Fixtures/BarInterface>>',
"\t<info>+</info>bar;<question>-</question>foo",
"\t<info>+</info>bar();<question>-</question>foo()",
'<info>YumlPhp/Tests/Fixtures/Bar</info><comment></comment>',
"\t<info>+</info>bar;<question>-</question>foo",
"\t<info>+</info>bar();<question>-</question>foo()",
'<info>YumlPhp/Tests/Fixtures/Bazz</info><comment></comment>',
'<info>YumlPhp/Tests/Fixtures/FooBazzWithInterface</info><comment> YumlPhp/Tests/Fixtures/Bazz</comment> <<YumlPhp/Tests/Fixtures/BazzInterface>>',
'<info><<YumlPhp/Tests/Fixtures/FooBazzWithInterface>>{bg:orange}</info><comment> YumlPhp/Tests/Fixtures/Bazz</comment> <<YumlPhp/Tests/Fixtures/BazzInterface>>',
"\t<info>+</info>bar;<question>-</question>foo",
"\t<info>+</info>bar();<question>-</question>foo()",
'<info>YumlPhp/Tests/Fixtures/Foo</info><comment> YumlPhp/Tests/Fixtures/Bazz</comment>',
"\t<info>+</info>bar;<question>-</question>foo",
"\t<info>+</info>bar();<question>-</question>foo()",
'<info>YumlPhp/Tests/Fixtures/FooWithInterface</info><comment></comment> <<YumlPhp/Tests/Fixtures/BazzInterface>> <<YumlPhp/Tests/Fixtures/FooInterface>>',
'<info><<YumlPhp/Tests/Fixtures/FooWithInterface>>{bg:orange}</info><comment></comment> <<YumlPhp/Tests/Fixtures/BazzInterface>> <<YumlPhp/Tests/Fixtures/FooInterface>>',
"\t<info>+</info>bar();<info>+</info>foo()",
'<info><<YumlPhp/Tests/Fixtures/BarInterface>></info><comment></comment>',
'<info><<YumlPhp/Tests/Fixtures/BazzInterface>></info><comment></comment>',
'<info><<YumlPhp/Tests/Fixtures/FooInterface>></info><comment></comment> <<YumlPhp/Tests/Fixtures/BazzInterface>>',
'<info><<YumlPhp/Tests/Fixtures/BarInterface>>{bg:orange}</info><comment></comment>',
'<info><<YumlPhp/Tests/Fixtures/BazzInterface>>{bg:orange}</info><comment></comment>',
'<info><<YumlPhp/Tests/Fixtures/FooInterface>>{bg:orange}</info><comment></comment> <<YumlPhp/Tests/Fixtures/BazzInterface>>'
), array_values($request->build()));
}
}
10 changes: 5 additions & 5 deletions tests/YumlPhp/Tests/Request/HttpClassesRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ public function testBuild()
$request->setPath(__DIR__ . '/../Fixtures');

$this->assertEquals(array(
'[<<YumlPhp/Tests/Fixtures/BarInterface>>]^-.-[YumlPhp/Tests/Fixtures/BarWithInterface]',
'[<<YumlPhp/Tests/Fixtures/BarInterface>>{bg:orange}]',
'[<<YumlPhp/Tests/Fixtures/BazzInterface>>]^-.-[;<<YumlPhp/Tests/Fixtures/FooInterface>>]^-.-[YumlPhp/Tests/Fixtures/FooWithInterface]',
'[<<YumlPhp/Tests/Fixtures/BazzInterface>>]^-.-[<<YumlPhp/Tests/Fixtures/FooInterface>>{bg:orange}]',
'[<<YumlPhp/Tests/Fixtures/BarInterface>>{bg:orange}]^-.-[<<YumlPhp/Tests/Fixtures/BarWithInterface>>{bg:orange}]',
'[<<YumlPhp/Tests/Fixtures/BazzInterface>>{bg:orange}]',
'[<<YumlPhp/Tests/Fixtures/BazzInterface>>{bg:orange}]^-.-[;<<YumlPhp/Tests/Fixtures/FooInterface>>{bg:orange}]^-.-[<<YumlPhp/Tests/Fixtures/FooWithInterface>>{bg:orange}]',
'[<<YumlPhp/Tests/Fixtures/BazzInterface>>{bg:orange}]^-.-[<<YumlPhp/Tests/Fixtures/FooInterface>>{bg:orange}]',
'[Symfony/Component/Console/Input/StringInput]^[YumlPhp/Tests/Fixtures/BarWithExternal]',
'[YumlPhp/Tests/Fixtures/Bar]',
'[YumlPhp/Tests/Fixtures/Bazz]',
'[YumlPhp/Tests/Fixtures/Bazz]^[<<YumlPhp/Tests/Fixtures/BazzInterface>>]^-.-[YumlPhp/Tests/Fixtures/FooBazzWithInterface]',
'[YumlPhp/Tests/Fixtures/Bazz]^[YumlPhp/Tests/Fixtures/Foo]',
'[YumlPhp/Tests/Fixtures/Bazz]^[<<YumlPhp/Tests/Fixtures/BazzInterface>>{bg:orange}]^-.-[<<YumlPhp/Tests/Fixtures/FooBazzWithInterface>>{bg:orange}]',
'[YumlPhp/Tests/Fixtures/Bazz]^[YumlPhp/Tests/Fixtures/Foo]'
), array_values($request->build()));
}
}

0 comments on commit 5abdd49

Please sign in to comment.