Skip to content

Commit b9a63f3

Browse files
feat: New DocComment and Field reflector classes
first steps fix CS issues
1 parent 6fd4c4f commit b9a63f3

File tree

3 files changed

+129
-13
lines changed

3 files changed

+129
-13
lines changed

src/Form/Field/BlocksField.php

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Kirby\Exception\InvalidArgumentException;
1313
use Kirby\Exception\NotFoundException;
1414
use Kirby\Form\FieldClass;
15+
use Kirby\Form\Fields;
1516
use Kirby\Form\Form;
1617
use Kirby\Form\Mixin\EmptyState;
1718
use Kirby\Form\Mixin\Max;
@@ -25,29 +26,60 @@ class BlocksField extends FieldClass
2526
use Max;
2627
use Min;
2728

29+
/**
30+
* Defines the allowed block types in the blocks field. See below.
31+
*/
2832
protected Fieldsets $fieldsets;
2933
protected array $forms;
34+
35+
/**
36+
* Group name to identify all block fields that can share blocks via drag & drop
37+
*/
3038
protected string|null $group;
39+
40+
/**
41+
* Saves pretty printed JSON in text files
42+
*/
3143
protected bool $pretty;
3244
protected mixed $value = [];
3345

3446
public function __construct(
35-
string|array|null $empty = null,
36-
string|array|null $fieldsets = null,
47+
bool $autofocus = false,
48+
array $default = [],
49+
bool $disabled = false,
50+
array|string|null $empty = null,
51+
array|string|null $fieldsets = null,
52+
array|string|null $help = null,
3753
string|null $group = null,
54+
array|string|null $label = null,
55+
ModelWithContent|null $model = null,
56+
string|null $name = null,
3857
int|null $max = null,
3958
int|null $min = null,
4059
bool $pretty = false,
41-
...$props
60+
bool $required = false,
61+
Fields|null $siblings = null,
62+
bool $translate = true,
63+
array $when = [],
64+
string|null $width = '1/1',
4265
) {
43-
$this->setFieldsets(
44-
$fieldsets,
45-
$props['model'] ?? App::instance()->site()
66+
parent::__construct(
67+
autofocus: $autofocus,
68+
default: $default,
69+
disabled: $disabled,
70+
help: $help,
71+
label: $label,
72+
model: $model,
73+
name: $name,
74+
required: $required,
75+
siblings: $siblings,
76+
translate: $translate,
77+
when: $when,
78+
width: $width
4679
);
4780

48-
parent::__construct(...$props);
49-
5081
$this->setEmpty($empty);
82+
$this->setFieldsets($fieldsets);
5183
$this->setGroup($group);
5284
$this->setMax($max);
5385
$this->setMin($min);
@@ -262,17 +294,15 @@ protected function setDefault(mixed $default = null): void
262294
parent::setDefault($default);
263295
}
264296

265-
protected function setFieldsets(
266-
string|array|null $fieldsets,
267-
ModelWithContent $model
268-
): void {
297+
protected function setFieldsets(string|array|null $fieldsets): void
298+
{
269299
if (is_string($fieldsets) === true) {
270300
$fieldsets = [];
271301
}
272302

273303
$this->fieldsets = Fieldsets::factory(
274304
$fieldsets,
275-
['parent' => $model]
305+
['parent' => $this->model]
276306
);
277307
}
278308

src/Reflection/DocComment.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Kirby\Reflection;
4+
5+
use ReflectionClass;
6+
use ReflectionFunction;
7+
use ReflectionMethod;
8+
use ReflectionProperty;
9+
10+
class DocComment
11+
{
12+
public function __construct(
13+
protected string $comment,
14+
) {
15+
}
16+
17+
public static function from(
18+
ReflectionClass|ReflectionFunction|ReflectionMethod|ReflectionProperty|null $object
19+
) {
20+
return new static($object?->getDocComment() ?? '');
21+
}
22+
23+
/**
24+
* Returns the cleaned docblock text of the given property.
25+
*/
26+
public function description(): string|null
27+
{
28+
$comment = preg_replace(['#^/\*\*#', '#\*/$#'], '', $this->comment);
29+
$lines = preg_split('/\R/', (string)$comment) ?: [];
30+
$lines = array_map(static fn (string $line): string => ltrim(trim($line), "* \t"), $lines);
31+
$lines = array_filter(
32+
$lines,
33+
static fn (string $line): bool => $line !== '' && str_starts_with($line, '@') === false
34+
);
35+
36+
return implode(' ', $lines);
37+
}
38+
}

src/Reflection/Field.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Kirby\Reflection;
4+
5+
use Kirby\Form\FieldClass;
6+
use ReflectionClass;
7+
8+
class Field
9+
{
10+
protected Constructor $constructor;
11+
protected ReflectionClass $class;
12+
13+
public function __construct(
14+
protected FieldClass $field
15+
) {
16+
$this->class = new ReflectionClass($field);
17+
$this->constructor = new Constructor($field);
18+
}
19+
20+
/**
21+
* Returns field properties based on the constructor signature.
22+
*/
23+
public function props(): array
24+
{
25+
$props = [];
26+
$ignore = ['model', 'siblings', 'props'];
27+
28+
foreach ($this->constructor->getParameters() as $parameter) {
29+
$name = $parameter->getName();
30+
31+
if (in_array($name, $ignore) === true) {
32+
continue;
33+
}
34+
35+
$property = $this->class->hasProperty($name) ? $this->class->getProperty($name) : null;
36+
$comment = DocComment::from($property);
37+
38+
$props[$name] = [
39+
'name' => $name,
40+
'type' => (string)$parameter->getType(),
41+
'default' => $parameter->getDefaultValue(),
42+
'description' => $comment->description()
43+
];
44+
}
45+
46+
return $props;
47+
}
48+
}

0 commit comments

Comments
 (0)