Skip to content

Commit a66b6c4

Browse files
committed
chore: update examples, and add just file
Signed-off-by: azjezz <azjezz@protonmail.com>
1 parent dc509e9 commit a66b6c4

File tree

3 files changed

+149
-3
lines changed

3 files changed

+149
-3
lines changed

Justfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
default:
2+
@just --list
3+
4+
# build the library
5+
build:
6+
cargo build
7+
8+
# regenerate schema
9+
update-examples:
10+
rm examples/simple.php examples/complete.php
11+
cargo run --example simple >> examples/simple.php
12+
cargo run --example complete >> examples/complete.php
13+
14+
# detect linting problems.
15+
lint:
16+
cargo fmt --all -- --check
17+
cargo clippy
18+
19+
# fix linting problems.
20+
fix:
21+
cargo fmt
22+
cargo clippy --fix --allow-dirty --allow-staged

examples/complete.php

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,51 @@
22

33
declare(strict_types=1);
44

5-
namespace App;
5+
namespace Foo;
6+
7+
use Foo\Bar\Baz as Qux;
8+
use Throwable;
9+
use DateTime;
10+
use DateTimeImmutable;
11+
12+
use function strlen;
13+
use function array_map;
14+
use function array_filter;
15+
16+
use const Foo\HELLO;
17+
use const Foo\HEY;
18+
19+
20+
const A = "Hello World!";
21+
const B = null;
22+
const C = 1;
23+
const D = false;
24+
const E = null;
25+
const F = 213.412;
26+
const G = [1, 25];
627

728
/**
8-
* Format a string with the given arguments using sprintf.
29+
* This is a simple hello function.
30+
*
31+
* @param non-empty-string $firstname
932
*
10-
* @param non-empty-string $template
33+
* @return string
1134
*
1235
* @pure
1336
*/
37+
#[Qux(foo: 1, bar: 2)]
38+
function hello(
39+
#[Validation\NotBlank, Validation\Length(min: 2, max: 10)]
40+
string $firstname,
41+
string $lastname = Qux::Foo,
42+
): string {
43+
return 'Hello ' . $firstname . ' ' . $lastname . '!';
44+
}
45+
46+
function nothing(): void {
47+
// empty body
48+
}
49+
1450
function format(
1551
string $template,
1652
int|float|string|null ...$args,
@@ -21,3 +57,73 @@ function format(
2157
));
2258
}
2359

60+
/**
61+
* This is an example class.
62+
*
63+
* @immutable
64+
*/
65+
abstract class Example
66+
{
67+
final const A = "Hello World!";
68+
69+
protected const B = null;
70+
71+
private const C = 1;
72+
73+
public const D = false;
74+
75+
private string $foo;
76+
77+
protected string $bar;
78+
79+
public string|int $baz = "Hello World!";
80+
81+
/**
82+
* This is a simple hello function.
83+
*
84+
* @param non-empty-string $firstname
85+
*
86+
* @return string
87+
*
88+
* @pure
89+
*/
90+
#[Qux(foo: 1, bar: 2), Qux(foo: 1, bar: 2)]
91+
function hello(
92+
string $firstname,
93+
string $lastname = Qux::Foo,
94+
): string {
95+
return 'Hello ' . $firstname . ' ' . $lastname . '!';
96+
}
97+
98+
/**
99+
* This is a simple x function.
100+
*
101+
* @pure
102+
*/
103+
#[Foo(foo: 1, bar: 2), Bar(foo: 1, bar: 2)]
104+
#[Baz, Qux]
105+
public function x(): mixed {
106+
return 'Hello!';
107+
}
108+
109+
/**
110+
* This is a simple poop function.
111+
*/
112+
public abstract function poop(): void;
113+
114+
/**
115+
* This is a simple echo function.
116+
*/
117+
public final function helloWorld(): void {
118+
echo 'Hello World!';
119+
}
120+
}
121+
122+
interface Formatter
123+
{
124+
public function format(
125+
string $template,
126+
int|float|string|null ...$args,
127+
): string;
128+
}
129+

examples/complete.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use php_codegen::constant::Constant;
66
use php_codegen::data_type::DataType;
77
use php_codegen::file::File;
88
use php_codegen::function::Function;
9+
use php_codegen::interface::Interface;
910
use php_codegen::literal::Value;
1011
use php_codegen::method::Method;
1112
use php_codegen::modifiers::Modifier;
@@ -171,6 +172,23 @@ fn main() {
171172
indentation.indent("echo 'Hello World!';", level)
172173
}),
173174
),
175+
)
176+
.interface(
177+
Interface::new("Formatter").method(
178+
Method::new("format")
179+
.parameter(Parameter::new("template").typed(DataType::String))
180+
.parameter(
181+
Parameter::new("args")
182+
.variadic()
183+
.typed(DataType::Union(vec![
184+
DataType::Integer,
185+
DataType::Float,
186+
DataType::String,
187+
DataType::Null,
188+
])),
189+
)
190+
.returns(DataType::String),
191+
),
174192
);
175193

176194
print!("{file}");

0 commit comments

Comments
 (0)