Skip to content

Commit 70c5fbc

Browse files
committed
change: Add strict parameter and return typehints
1 parent 1455baa commit 70c5fbc

13 files changed

+159
-236
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
### Unreleased
22

33
* [BREAKING] Use doctrine attributes instead of annotations for entity metadata
4+
* [BREAKING] Add hard parameter and return typehints to all classes
45

56
### v1.5.0 (2024-10-01)
67

src/BehatContexts/ContentSnippetsContext.php

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,62 +9,54 @@
99

1010
use Behat\Gherkin\Node\PyStringNode;
1111
use Behat\MinkExtension\Context\RawMinkContext;
12+
use Behat\Step\Given;
1213
use Ingenerator\ContentSnippets\Repository\ContentSnippetRepository;
14+
use PHPUnit\Framework\Assert;
15+
use function trim;
1316

1417
class ContentSnippetsContext extends RawMinkContext
1518
{
1619

17-
/**
18-
* @var \Ingenerator\ContentSnippets\Repository\ContentSnippetRepository
19-
*/
20-
protected $repo;
20+
protected ContentSnippetRepository $repo;
2121

2222
public function __construct(ContentSnippetRepository $repo)
2323
{
2424
$this->repo = $repo;
2525
}
2626

27-
/**
28-
* @Given /^the (?P<slug>[^ ]+) snippet has the following content:$/
29-
*/
30-
public function givenContent($slug, PyStringNode $content)
27+
#[Given('/^the (?P<slug>[^ ]+) snippet has the following content:$/')]
28+
public function givenContent(string $slug, PyStringNode $content): void
3129
{
3230
$snippet = $this->repo->load($slug);
3331
$snippet->setContent($content->getRaw());
3432
$this->repo->save($snippet);
3533
}
3634

37-
/**
38-
* @Given /^the (?P<slug>[^ ]+) snippet is empty$/
39-
*/
40-
public function givenEmpty($slug)
35+
#[Given('/^the (?P<slug>[^ ]+) snippet is empty$/')]
36+
public function givenEmpty(string $slug): void
4137
{
4238
$snippet = $this->repo->load($slug);
4339
$snippet->setContent(NULL);
4440
$this->repo->save($snippet);
4541
}
4642

47-
/**
48-
* @When /^I try to update the "(?P<display_name>[^"]+)" snippet with:$/
49-
*/
50-
public function tryToUpdateContent($display_name, PyStringNode $new_content)
43+
#[When('/^I try to update the "(?P<display_name>[^"]+)" snippet with:$/')]
44+
public function tryToUpdateContent(string $display_name, PyStringNode $new_content): void
5145
{
5246
$assert = $this->assertSession();
53-
$table = $assert->elementExists('css', '[data-content-snippets-list]');
47+
$table = $assert->elementExists('css', '[data-content-snippets-list]');
5448
$table->clickLink('Edit '.$display_name);
5549
$page = $this->getSession()->getPage();
5650
$page->fillField('Content', $new_content->getRaw());
5751
$page->pressButton('Save changes');
5852
}
5953

60-
/**
61-
* @Then /^the (?P<selector>.+?) element should have this exact HTML:$/
62-
*/
63-
public function assertElementExactHtml($selector, PyStringNode $expect)
54+
#[Then('/^the (?P<selector>.+?) element should have this exact HTML:$/')]
55+
public function assertElementExactHtml(string $selector, PyStringNode $expect): void
6456
{
6557
$element = $this->assertSession()->elementExists('css', $selector);
66-
$actual = \trim($element->getHtml());
67-
\PHPUnit\Framework\Assert::assertEquals($expect->getRaw(), $actual);
58+
$actual = trim($element->getHtml());
59+
Assert::assertEquals($expect->getRaw(), $actual);
6860
}
6961

7062
}

src/ContentSnippetsDependencyFactory.php

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,45 @@
77
namespace Ingenerator\ContentSnippets;
88

99

10+
use HTMLPurifier_Config;
1011
use Ingenerator\ContentSnippets\Repository\DoctrineContentSnippetRepository;
12+
use function sys_get_temp_dir;
1113

