Skip to content

Commit

Permalink
coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 26, 2023
1 parent a9a4048 commit 52d62be
Show file tree
Hide file tree
Showing 27 changed files with 649 additions and 503 deletions.
8 changes: 5 additions & 3 deletions tests/PhpGenerator/ClassType.addMember.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Assert::same('', $method->getBody());
// duplicity
$class = new ClassType('Example');
$class->addMember(new Nette\PhpGenerator\Method('foo'));
Assert::exception(function () use ($class) {
$class->addMember(new Nette\PhpGenerator\Method('FOO'));
}, Nette\InvalidStateException::class, "Cannot add member 'FOO', because it already exists.");
Assert::exception(
fn() => $class->addMember(new Nette\PhpGenerator\Method('FOO')),
Nette\InvalidStateException::class,
"Cannot add member 'FOO', because it already exists.",
);
10 changes: 6 additions & 4 deletions tests/PhpGenerator/ClassType.from.bodies.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ Assert::exception(function () {
}, Nette\InvalidStateException::class, 'Source code of PDO not found.');


Assert::exception(function () {
ClassType::from(new class {
Assert::exception(
fn() => ClassType::from(new class {
public function f()
{
}
}, withBodies: true);
}, Nette\NotSupportedException::class, 'The $withBodies parameter cannot be used for anonymous functions.');
}, withBodies: true),
Nette\NotSupportedException::class,
'The $withBodies parameter cannot be used for anonymous functions.',
);


$res = ClassType::from(Abc\Class7::class, withBodies: true);
Expand Down
63 changes: 37 additions & 26 deletions tests/PhpGenerator/ClassType.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Assert::true($p->isPublic());
$m = $class->addMethod('getHandle')
->addComment('Returns file handle.')
->addComment('@return resource')
->setFinal(true)
->setFinal()
->setBody('return $this->?;', ['handle']);

Assert::same($m, $class->getMethod('getHandle'));
Expand All @@ -102,9 +102,9 @@ Assert::same('public', $m->getVisibility());
Assert::same('return $this->handle;', $m->getBody());

$m = $class->addMethod('getSections')
->setStatic(true)
->setStatic()
->setVisibility('protected')
->setReturnReference(true)
->setReturnReference()
->addBody('$mode = ?;', [123])
->addBody('return self::$sections;');
$m->addParameter('mode', new Literal('self::ORDER'));
Expand All @@ -120,19 +120,19 @@ Assert::true($m->isProtected());
Assert::false($m->isPublic());

$method = $class->addMethod('show')
->setAbstract(true);
->setAbstract();

$method->addParameter('foo');
$method->removeParameter('foo');

$method->addParameter('item');

$method->addParameter('res', null)
->setReference(true)
->setReference()
->setType(Type::union(Type::Array, 'null'));

$method->addParameter('bar', null)
->setNullable(true)
->setNullable()
->setType('stdClass|string');

$class->addTrait('foo');
Expand Down Expand Up @@ -168,37 +168,48 @@ $method->setParameters(array_values($parameters));
Assert::same($parameters, $method->getParameters());


Assert::exception(function () {
$class = new ClassType;
$class->addMethod('method')->setVisibility('unknown');
}, Nette\InvalidArgumentException::class, 'Argument must be public|protected|private.');
Assert::exception(
fn() => (new ClassType)->addMethod('method')->setVisibility('unknown'),
Nette\InvalidArgumentException::class,
'Argument must be public|protected|private.',
);


// duplicity
$class = new ClassType('Example');
$class->addConstant('a', 1);
Assert::exception(function () use ($class) {
$class->addConstant('a', 1);
}, Nette\InvalidStateException::class, "Cannot add constant 'a', because it already exists.");
Assert::exception(
fn() => $class->addConstant('a', 1),
Nette\InvalidStateException::class,
"Cannot add constant 'a', because it already exists.",
);

$class->addProperty('a');
Assert::exception(function () use ($class) {
$class->addProperty('a');
}, Nette\InvalidStateException::class, "Cannot add property 'a', because it already exists.");
Assert::exception(
fn() => $class->addProperty('a'),
Nette\InvalidStateException::class,
"Cannot add property 'a', because it already exists.",
);

$class->addMethod('a');
Assert::exception(function () use ($class) {
$class->addMethod('a');
}, Nette\InvalidStateException::class, "Cannot add method 'a', because it already exists.");

Assert::exception(function () use ($class) {
$class->addMethod('A');
}, Nette\InvalidStateException::class, "Cannot add method 'A', because it already exists.");
Assert::exception(
fn() => $class->addMethod('a'),
Nette\InvalidStateException::class,
"Cannot add method 'a', because it already exists.",
);

Assert::exception(
fn() => $class->addMethod('A'),
Nette\InvalidStateException::class,
"Cannot add method 'A', because it already exists.",
);

$class->addTrait('A');
Assert::exception(function () use ($class) {
$class->addTrait('A');
}, Nette\InvalidStateException::class, "Cannot add trait 'A', because it already exists.");
Assert::exception(
fn() => $class->addTrait('A'),
Nette\InvalidStateException::class,
"Cannot add trait 'A', because it already exists.",
);


