Skip to content

Commit

Permalink
Kint_Object_MethodTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jnvsor committed May 13, 2017
1 parent fa308f4 commit 17ec4f2
Show file tree
Hide file tree
Showing 9 changed files with 763 additions and 502 deletions.
257 changes: 134 additions & 123 deletions build/kint-aante-light.php

Large diffs are not rendered by default.

279 changes: 149 additions & 130 deletions build/kint-solarized-dark.php

Large diffs are not rendered by default.

249 changes: 125 additions & 124 deletions build/kint-solarized.php

Large diffs are not rendered by default.

234 changes: 124 additions & 110 deletions build/kint.php

Large diffs are not rendered by default.

27 changes: 15 additions & 12 deletions src/Object/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public function __construct($method)
$this->endline = $method->getEndLine();
$this->internal = $method->isInternal();
$this->docstring = $method->getDocComment();
$this->operator = $this->static ? Kint_Object::OPERATOR_STATIC : Kint_Object::OPERATOR_OBJECT;
$this->access = Kint_Object::ACCESS_PUBLIC;

foreach ($method->getParameters() as $param) {
$this->parameters[] = new Kint_Object_Parameter($param);
Expand All @@ -45,9 +43,11 @@ public function __construct($method)

if ($method instanceof ReflectionMethod) {
$this->static = $method->isStatic();
$this->operator = $this->static ? Kint_Object::OPERATOR_STATIC : Kint_Object::OPERATOR_OBJECT;
$this->abstract = $method->isAbstract();
$this->final = $method->isFinal();
$this->owner_class = $method->getDeclaringClass()->name;
$this->access = Kint_Object::ACCESS_PUBLIC;
if ($method->isProtected()) {
$this->access = Kint_Object::ACCESS_PROTECTED;
} elseif ($method->isPrivate()) {
Expand All @@ -65,13 +65,13 @@ public function __construct($method)
$this->addRepresentation($docstring);
}

public function setAccessPathFrom(Kint_Object $parent, $class)
public function setAccessPathFrom(Kint_Object_Instance $parent)
{
if ($this->name === '__construct') {
if (KINT_PHP53) {
$this->access_path = 'new \\'.$class;
$this->access_path = 'new \\'.$parent->getType();
} else {
$this->access_path = 'new '.$class;
$this->access_path = 'new '.$parent->getType();
}
} elseif ($this->static) {
if (KINT_PHP53) {
Expand Down Expand Up @@ -103,7 +103,7 @@ public function getValueShort()
}

if (strlen($out)) {
return $out;
return rtrim($out);
}
}

Expand All @@ -113,7 +113,6 @@ public function getModifiers()
$this->abstract ? 'abstract' : null,
$this->final ? 'final' : null,
$this->getAccess(),
$this->const ? 'const' : null,
$this->static ? 'static' : null,
);

Expand Down Expand Up @@ -147,14 +146,18 @@ public function getParams()

foreach ($this->parameters as $p) {
$type = $p->getType();
if ($type) {
$type .= ' ';
}

$default = $p->getDefault();
if ($default) {
$default = ' = '.$default;
}

$ref = $p->reference ? '&' : '';

if ($type) {
$out[] = $type.' '.$ref.$p->getName();
} else {
$out[] = $ref.$p->getName();
}
$out[] = $type.$ref.$p->getName().$default;
}

return $this->paramcache = implode(', ', $out);
Expand Down
23 changes: 21 additions & 2 deletions src/Object/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class Kint_Object_Parameter extends Kint_Object
{
public $type_hint = null;
public $default;
public $default = null;
public $position = null;
public $hints = array('parameter');

Expand Down Expand Up @@ -46,7 +46,26 @@ public function __construct(ReflectionParameter $param)
}

if ($param->isDefaultValueAvailable()) {
$this->default = var_export($param->getDefaultValue(), true);
$default = $param->getDefaultValue();
switch (gettype($default)) {
case 'NULL':
$this->default = 'null';
break;
case 'boolean':
$this->default = $default ? 'true' : 'false';
break;
case 'array':
$this->default = count($default) ? 'array(...)' : 'array()';
break;
default:
$this->default = var_export($default, true);
break;
}
}
}

public function getDefault()
{
return $this->default;
}
}
2 changes: 1 addition & 1 deletion src/Parser/ClassMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function parse(&$var, Kint_Object &$o, $trigger)
if (!$this->parser->childHasPath($o, $method)) {
$method->access_path = null;
} else {
$method->setAccessPathFrom($o, $class);
$method->setAccessPathFrom($o);
}

