From 52858e44b085f0cc9f978528d4c9d2603b744cd7 Mon Sep 17 00:00:00 2001 From: Lina Wolf <48202465+linawolf@users.noreply.github.com> Date: Thu, 2 May 2024 11:34:05 +0200 Subject: [PATCH] [TASK] Introduce phpstan (#95) And fix issues up to level 3 --- Build/phpstan-baseline.neon | 0 Build/phpstan.neon | 17 ++++++++++++++ Classes/Domain/Model/Blog.php | 2 +- Classes/Domain/Model/FrontendUser.php | 23 ------------------- Classes/Domain/Model/Post.php | 2 +- Classes/Domain/Model/Tag.php | 2 +- .../Repository/AdministratorRepository.php | 2 ++ Classes/Domain/Repository/BlogRepository.php | 2 ++ .../Domain/Repository/CommentRepository.php | 3 ++- .../Domain/Repository/PersonRepository.php | 3 ++- Classes/Domain/Repository/PostRepository.php | 1 + Classes/Domain/Validator/BlogValidator.php | 5 ---- Classes/Service/BlogFactory.php | 12 +++++----- Classes/Service/BlogValidationService.php | 2 +- Makefile | 9 ++++++++ composer.json | 2 ++ 16 files changed, 47 insertions(+), 40 deletions(-) create mode 100644 Build/phpstan-baseline.neon create mode 100644 Build/phpstan.neon diff --git a/Build/phpstan-baseline.neon b/Build/phpstan-baseline.neon new file mode 100644 index 0000000..e69de29 diff --git a/Build/phpstan.neon b/Build/phpstan.neon new file mode 100644 index 0000000..ca3d3c8 --- /dev/null +++ b/Build/phpstan.neon @@ -0,0 +1,17 @@ +includes: + - phpstan-baseline.neon +parameters: + level: 3 + + inferPrivatePropertyTypeFromConstructor: true + treatPhpDocTypesAsCertain: false + + paths: + - ../ + + tmpDir: ../.Build/.cache/phpstan/ + + excludePaths: + - '*/Build/node_modules' + - '../ext_emconf.php' + diff --git a/Classes/Domain/Model/Blog.php b/Classes/Domain/Model/Blog.php index e130c6b..7d022c4 100644 --- a/Classes/Domain/Model/Blog.php +++ b/Classes/Domain/Model/Blog.php @@ -16,8 +16,8 @@ * * The TYPO3 project - inspiring people to share! */ -use T3docs\BlogExample\Domain\Model\Extbase\ORM\Lazy; use TYPO3\CMS\Extbase\Annotation\ORM\Cascade; +use TYPO3\CMS\Extbase\Annotation\ORM\Lazy; use TYPO3\CMS\Extbase\Annotation\Validate; use TYPO3\CMS\Extbase\Domain\Model\Category; use TYPO3\CMS\Extbase\DomainObject\AbstractEntity; diff --git a/Classes/Domain/Model/FrontendUser.php b/Classes/Domain/Model/FrontendUser.php index b774f15..c53d4ad 100644 --- a/Classes/Domain/Model/FrontendUser.php +++ b/Classes/Domain/Model/FrontendUser.php @@ -16,7 +16,6 @@ * * The TYPO3 project - inspiring people to share! */ -use TYPO3\CMS\Extbase\Domain\Model\FileReference; use TYPO3\CMS\Extbase\DomainObject\AbstractEntity; use TYPO3\CMS\Extbase\Persistence\ObjectStorage; @@ -36,7 +35,6 @@ class FrontendUser extends AbstractEntity public function __construct() { $this->usergroup = new ObjectStorage(); - $this->image = new ObjectStorage(); } /** @@ -45,7 +43,6 @@ public function __construct() public function initializeObject(): void { $this->usergroup = $this->usergroup ?? new ObjectStorage(); - $this->image = $this->image ?? new ObjectStorage(); } /** @@ -85,24 +82,4 @@ public function getUsergroup(): ObjectStorage { return $this->usergroup; } - - /** - * Sets the image value - * - * @param ObjectStorage $image - */ - public function setImage(ObjectStorage $image): void - { - $this->image = $image; - } - - /** - * Gets the image value - * - * @return ObjectStorage - */ - public function getImage(): ObjectStorage - { - return $this->image; - } } diff --git a/Classes/Domain/Model/Post.php b/Classes/Domain/Model/Post.php index c9d59ef..d5494ba 100644 --- a/Classes/Domain/Model/Post.php +++ b/Classes/Domain/Model/Post.php @@ -16,8 +16,8 @@ * * The TYPO3 project - inspiring people to share! */ -use T3docs\BlogExample\Domain\Model\Extbase\ORM\Lazy; use TYPO3\CMS\Extbase\Annotation\ORM\Cascade; +use TYPO3\CMS\Extbase\Annotation\ORM\Lazy; use TYPO3\CMS\Extbase\Annotation\Validate; use TYPO3\CMS\Extbase\Domain\Model\Category; use TYPO3\CMS\Extbase\DomainObject\AbstractEntity; diff --git a/Classes/Domain/Model/Tag.php b/Classes/Domain/Model/Tag.php index 529d3eb..2634a23 100644 --- a/Classes/Domain/Model/Tag.php +++ b/Classes/Domain/Model/Tag.php @@ -37,6 +37,6 @@ public function __construct(string $name = '') */ public function __toString(): string { - return $this->getName(); + return $this->name; } } diff --git a/Classes/Domain/Repository/AdministratorRepository.php b/Classes/Domain/Repository/AdministratorRepository.php index 7f6c7d2..51aaa2c 100644 --- a/Classes/Domain/Repository/AdministratorRepository.php +++ b/Classes/Domain/Repository/AdministratorRepository.php @@ -17,9 +17,11 @@ * The TYPO3 project - inspiring people to share! */ +use T3docs\BlogExample\Domain\Model\Administrator; use TYPO3\CMS\Extbase\Persistence\Repository; /** * A repository for administrators + * @extends Repository */ class AdministratorRepository extends Repository {} diff --git a/Classes/Domain/Repository/BlogRepository.php b/Classes/Domain/Repository/BlogRepository.php index 6f5d0ab..71c0f68 100644 --- a/Classes/Domain/Repository/BlogRepository.php +++ b/Classes/Domain/Repository/BlogRepository.php @@ -17,11 +17,13 @@ * The TYPO3 project - inspiring people to share! */ +use T3docs\BlogExample\Domain\Model\Blog; use TYPO3\CMS\Extbase\Persistence\QueryInterface; use TYPO3\CMS\Extbase\Persistence\Repository; /** * A repository for blogs + * @extends Repository */ class BlogRepository extends Repository { diff --git a/Classes/Domain/Repository/CommentRepository.php b/Classes/Domain/Repository/CommentRepository.php index 486e574..fe0288e 100644 --- a/Classes/Domain/Repository/CommentRepository.php +++ b/Classes/Domain/Repository/CommentRepository.php @@ -17,6 +17,7 @@ * The TYPO3 project - inspiring people to share! */ +use T3docs\BlogExample\Domain\Model\Comment; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface; use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings; @@ -25,7 +26,7 @@ use TYPO3\CMS\Extbase\Persistence\Repository; /** - * A repository for persons + * @extends Repository */ class CommentRepository extends Repository { diff --git a/Classes/Domain/Repository/PersonRepository.php b/Classes/Domain/Repository/PersonRepository.php index 7afa514..816b3f6 100644 --- a/Classes/Domain/Repository/PersonRepository.php +++ b/Classes/Domain/Repository/PersonRepository.php @@ -17,11 +17,12 @@ * The TYPO3 project - inspiring people to share! */ +use T3docs\BlogExample\Domain\Model\Person; use TYPO3\CMS\Extbase\Persistence\QueryInterface; use TYPO3\CMS\Extbase\Persistence\Repository; /** - * A repository for persons + * @extends Repository */ class PersonRepository extends Repository { diff --git a/Classes/Domain/Repository/PostRepository.php b/Classes/Domain/Repository/PostRepository.php index 9c6bae3..e028b70 100644 --- a/Classes/Domain/Repository/PostRepository.php +++ b/Classes/Domain/Repository/PostRepository.php @@ -25,6 +25,7 @@ /** * A repository for blog posts + * @extends Repository * * @method Post findByUid($uid) */ diff --git a/Classes/Domain/Validator/BlogValidator.php b/Classes/Domain/Validator/BlogValidator.php index de9a619..5238ed0 100644 --- a/Classes/Domain/Validator/BlogValidator.php +++ b/Classes/Domain/Validator/BlogValidator.php @@ -29,11 +29,6 @@ final class BlogValidator extends AbstractValidator { public function __construct(private readonly BlogValidationService $blogValidationService) {} - /** - * Checks whether the given blog is valid - * - * @param Blog $blog The blog - */ protected function isValid(mixed $value): void { if (!$value instanceof Blog) { diff --git a/Classes/Service/BlogFactory.php b/Classes/Service/BlogFactory.php index 88b6d95..3df9f13 100644 --- a/Classes/Service/BlogFactory.php +++ b/Classes/Service/BlogFactory.php @@ -5,9 +5,9 @@ namespace T3docs\BlogExample\Service; use T3docs\BlogExample\Domain\Model\Administrator; -use T3docs\BlogExample\Domain\Model\Author; use T3docs\BlogExample\Domain\Model\Blog; use T3docs\BlogExample\Domain\Model\Comment; +use T3docs\BlogExample\Domain\Model\Person; use T3docs\BlogExample\Domain\Model\Post; use T3docs\BlogExample\Domain\Model\Tag; use TYPO3\CMS\Core\SingletonInterface; @@ -43,16 +43,16 @@ public function createBlog(int $blogNumber = 1): Blog // initialize blog $blog = new Blog(); $blog->setTitle('Blog #' . $blogNumber); - $blog->setDescription('A blog about TYPO3 extension development.'); + $blog->description = 'A blog about TYPO3 extension development.'; // create author - $author = new Author('Stephen', 'Smith', 'foo.bar@example.com'); + $author = new Person('Stephen', 'Smith', 'foo.bar@example.com'); // create administrator $administrator = new Administrator(); - $administrator->setName('John Doe'); - $administrator->setEmail('john.doe@example.com'); - $blog->setAdministrator($administrator); + $administrator->name = 'John Doe'; + $administrator->email = 'john.doe@example.com'; + $blog->administrator = $administrator; // create sample posts for ($postNumber = 1; $postNumber < 6; $postNumber++) { diff --git a/Classes/Service/BlogValidationService.php b/Classes/Service/BlogValidationService.php index cb33995..fbeaae0 100644 --- a/Classes/Service/BlogValidationService.php +++ b/Classes/Service/BlogValidationService.php @@ -35,6 +35,6 @@ public function isBlogCategoryCountValid(Blog $blog) public function isBlogSubtitleValid(Blog $blog) { - return strtolower($blog->getTitle()) !== strtolower($blog->getSubtitle()); + return strtolower($blog->getTitle()) !== strtolower($blog->subtitle); } } diff --git a/Makefile b/Makefile index 5eaa26c..65a2b3f 100644 --- a/Makefile +++ b/Makefile @@ -10,3 +10,12 @@ rector: ## Run rector .PHONY: fix-cs fix-cs: ## Fix PHP coding styles .Build/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php + +.PHONY: phpstan +phpstan: ## Run phpstan tests + .Build/bin/phpstan --configuration=Build/phpstan.neon + +.PHONY: phpstan-baseline +phpstan-baseline: ## Update the phpstan baseline + .Build/bin/phpstan --configuration=Build/phpstan.neon --generate-baseline=Build/phpstan-baseline.neon -v + diff --git a/composer.json b/composer.json index f49dd8c..56f8ea8 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,7 @@ "source": "https://github.com/TYPO3-Documentation/blog_example" }, "require": { + "typo3/cms-backend": "^13.1 || dev-main", "typo3/cms-core": "^13.1 || dev-main", "typo3/cms-extbase": "^13.1 || dev-main", "typo3/cms-fluid": "^13.1 || dev-main" @@ -22,6 +23,7 @@ "require-dev": { "ergebnis/composer-normalize": "~2.42.0", "friendsofphp/php-cs-fixer": "^3.52", + "phpstan/phpstan": "^1.10", "ssch/typo3-rector": "^2.5" }, "minimum-stability": "dev",