From bacc368c9a38ef4aaa02602ef673be31184ccaf6 Mon Sep 17 00:00:00 2001 From: horea Date: Mon, 10 Mar 2025 18:26:12 +0200 Subject: [PATCH 1/3] Issue#26: Replace Psalm with PHPStan Signed-off-by: horea --- .github/workflows/static-analysis.yml | 49 +++++++++++++++++++ README.md | 20 +++++--- composer.json | 17 ++++--- docs/book/v3/overview.md | 14 ++++++ mkdocs.yml | 2 +- phpstan.neon | 8 +++ psalm.xml | 17 ------- src/FlashMessenger.php | 2 + test/Factory/FlashMessengerFactoryTest.php | 8 +-- .../FlashMessengerOptionsFactoryTest.php | 2 +- .../FlashMessengerRendererFactoryTest.php | 2 +- 11 files changed, 102 insertions(+), 39 deletions(-) create mode 100644 .github/workflows/static-analysis.yml create mode 100644 phpstan.neon delete mode 100644 psalm.xml diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml new file mode 100644 index 0000000..2423564 --- /dev/null +++ b/.github/workflows/static-analysis.yml @@ -0,0 +1,49 @@ +on: + - push + +name: Run PHPStan checks + +jobs: + mutation: + name: PHPStan ${{ matrix.php }}-${{ matrix.os }} + + runs-on: ${{ matrix.os }} + + strategy: + matrix: + os: + - ubuntu-latest + + php: + - "8.1" + - "8.2" + - "8.3" + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "${{ matrix.php }}" + coverage: pcov + ini-values: assert.exception=1, zend.assertions=1, error_reporting=-1, log_errors_max_len=0, display_errors=On + tools: composer:v2, cs2pr + + - name: Determine composer cache directory + run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV + + - name: Cache dependencies installed with composer + uses: actions/cache@v4 + with: + path: ${{ env.COMPOSER_CACHE_DIR }} + key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: | + php${{ matrix.php }}-composer- + + - name: Install dependencies with composer + run: composer install --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi + + - name: Run static analysis with PHPStan + run: vendor/bin/phpstan analyse diff --git a/README.md b/README.md index 9da7076..0c8717b 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,18 @@ # dot-flashmessenger +Flash messenger library for session messages between redirects. +A flash message, or session message is a piece of text data that survives one requests(available only in the next request). +This library accepts session data as well, not just string messages, with the same behaviour. +The flash messenger is a convenient way to add data to the session and get it back on the next request without bothering with setting and clearing the data manually. + +## Documentation + +Documentation is available at: https://docs.dotkernel.org/dot-flashmessenger/. + +## Badges + ![OSS Lifecycle](https://img.shields.io/osslifecycle/dotkernel/dot-flashmessenger) -![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-flashmessenger/3.4.2) +![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-flashmessenger/3.6.0) [![GitHub issues](https://img.shields.io/github/issues/dotkernel/dot-flashmessenger)](https://github.com/dotkernel/dot-flashmessenger/issues) [![GitHub forks](https://img.shields.io/github/forks/dotkernel/dot-flashmessenger)](https://github.com/dotkernel/dot-flashmessenger/network) @@ -10,12 +21,7 @@ [![Build Static](https://github.com/dotkernel/dot-flashmessenger/actions/workflows/continuous-integration.yml/badge.svg?branch=3.0)](https://github.com/dotkernel/dot-flashmessenger/actions/workflows/continuous-integration.yml) [![codecov](https://codecov.io/gh/dotkernel/dot-flashmessenger/graph/badge.svg?token=B4WAT3RYKJ)](https://codecov.io/gh/dotkernel/dot-flashmessenger) - -[![SymfonyInsight](https://insight.symfony.com/projects/94ace687-5124-446f-a324-0ecca1b47f88/big.svg)](https://insight.symfony.com/projects/94ace687-5124-446f-a324-0ecca1b47f88) - -Flash messenger library for session messages between redirects. A flash message, or session message is a piece of text data that survives one requests(available only in the next request). -This library accepts session data as well, not just string messages, with the same behaviour. -The flash messenger is a convenient way to add data to the session and get it back on the next request without bothering with setting and clearing the data manually. +[![PHPStan](https://github.com/dotkernel/dot-flashmessenger/actions/workflows/static-analysis.yml/badge.svg?branch=3.0)](https://github.com/dotkernel/dot-flashmessenger/actions/workflows/static-analysis.yml) ## Installation diff --git a/composer.json b/composer.json index 9d7ffa5..ab83b93 100644 --- a/composer.json +++ b/composer.json @@ -1,12 +1,12 @@ { "name": "dotkernel/dot-flashmessenger", "type": "library", - "description": "DotKernel flash messenger component for session messages between redirects", + "description": "Dotkernel flash messenger component for session messages between redirects", "license": "MIT", "homepage": "https://github.com/dotkernel/dot-flashmessenger", "authors": [ { - "name": "DotKernel Team", + "name": "Dotkernel Team", "email": "team@dotkernel.com" } ], @@ -29,9 +29,10 @@ "mezzio/mezzio-template": "^2.9.0" }, "require-dev": { - "laminas/laminas-coding-standard": "^2.5.0", - "phpunit/phpunit": "^10.4.2", - "vimeo/psalm": "^5.16.0" + "laminas/laminas-coding-standard": "^3.0.0", + "phpstan/phpstan": "^2.1", + "phpstan/phpstan-phpunit": "^2.0", + "phpunit/phpunit": "^10.4.2" }, "autoload": { "psr-4": { @@ -46,12 +47,12 @@ "scripts": { "check": [ "@cs-check", - "@test" + "@test", + "@static-analysis" ], "cs-check": "phpcs", "cs-fix": "phpcbf", "test": "phpunit --colors=always", - "test-coverage": "phpunit --colors=always --coverage-clover clover.xml", - "static-analysis": "psalm --shepherd --stats" + "static-analysis": "phpstan analyse --memory-limit 1G" } } diff --git a/docs/book/v3/overview.md b/docs/book/v3/overview.md index 42f9fbb..22e3c4e 100644 --- a/docs/book/v3/overview.md +++ b/docs/book/v3/overview.md @@ -7,3 +7,17 @@ A flash message, or session message is a piece of text data that survives one re This library accepts session data as well, not just string messages, with the same behaviour. The flash messenger is a convenient way to add data to the session and get it back on the next request without bothering with setting and clearing the data manually. + +## Badges + +![OSS Lifecycle](https://img.shields.io/osslifecycle/dotkernel/dot-flashmessenger) +![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-flashmessenger/3.6.0) + +[![GitHub issues](https://img.shields.io/github/issues/dotkernel/dot-flashmessenger)](https://github.com/dotkernel/dot-flashmessenger/issues) +[![GitHub forks](https://img.shields.io/github/forks/dotkernel/dot-flashmessenger)](https://github.com/dotkernel/dot-flashmessenger/network) +[![GitHub stars](https://img.shields.io/github/stars/dotkernel/dot-flashmessenger)](https://github.com/dotkernel/dot-flashmessenger/stargazers) +[![GitHub license](https://img.shields.io/github/license/dotkernel/dot-flashmessenger)](https://github.com/dotkernel/dot-flashmessenger/blob/3.0/LICENSE.md) + +[![Build Static](https://github.com/dotkernel/dot-flashmessenger/actions/workflows/continuous-integration.yml/badge.svg?branch=3.0)](https://github.com/dotkernel/dot-flashmessenger/actions/workflows/continuous-integration.yml) +[![codecov](https://codecov.io/gh/dotkernel/dot-flashmessenger/graph/badge.svg?token=B4WAT3RYKJ)](https://codecov.io/gh/dotkernel/dot-flashmessenger) +[![PHPStan](https://github.com/dotkernel/dot-flashmessenger/actions/workflows/static-analysis.yml/badge.svg?branch=3.0)](https://github.com/dotkernel/dot-flashmessenger/actions/workflows/static-analysis.yml) diff --git a/mkdocs.yml b/mkdocs.yml index b917205..d697f59 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -14,7 +14,7 @@ nav: - Usage: v3/usage.md - Renderer: v3/renderer.md site_name: dot-flashmessenger -site_description: "DotKernel's one time session message" +site_description: "Dotkernel's one time session message" repo_url: "https://github.com/dotkernel/dot-flashmessenger" plugins: - search diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..349be25 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,8 @@ +includes: + - vendor/phpstan/phpstan-phpunit/extension.neon +parameters: + level: 5 + paths: + - src + - test + treatPhpDocTypesAsCertain: false diff --git a/psalm.xml b/psalm.xml deleted file mode 100644 index 7272b57..0000000 --- a/psalm.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - diff --git a/src/FlashMessenger.php b/src/FlashMessenger.php index 4be3a18..5df1799 100644 --- a/src/FlashMessenger.php +++ b/src/FlashMessenger.php @@ -93,6 +93,7 @@ public function addData( ): void { $container = $this->getSessionContainer(); if (! isset($container->data)) { + /** @phpstan-ignore-next-line */ $container->data = []; } @@ -127,6 +128,7 @@ public function addMessage( ): void { $container = $this->getSessionContainer(); if (! isset($container->messages)) { + /** @phpstan-ignore-next-line */ $container->messages = []; } diff --git a/test/Factory/FlashMessengerFactoryTest.php b/test/Factory/FlashMessengerFactoryTest.php index f3dc097..54e679b 100644 --- a/test/Factory/FlashMessengerFactoryTest.php +++ b/test/Factory/FlashMessengerFactoryTest.php @@ -58,7 +58,7 @@ public function testCreateFlashMessengerWithFoundSessionManager(): void $result = (new FlashMessengerFactory())($this->container, FlashMessenger::class); - $this->assertInstanceOf(FlashMessenger::class, $result); + $this->assertSame(FlashMessenger::class, $result::class); $this->assertSame('dot_messenger', $result->getNamespace()); $this->assertSame($managerInterface, $result->getSessionManager()); } @@ -84,7 +84,7 @@ public function testCreateFlashMessengerWithSessionManagerClass(): void $result = (new FlashMessengerFactory())($this->container, FlashMessenger::class); - $this->assertInstanceOf(FlashMessenger::class, $result); + $this->assertSame(FlashMessenger::class, $result::class); $this->assertSame('dot_messenger', $result->getNamespace()); $this->assertInstanceOf( $this->config['dot_flashmessenger']['options']['session_manager'], @@ -117,9 +117,9 @@ public function testCreateFlashMessengerWithDefaultSessionManager(): void $result = (new FlashMessengerFactory())($this->container, FlashMessenger::class); - $this->assertInstanceOf(FlashMessenger::class, $result); + $this->assertSame(FlashMessenger::class, $result::class); $this->assertSame('test_namespace', $result->getNamespace()); - $this->assertInstanceOf(ManagerInterface::class, $result->getSessionManager()); + $this->assertContainsOnlyInstancesOf(ManagerInterface::class, [$result->getSessionManager()]); } /** diff --git a/test/Factory/FlashMessengerOptionsFactoryTest.php b/test/Factory/FlashMessengerOptionsFactoryTest.php index 7b39e47..35b247e 100644 --- a/test/Factory/FlashMessengerOptionsFactoryTest.php +++ b/test/Factory/FlashMessengerOptionsFactoryTest.php @@ -30,6 +30,6 @@ public function testCreateFlashMessengerOptionsWithDefaultConfig(): void ->willReturn($this->config); $result = (new FlashMessengerOptionsFactory())($container); - $this->assertInstanceOf(FlashMessengerOptions::class, $result); + $this->assertSame(FlashMessengerOptions::class, $result::class); } } diff --git a/test/Factory/FlashMessengerRendererFactoryTest.php b/test/Factory/FlashMessengerRendererFactoryTest.php index fb9790c..f6af105 100644 --- a/test/Factory/FlashMessengerRendererFactoryTest.php +++ b/test/Factory/FlashMessengerRendererFactoryTest.php @@ -32,6 +32,6 @@ public function testCreateFlashMessengerRenderer(): void ]); $result = (new FlashMessengerRendererFactory())($container); - $this->assertInstanceOf(FlashMessengerRenderer::class, $result); + $this->assertSame(FlashMessengerRenderer::class, $result::class); } } From 75cac42150cb163fa94553b6d5f2337ac5b956c4 Mon Sep 17 00:00:00 2001 From: horea Date: Mon, 10 Mar 2025 18:26:24 +0200 Subject: [PATCH 2/3] Issue#26: Replace Psalm with PHPStan Signed-off-by: horea --- docs/book/index.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 120000 docs/book/index.md diff --git a/docs/book/index.md b/docs/book/index.md deleted file mode 100644 index fe84005..0000000 --- a/docs/book/index.md +++ /dev/null @@ -1 +0,0 @@ -../../README.md \ No newline at end of file diff --git a/docs/book/index.md b/docs/book/index.md new file mode 120000 index 0000000..fe84005 --- /dev/null +++ b/docs/book/index.md @@ -0,0 +1 @@ +../../README.md \ No newline at end of file From c9e18825946ac481e898bca8690a7c20e9885ec9 Mon Sep 17 00:00:00 2001 From: horea Date: Tue, 11 Mar 2025 17:15:53 +0200 Subject: [PATCH 3/3] Issue#26: Replace Psalm with PHPStan Signed-off-by: horea --- README.md | 104 ++++++++++++++++++++--------------- docs/book/v3/installation.md | 4 +- 2 files changed, 63 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 0c8717b..7988078 100644 --- a/README.md +++ b/README.md @@ -27,18 +27,22 @@ Documentation is available at: https://docs.dotkernel.org/dot-flashmessenger/. Run the following command in your project folder - composer require dotkernel/dot-flashmessenger +```shell +composer require dotkernel/dot-flashmessenger +``` This will also install `laminas/laminas-session` as session handling is based on this library. Next, merge the `ConfigProvider` to your application's configuration ## Configuration - return [ - 'dot_flashmessenger' => [ - 'namespace' => 'flash messages session namespace name' - ], - ]; +```php +return [ + 'dot_flashmessenger' => [ + 'namespace' => 'flash messages session namespace name' + ], +]; +``` Sets the session namespace to use for all flash messages and data @@ -49,56 +53,64 @@ Just inject this service in you classes, wherever you need flash messages. ### Getting the service in a factory - $container->get(FlashMessengerInterface::class); +```php +$container->get(FlashMessengerInterface::class); +``` ### Using the flash messenger service To add and retrieve text messages - $this->flashMessenger->addMessage('error', 'This is a error flash message'); - - //on the next request you can get all messages from a namespace, or all messages from all namespaces if namespace is omitted - $this->flashMessenger->getMessages('error'); +```php +$this->flashMessenger->addMessage('error', 'This is a error flash message'); +//on the next request you can get all messages from a namespace, or all messages from all namespaces if namespace is omitted +$this->flashMessenger->getMessages('error'); +``` Adding general data, not just messages, has a different method for that, accepting data as key/value pairs - $this->flashMessenger->addData('myData', $someData); - - // next request - $this->flashMessenger->getData('myData'); +```php +$this->flashMessenger->addData('myData', $someData); +// next request +$this->flashMessenger->getData('myData'); +``` There are also some predefined namespaces, along with shortcuts to add a message in the predefined namespaces - FlashMessengerInterface::ERROR_NAMESPACE - FlashMessengerInterface::WARNING_NAMESPACE - FlashMessengerInterface::INFO_NAMESPACE - FlashMessengerInterface::SUCCESS_NAMESPACE +```php +FlashMessengerInterface::ERROR_NAMESPACE +FlashMessengerInterface::WARNING_NAMESPACE +FlashMessengerInterface::INFO_NAMESPACE +FlashMessengerInterface::SUCCESS_NAMESPACE +``` using the methods: - /** - * @param string $error - * @return void - */ - public function addError($error); - - /** - * @param string $info - * @return void - */ - public function addInfo($info); - - /** - * @param string $warning - * @return void - */ - public function addWarning($warning); - - /** - * @param string $success - * @return void - */ - public function addSuccess($success); +```php +/** + * @param string $error + * @return void + */ +public function addError($error); + +/** + * @param string $info + * @return void + */ +public function addInfo($info); + +/** + * @param string $warning + * @return void + */ +public function addWarning($warning); + +/** + * @param string $success + * @return void + */ +public function addSuccess($success); +``` ## FlashMessengerRenderer @@ -108,10 +120,14 @@ There are also a twig extension provided in [dot-twigrenderer](https://github.co ## Registered services - Dot\FlashMessenger\FlashMessengerInterface::class +```php +Dot\FlashMessenger\FlashMessengerInterface::class +``` The flash messenger service - Dot\FlashMessenger\View\RendererInterface::class +```php +Dot\FlashMessenger\View\RendererInterface::class +``` The registered renderer class diff --git a/docs/book/v3/installation.md b/docs/book/v3/installation.md index 24d99a8..a334c28 100644 --- a/docs/book/v3/installation.md +++ b/docs/book/v3/installation.md @@ -2,6 +2,8 @@ Run the following command in your project folder - composer require dotkernel/dot-flashmessenger +``` shell +composer require dotkernel/dot-flashmessenger +``` This will also install `laminas/laminas-session` as session handling is based on this library.