1214
class ContentSnippetsDependencyFactory
1315
{
1416

15-
public static function definitions()
17+
public static function definitions(): array
1618
{
1719
return [
1820
'content_snippets' => [
1921
'content_filter' => [
2022
'_settings' => [
21-
'class' => ContentSnippetContentFilter::class,
23+
'class' => ContentSnippetContentFilter::class,
2224
'arguments' => [
2325
'%content_snippets.html_purifier.purifier%',
2426
],
2527
],
2628
],
27-
'html_purifier' => [
28-
'config' => [
29+
'html_purifier' => [
30+
'config' => [
2931
'_settings' => [
30-
'class' => static::class,
32+
'class' => static::class,
3133
'constructor' => 'makePurifierConfig',
32-
'arguments' => [],
34+
'arguments' => [],
3335
],
3436
],
3537
'purifier' => [
3638
'_settings' => [
37-
'class' => \HTMLPurifier::class,
39+
'class' => \HTMLPurifier::class,
3840
'arguments' => [
3941
'%content_snippets.html_purifier.config%',
4042
],
4143
],
4244
],
4345
],
44-
'repository' => [
46+
'repository' => [
4547
'_settings' => [
46-
'class' => DoctrineContentSnippetRepository::class,
48+
'class' => DoctrineContentSnippetRepository::class,
4749
'arguments' => [
4850
'%doctrine.entity_manager%',
4951
],
@@ -53,25 +55,25 @@ public static function definitions()
5355
];
5456
}
5557

56-
public static function controllerDefinitions()
58+
public static function controllerDefinitions(): array
5759
{
5860
return [];
5961
}
6062

61-
public static function makePurifierConfig()
63+
public static function makePurifierConfig(): HTMLPurifier_Config
6264
{
63-
return \HTMLPurifier_Config::create(
65+
return HTMLPurifier_Config::create(
6466
[
65-
'AutoFormat.RemoveEmpty' => FALSE,
66-
'Attr.AllowedFrameTargets' => ['_blank'],
67-
'AutoFormat.RemoveEmpty.RemoveNbsp' => TRUE,
67+
'AutoFormat.RemoveEmpty' => FALSE,
68+
'Attr.AllowedFrameTargets' => ['_blank'],
69+
'AutoFormat.RemoveEmpty.RemoveNbsp' => TRUE,
6870
'AutoFormat.RemoveSpansWithoutAttributes' => TRUE,
69-
'Cache.SerializerPath' => \sys_get_temp_dir(),
70-
'Core.RemoveProcessingInstructions' => TRUE,
71-
'HTML.Doctype' => 'HTML 4.01 Transitional',
72-
'URI.AllowedSchemes' => ['http', 'https', 'mailto', 'tel'],
73-
'URI.DefaultScheme' => 'https',
74-
'URI.DisableExternalResources' => TRUE,
71+
'Cache.SerializerPath' => sys_get_temp_dir(),
72+
'Core.RemoveProcessingInstructions' => TRUE,
73+
'HTML.Doctype' => 'HTML 4.01 Transitional',
74+
'URI.AllowedSchemes' => ['http', 'https', 'mailto', 'tel'],
75+
'URI.DefaultScheme' => 'https',
76+
'URI.DisableExternalResources' => TRUE,
7577
]
7678
);
7779
}

src/Entity/ContentSnippet.php

Lines changed: 24 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -7,137 +7,97 @@
77
namespace Ingenerator\ContentSnippets\Entity;
88

99

10+
use DateTimeImmutable;
1011
use Doctrine\DBAL\Types\Types;
1112
use Doctrine\ORM\Mapping\ChangeTrackingPolicy;
1213
use Doctrine\ORM\Mapping\Column;
1314
use Doctrine\ORM\Mapping\Entity;
1415
use Doctrine\ORM\Mapping\Id;
1516
use Doctrine\ORM\Mapping\Table;
17+
use InvalidArgumentException;
18+
use function strip_tags;
1619

1720
#[Entity]
1821
#[ChangeTrackingPolicy('DEFERRED_EXPLICIT')]
1922
#[Table(name: 'content_snippets')]
2023
class ContentSnippet
2124
{
2225

23-
/**
24-
* @var string
25-
*/
2626
#[Id]
2727
#[Column(type: Types::STRING)]
28-
protected $slug;
28+
protected string $slug;
2929

30-
/**
31-
* @var string
32-
*/
3330
#[Column(type: Types::STRING)]
34-
protected $display_name;
31+
protected string $display_name;
3532

36-
/**
37-
* @var string
38-
*/
3933
#[Column(type: Types::TEXT, nullable: TRUE)]
40-
protected $help_text;
34+
protected ?string $help_text = NULL;
4135

42-
/**
43-
* @var bool
44-
*/
4536
#[Column(type: Types::BOOLEAN)]
46-
protected $allows_html;
37+
protected bool $allows_html = FALSE;
4738

48-
/**
49-
* @var string
50-
*/
5139
#[Column(type: Types::TEXT, nullable: TRUE)]
52-
protected $content;
40+
protected ?string $content = NULL;
5341

54-
/**
55-
* @var \DateTimeImmutable
56-
*/
5742
#[Column(type: Types::DATETIME_IMMUTABLE)]
58-
protected $updated_at;
43+
protected DateTimeImmutable $updated_at;
5944

60-
/**
61-
* @param string $content
62-
*
63-
* @return bool
64-
*/
65-
public static function isHtmlString($content)
45+
public static function isHtmlString(?string $content): bool
6646
{
6747
if ($content === NULL) {
6848
return FALSE;
6949
}
7050

71-
return $content !== \strip_tags($content);
51+
return $content !== strip_tags($content);
7252
}
7353

74-
/**
75-
* @return string
76-
*/
77-
public function getSlug()
54+
public function getSlug(): string
7855
{
7956
return $this->slug;
8057
}
8158

82-
/**
83-
* @return string
84-
*/
85-
public function getDisplayName()
59+
public function getDisplayName(): string
8660
{
8761
return $this->display_name;
8862
}
8963

90-
/**
91-
* @return string
92-
*/
93-
public function getHelpText()
64+
public function getHelpText(): ?string
9465
{
9566
return $this->help_text;
9667
}
9768

98-
/**
99-
* @return string
100-
*/
101-
public function getContent()
69+
public function getContent(): ?string
10270
{
10371
return $this->content;
10472
}
10573

10674
/**
107-
* @param string $content
108-
*
109-
* @throws \InvalidArgumentException if passing HTML and the snippet doesn't allow it
75+
* @throws InvalidArgumentException if passing HTML and the snippet doesn't allow it
11076
*/
111-
public function setContent($content)
77+
public function setContent(?string $content): void
11278
{
113-
if (( ! $this->allowsHtml()) AND static::isHtmlString($content)) {
114-
throw new \InvalidArgumentException(
79+
if (( ! $this->allowsHtml()) and static::isHtmlString($content)) {
80+
throw new InvalidArgumentException(
11581
'HTML content is not permitted for snippet '.$this->slug
11682
);
11783
}
11884
if ($content !== $this->content) {
119-
$this->content = $content;
120-
$this->updated_at = new \DateTimeImmutable;
85+
$this->content = $content;
86+
$this->updated_at = new DateTimeImmutable;
12187
}
12288
}
12389

124-
/**
125-
* @return bool
126-
*/
127-
public function allowsHtml()
90+
public function allowsHtml(): bool
12891
{
12992
return $this->allows_html;
13093
}
13194

132-
public function hasContent()
95+
public function hasContent(): bool
13396
{
13497
return (bool) $this->content;
13598
}
13699

137-
/**
138-
* @return \DateTimeImmutable
139-
*/
140-
public function getUpdatedAt()
100+
public function getUpdatedAt(): DateTimeImmutable
141101
{
142102
return $this->updated_at;
143103
}

src/Message/ContentSnippetUpdateMessage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
class ContentSnippetUpdateMessage extends Message
1414
{
1515

16-
public static function updated(ContentSnippet $snippet)
16+
public static function updated(ContentSnippet $snippet): ContentSnippetUpdateMessage
1717
{
1818
return new static(
1919
'Success',
@@ -22,7 +22,7 @@ public static function updated(ContentSnippet $snippet)
2222
);
2323
}
2424

25-
public static function updatedCleaned(ContentSnippet $snippet)
25+
public static function updatedCleaned(ContentSnippet $snippet): ContentSnippetUpdateMessage
2626
{
2727
return new static(
2828
'Updated after tidying',

0 commit comments

Comments
 (0)