if ($method->owner_class !== $class) {
Expand Down
160 changes: 160 additions & 0 deletions tests/Object/MethodTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php

class Kint_Object_MethodTest extends PHPUnit_Framework_TestCase
{
public function testConstruct()
{
$reflection = new ReflectionMethod('TestClass', 'mix');
$m = new Kint_Object_Method($reflection);
$this->assertEquals('mix', $m->name);
$this->assertEquals($reflection->getFilename(), $m->filename);
$this->assertEquals($reflection->getStartLine(), $m->startline);
$this->assertEquals($reflection->getEndLine(), $m->endline);
$this->assertEquals(false, $m->internal);
$this->assertEquals($reflection->getDocComment(), $m->docstring);
$this->assertEquals(Kint_Object::OPERATOR_STATIC, $m->operator);
$this->assertEquals(Kint_Object::ACCESS_PROTECTED, $m->access);
$this->assertEquals('TestClass', $m->owner_class);
$this->assertTrue($m->static);
$this->assertTrue($m->final);
$this->assertFalse($m->abstract);
$this->assertFalse($m->internal);

$reflection = new ReflectionMethod('ChildTestClass', '__construct');
$parent_reflection = new ReflectionMethod('TestClass', '__construct');
$m = new Kint_Object_Method($reflection);
$this->assertEquals($parent_reflection->getDocComment(), $m->docstring);
$this->assertEquals(Kint_Object::OPERATOR_OBJECT, $m->operator);
$this->assertEquals(Kint_Object::ACCESS_PUBLIC, $m->access);
$this->assertEquals('TestClass', $m->owner_class);

$reflection = new ReflectionFunction('explode');
$m = new Kint_Object_Method($reflection);
$this->assertTrue($m->internal);
$this->assertEquals(Kint_Object::OPERATOR_NONE, $m->operator);
$this->assertEquals(Kint_Object::ACCESS_NONE, $m->access);
$this->assertEquals(null, $m->owner_class);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testConstructWrongType()
{
$m = new Kint_Object_Method(new stdClass());
}

public function testSetAccessPathFrom()
{
$o = Kint_Object::blank('$tc');
$o = $o->transplant(new Kint_Object_Instance());
$o->classname = 'TestClass';

$m = new Kint_Object_Method(new ReflectionMethod('TestClass', '__construct'));
$this->assertNull($m->getAccessPath());
$m->setAccessPathFrom($o);
$this->assertEquals('new \\TestClass()', $m->getAccessPath());

$m = new Kint_Object_Method(new ReflectionMethod('TestClass', 'static_method'));
$this->assertNull($m->getAccessPath());
$m->setAccessPathFrom($o);
$this->assertEquals('\\TestClass::static_method()', $m->getAccessPath());

$m = new Kint_Object_Method(new ReflectionMethod('TestClass', 'final_method'));
$this->assertNull($m->getAccessPath());
$m->setAccessPathFrom($o);
$this->assertEquals('$tc->final_method()', $m->getAccessPath());

$m = new Kint_Object_Method(new ReflectionMethod('TestClass', 'mix'));
$this->assertNull($m->getAccessPath());
$m->setAccessPathFrom($o);
$this->assertEquals(
'\\TestClass::mix(array &$x, TestClass $y = null, $z = array(...), $_ = \'string\')',
$m->getAccessPath()
);
}

public function testGetValueShort()
{
$m = new Kint_Object_Method(new ReflectionMethod('TestClass', '__construct'));
$this->assertEquals(
'This is a constructor for a TestClass with the first line of the docstring split into two different lines.',
$m->getValueShort()
);
}

public function testGetModifiers()
{
$m = new Kint_Object_Method(new ReflectionMethod('TestClass', 'static_method'));
$this->assertEquals('private static', $m->getModifiers());

$m = new Kint_Object_Method(new ReflectionMethod('TestClass', 'final_method'));
$this->assertEquals('final public', $m->getModifiers());

$m = new Kint_Object_Method(new ReflectionMethod('ReflectionFunctionAbstract', '__toString'));
$this->assertEquals('abstract public', $m->getModifiers());

$m = new Kint_Object_Method(new ReflectionMethod('TestClass', 'mix'));
$this->assertEquals('final protected static', $m->getModifiers());
}

public function testGetAccessPath()
{
$m = new Kint_Object_Method(new ReflectionMethod('TestClass', 'array_hint'));
$this->assertNull($m->getAccessPath());
$m->access_path = '$m->array_hint';
$this->assertEquals('$m->array_hint(array $x)', $m->getAccessPath());
}

public function testGetParams()
{
$m = new Kint_Object_Method(new ReflectionFunction('explode'));
if (defined('HHVM_VERSION')) {
$this->assertStringStartsWith('$delimiter, $str, $limit = ', $m->getParams());
} else {
$this->assertEquals('$separator, $str, $limit', $m->getParams());
}

$m = new Kint_Object_Method(new ReflectionMethod('TestClass', 'array_hint'));
$this->assertEquals('array $x', $m->getParams());

$m = new Kint_Object_Method(new ReflectionMethod('TestClass', 'class_hint'));
$this->assertEquals('TestClass $x', $m->getParams());

$m = new Kint_Object_Method(new ReflectionMethod('TestClass', 'ref'));
$this->assertEquals('&$x', $m->getParams());

$m = new Kint_Object_Method(new ReflectionMethod('TestClass', 'default_method'));
$this->assertEquals('$x = 1234', $m->getParams());

$m = new Kint_Object_Method(new ReflectionMethod('TestClass', 'mix'));
$this->assertEquals(
'array &$x, TestClass $y = null, $z = array(...), $_ = \'string\'',
$m->getParams()
);
}

public function testGetPhpDocUrl()
{
$m = new Kint_Object_Method(new ReflectionMethod('ReflectionMethod', '__construct'));
$this->assertEquals(
'https://secure.php.net/manual/en/reflectionmethod.construct.php',
$m->getPhpDocUrl()
);
}

public function testGetPhpDocUrlParent()
{
$m = new Kint_Object_Method(new ReflectionMethod('ReflectionMethod', '__clone'));
$this->assertEquals(
'https://secure.php.net/manual/en/reflectionfunctionabstract.clone.php',
$m->getPhpDocUrl()
);
}

public function testGetPhpDocUrlUserDefined()
{
$m = new Kint_Object_Method(new ReflectionMethod(__CLASS__, __FUNCTION__));
$this->assertNull($m->getPhpDocUrl());
}
}
34 changes: 34 additions & 0 deletions tests/TestClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,44 @@ class TestClass
protected static $prostat;
private static $pristat;

/**
* This is a constructor for a TestClass with the first
* line of the docstring split into two different lines.
*
* And here's some more information about it
*/
public function __construct()
{
$this->pub = array('pub');
$this->pro = array('pro');
$this->pri = array('pri');
}

private static function static_method()
{
}

final public function final_method()
{
}

private function array_hint(array $x)
{
}

private function class_hint(TestClass $x)
{
}

private function ref(&$x)
{
}

private function default_method($x = 1234)
{
}

final protected static function mix(array &$x, TestClass $y = null, $z = array(1, 2, 3), $_ = 'string')
{
}
}

0 comments on commit 17ec4f2

Please sign in to comment.