Skip to content

Commit 9c9af00

Browse files
committed
Improve typehints
1 parent 418c95d commit 9c9af00

File tree

8 files changed

+29
-17
lines changed

8 files changed

+29
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Filesystem errors on some Windows environments when running coilpack install command
1111
- GraphQL fieldtype modifier registration bug
1212
- Use coding standard
13+
- Improved typehints
1314

1415
## [0.0.1] - 2022-12-29
1516

src/Fieldtypes/Fieldtype.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
abstract class Fieldtype
99
{
10-
public function __construct($name, $id = null)
10+
public function __construct(string $name, $id = null)
1111
{
1212
$this->name = $name;
1313
$this->id = $id;
@@ -19,14 +19,14 @@ public function __construct($name, $id = null)
1919
* @param FieldContent $content
2020
* @return FieldtypeOutput
2121
*/
22-
abstract public function apply(FieldContent $content, $parameters = []);
22+
abstract public function apply(FieldContent $content, array $parameters = []);
2323

2424
public function modifiers()
2525
{
2626
return [];
2727
}
2828

29-
public function callModifier(FieldContent $content, $name, $parameters)
29+
public function callModifier(FieldContent $content, string $name, array $parameters = [])
3030
{
3131
if (array_key_exists($name, $this->modifiers()) && $this->modifiers()[$name] instanceof Modifier) {
3232
return $this->modifiers()[$name]->handle($content, $parameters);

src/Fieldtypes/File.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class File extends Generic
1010
{
11-
public function apply(FieldContent $content, $parameters = [])
11+
public function apply(FieldContent $content, array $parameters = [])
1212
{
1313
$handler = $this->getHandler();
1414
$data = $handler->pre_process($content->data);

src/Fieldtypes/FluidField.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
class FluidField extends Fieldtype
1212
{
13-
public function apply(FieldContent $content, $parameters = [])
13+
14+
public function apply(FieldContent $content, array $parameters = [])
1415
{
1516
$data = $this->loadData($content);
1617

src/Fieldtypes/Generic.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Generic extends Fieldtype
1010
{
1111
private $handler = null;
1212

13-
public function __construct($name, $id = null)
13+
public function __construct(string $name, $id = null)
1414
{
1515
$this->name = $name;
1616
$this->id = $id;
@@ -34,19 +34,23 @@ public function getHandler()
3434
return $this->handler;
3535
}
3636

37-
public function apply(FieldContent $content, $parameters = [])
37+
public function apply(FieldContent $content, array $parameters = [])
3838
{
3939
$handler = $this->getHandler();
4040

4141
$data = $content->getAttribute('data');
42-
$processed = $handler->replace_tag($data, $parameters);
4342

44-
return FieldtypeOutput::make($processed);
43+
$output = \Expressionengine\Coilpack\Facades\Coilpack::isolateTemplateLibrary(function ($template) use ($handler, $data, $parameters) {
44+
$output = $handler->replace_tag($data, $parameters);
45+
// If the Fieldtype stored data for us in the template library that is preferable to the generated output
46+
return $template->get_data() ?: $output;
47+
});
48+
49+
return FieldtypeOutput::make($output);
4550
}
4651

4752
public function modifiers()
4853
{
49-
// return [];
5054
// @todo Cache this statically
5155
return collect(get_class_methods($this->getHandler()))->flatMap(function ($method) {
5256
if (Str::startsWith($method, 'replace_') && $method !== 'replace_tag') {

src/Fieldtypes/Grid.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414

1515
class Grid extends Fieldtype implements GeneratesGraphType, ListsGraphType
1616
{
17-
public function apply(FieldContent $content, $parameters = [])
17+
public function apply(FieldContent $content, array $parameters = [])
1818
{
1919
$data = $this->loadData($content, $parameters);
2020

2121
return FieldtypeOutput::make($data);
2222
}
2323

24-
protected function loadData(FieldContent $content, $parameters = [])
24+
protected function loadData(FieldContent $content, array $parameters = [])
2525
{
2626
if (! isset($content->entry_id)) {
2727
return [];

src/Fieldtypes/OptionFieldtype.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class OptionFieldtype extends Generic
99
{
10-
public function apply(FieldContent $content, $parameters = [])
10+
public function apply(FieldContent $content, array $parameters = [])
1111
{
1212
$handler = $this->getHandler();
1313

src/Models/FieldContent.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ public function getFieldtype()
4040
return $this->fieldtype;
4141
}
4242

43-
public function value($parameters = [])
43+
/**
44+
* Get the value of this Field Content by applying the fieldtype
45+
*
46+
* @param array $parameters
47+
* @return \Expressionengine\Coilpack\FieldtypeOutput
48+
*/
49+
public function value(array $parameters = [])
4450
{
4551
// should hash and cache params too
4652
if (empty($parameters) && array_key_exists('value', $this->attributes)) {
@@ -57,14 +63,14 @@ public function value($parameters = [])
5763
return $value;
5864
}
5965

60-
public function callModifier($name, $parameters = [])
66+
public function callModifier(string $name, $parameters = [])
6167
{
6268
$fieldtype = $this->getFieldtype();
6369

64-
return $fieldtype->callModifier($this, $name, $parameters);
70+
return $fieldtype->callModifier($this, $name, is_array($parameters) ? $parameters : []);
6571
}
6672

67-
public function getIterator()
73+
public function getIterator(): \Traversable
6874
{
6975
return new \ArrayIterator($this->value()->getIterator());
7076
}

0 commit comments

Comments
 (0)