php-kilo/test.php
Timothy Warren 99685f3fcb
All checks were successful
timw4mail/php-kilo/pipeline/head This commit looks good
Some refactoring
2021-03-09 17:22:49 -05:00

124 lines
1.8 KiB
PHP

<?php declare(strict_types=1);
interface Ifoo {}
abstract class Foo implements Ifoo {
/**
* @param int $a
* @param float $b
* @param array $c
* @param callable $d
* @param string $e
* @return string
*/
abstract public function bar(int $a, float $b, array $c, callable $d, string $e): string;
protected function doNothing(): void {}
}
class Test {
public function __construct(public string $foo, public string $bar) {}
}
/**
* Docblock comment
*/
class FooBar extends Foo implements Ifoo {
public function bar(int $a, float $b, array $c, callable $d, string $e = 'default'): string
{
$cstr = print_r($c, TRUE);
$d();
return "{$a}, ${b}, " . $cstr;
}
private function operations(int $a, int $b): int
{
$this?->x?->bar();
$this->doNothing();
$c = $a + $b;
$a = $c - $b;
$c = $a * $b;
$b = (int) ($c / $a);
return $c;
}
}
trait Baz {
public function about(): array
{
return [
'__CLASS__' => __CLASS__,
'__DIR__' => __DIR__,
'__FILE__' => __FILE__,
'__FUNCTION__' => __FUNCTION__,
'__LINE__' => __LINE__,
'__METHOD__' => __METHOD__,
'__NAMESPACE__' => __NAMESPACE__,
'__TRAIT__' => __TRAIT__,
];
}
}
$square = fn (int $x) => $x ** 2;
foreach ([-1, 0, 1, 2] as $x)
{
$not = match ($x) {
0 => 1,
1,-1 => 0,
default => 0,
};
}
/*
* Multi-line comment
*/
$foobar = new FooBar();
$baz = ['a' => 'b'];
// C++ style comment
$x = 3;
# Perl-style comment
$y = [
1,
2,
3
];
// Multi-line ternary statement
#{Attribute]
$q = ($x !== 2)
? 'yes'
: 'no';
/*
Heredoc
*/$z = $x + $y;
$sql = <<<SQL
SELECT * FROM "foo" WHERE "bar"='baz' AND id={$x};
SQL;
/* Nowdoc */
$template = <<<'TEMPLATE'
<foo>{x}</foo>
TEMPLATE;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML</title>
</head>
<body>
<h1><?= $_SERVER['HTTP_HOST'] ?></h1>
</body>
</html>
<?php exit(); ?>