// remove members
Expand Down
6 changes: 3 additions & 3 deletions tests/PhpGenerator/ClassType.validate.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ require __DIR__ . '/../bootstrap.php';

Assert::exception(function () {
$class = new ClassType('A');
$class->setFinal(true)->setAbstract(true);
$class->setFinal()->setAbstract();
$class->validate();
}, Nette\InvalidStateException::class, "Class 'A' cannot be abstract and final at the same time.");

Assert::exception(function () {
$class = new ClassType('A');
$class->setAbstract(true)->setFinal(true);
$class->setAbstract()->setFinal();
$class->validate();
}, Nette\InvalidStateException::class, "Class 'A' cannot be abstract and final at the same time.");

Assert::exception(function () {
$class = new ClassType;
$class->setAbstract(true);
$class->setAbstract();
$class->validate();
}, Nette\InvalidStateException::class, 'Anonymous class cannot be abstract or final.');
6 changes: 4 additions & 2 deletions tests/PhpGenerator/Closure.from.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ $closure = #[ExampleAttribute] function (stdClass $a, $b = null) {};

$function = Closure::from($closure);
same(
'#[ExampleAttribute] function (stdClass $a, $b = null) {
}',
<<<'XX'
#[ExampleAttribute] function (stdClass $a, $b = null) {
}
XX,
(string) $function,
);
76 changes: 39 additions & 37 deletions tests/PhpGenerator/Closure.long.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,44 @@ for ($name = 'abcde'; $name < 'abcdu'; $name++) {
}

same(
'function (
$abcde,
$abcdf,
$abcdg,
$abcdh,
$abcdi,
$abcdj,
$abcdk,
$abcdl,
$abcdm,
$abcdn,
$abcdo,
$abcdp,
$abcdq,
$abcdr,
$abcds,
$abcdt,
) use (
$abcde,
$abcdf,
$abcdg,
$abcdh,
$abcdi,
$abcdj,
$abcdk,
$abcdl,
$abcdm,
$abcdn,
$abcdo,
$abcdp,
$abcdq,
$abcdr,
$abcds,
$abcdt,
) {
return null;
}',
<<<'XX'
function (
$abcde,
$abcdf,
$abcdg,
$abcdh,
$abcdi,
$abcdj,
$abcdk,
$abcdl,
$abcdm,
$abcdn,
$abcdo,
$abcdp,
$abcdq,
$abcdr,
$abcds,
$abcdt,
) use (
$abcde,
$abcdf,
$abcdg,
$abcdh,
$abcdi,
$abcdj,
$abcdk,
$abcdl,
$abcdm,
$abcdn,
$abcdo,
$abcdp,
$abcdq,
$abcdr,
$abcds,
$abcdt,
) {
return null;
}
XX,
(string) $function,
);
36 changes: 22 additions & 14 deletions tests/PhpGenerator/Closure.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@ require __DIR__ . '/../bootstrap.php';

$function = new Closure;
$function
->setReturnReference(true)
->setReturnReference()
->setBody('return $a + $b;');

$function->addParameter('a');
$function->addParameter('b');
$function->addUse('this');
$function->addUse('vars')
->setReference(true);
->setReference();

same(
'function &($a, $b) use ($this, &$vars) {
return $a + $b;
}',
<<<'XX'
function &($a, $b) use ($this, &$vars) {
return $a + $b;
}
XX,
(string) $function,
);

Expand All @@ -36,9 +38,11 @@ Assert::type(Nette\PhpGenerator\Parameter::class, $uses[1]);
$uses = $function->setUses([$uses[0]]);

same(
'function &($a, $b) use ($this) {
return $a + $b;
}',
<<<'XX'
function &($a, $b) use ($this) {
return $a + $b;
}
XX,
(string) $function,
);

Expand All @@ -58,9 +62,11 @@ $function
->addUse('this');

same(
'function () use ($this): array {
return [];
}',
<<<'XX'
function () use ($this): array {
return [];
}
XX,
(string) $function,
);

Expand All @@ -71,8 +77,10 @@ $function->setBody('return $a + $b;');
$function->addAttribute('ExampleAttribute');

same(
'#[ExampleAttribute] function () {
return $a + $b;
}',
<<<'XX'
#[ExampleAttribute] function () {
return $a + $b;
}
XX,
(string) $function,
);
12 changes: 7 additions & 5 deletions tests/PhpGenerator/Dumper.dump().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,12 @@ Assert::same(
$dumper->dump(new DateTimeImmutable('2016-06-22 20:52:43.1234', new DateTimeZone('Europe/Prague'))),
);
same(
"\\Nette\\PhpGenerator\\Dumper::createObject(\\TestDateTime::class, [
'date' => '2016-06-22 20:52:43.123400',
'timezone_type' => 3,
'timezone' => 'Europe/Prague',
])",
<<<'XX'
\Nette\PhpGenerator\Dumper::createObject(\TestDateTime::class, [
'date' => '2016-06-22 20:52:43.123400',
'timezone_type' => 3,
'timezone' => 'Europe/Prague',
])
XX,
$dumper->dump(new TestDateTime('2016-06-22 20:52:43.1234', new DateTimeZone('Europe/Prague'))),
);
Loading

0 comments on commit 52d62be

Please sign in to comment.