From 07f089e033fa9fc89f4fd4812cd8e5eda5de1f9c Mon Sep 17 00:00:00 2001 From: Carlos MAtos Date: Mon, 23 Dec 2024 02:21:27 +0000 Subject: [PATCH] PHPCS check and workflows. --- .github/workflows/composer.yml | 44 ++++++++++++++++++++++++++++++++++ .github/workflows/lint.yml | 43 +++++++++++++++++++++++++++++++++ .github/workflows/phpunit.yml | 28 ++++++++++++++++++++++ .phpcs-cache | 2 +- src/ServiceFactory.php | 10 +++++--- 5 files changed, 123 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/composer.yml create mode 100644 .github/workflows/lint.yml create mode 100644 .github/workflows/phpunit.yml diff --git a/.github/workflows/composer.yml b/.github/workflows/composer.yml new file mode 100644 index 0000000..f14149e --- /dev/null +++ b/.github/workflows/composer.yml @@ -0,0 +1,44 @@ +name: PHP Composer + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +permissions: + contents: read + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Set up PHP 8.2 + uses: shivammathur/setup-php@v2 + with: + php-version: 8.2 + + - name: Validate composer.json and composer.lock + run: composer validate + + - name: Cache Composer packages + id: composer-cache + uses: actions/cache@v3 + with: + path: vendor + key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-php- + + - name: Install dependencies + run: composer install --prefer-dist --no-progress + + # Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit" + # Docs: https://getcomposer.org/doc/articles/scripts.md + + # - name: Run test suite + # run: composer run-script test diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..c894b77 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,43 @@ +name: PHP Lint + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + lint: + runs-on: ubuntu-latest + + strategy: + matrix: + php-version: ['8.2'] + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + + - name: Install PHP_CodeSniffer + run: composer global require "squizlabs/php_codesniffer=*" + + - name: Run PHP_CodeSniffer + run: ~/.composer/vendor/bin/phpcs + + - name: Upload PHP_CodeSniffer results + if: always() + uses: actions/upload-artifact@v4 + with: + name: phpcs-results + path: phpcs.xml + + - name: Display PHP_CodeSniffer results + if: always() + run: cat phpcs.xml diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml new file mode 100644 index 0000000..405ffbc --- /dev/null +++ b/.github/workflows/phpunit.yml @@ -0,0 +1,28 @@ +name: PHPUnit Tests + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.2' # PHP version updated to 8.2 + extensions: mbstring, intl + coverage: xdebug # If you need code coverage + + - name: Install dependencies + run: composer install --prefer-dist --no-progress --no-suggest + + - name: Run PHPUnit + run: vendor/bin/phpunit diff --git a/.phpcs-cache b/.phpcs-cache index 94ae198..eb5852b 100644 --- a/.phpcs-cache +++ b/.phpcs-cache @@ -1 +1 @@ -{"config":{"phpVersion":80226,"phpExtensions":"6444a8d24aa127a4fe13989038fc4447","tabWidth":4,"encoding":"utf-8","recordErrors":true,"annotations":true,"configData":[],"codeHash":"b5dc58ae9d1a395f86384fd9a1678bb4","rulesetHash":"670f8348153700c3a70fc6127ca0e484"},"\/home\/carlos\/Projects\/factory\/src\/Exceptions\/ClassNotFoundException.php":{"hash":"ef42127b99db182750f3cb885f5a00a233204","errors":[],"warnings":[],"metrics":{"Declarations and side effects mixed":{"values":{"no":1}},"PHP short open tag used":{"values":{"no":1}},"EOL char":{"values":{"\\n":1}},"Number of newlines at EOF":{"values":{"1":1}},"PHP closing tag at end of PHP-only file":{"values":{"no":1}},"Line length":{"values":{"80 or less":5}},"PHP keyword case":{"values":{"lower":3}},"Multiple statements on same line":{"values":{"no":1}},"One class per file":{"values":{"yes":1}},"Class defined in namespace":{"values":{"yes":1}},"PascalCase class name":{"values":{"yes":1}},"Class opening brace placement":{"values":{"new line":1}}},"errorCount":0,"warningCount":0,"fixableCount":0,"numTokens":23},"\/home\/carlos\/Projects\/factory\/src\/Exceptions\/ServiceNotFoundException.php":{"hash":"fd4d95418d462fa9339b8f96cf301a7533204","errors":[],"warnings":[],"metrics":{"Declarations and side effects mixed":{"values":{"no":1}},"PHP short open tag used":{"values":{"no":1}},"EOL char":{"values":{"\\n":1}},"Number of newlines at EOF":{"values":{"1":1}},"PHP closing tag at end of PHP-only file":{"values":{"no":1}},"Line length":{"values":{"80 or less":5}},"PHP keyword case":{"values":{"lower":3}},"Multiple statements on same line":{"values":{"no":1}},"One class per file":{"values":{"yes":1}},"Class defined in namespace":{"values":{"yes":1}},"PascalCase class name":{"values":{"yes":1}},"Class opening brace placement":{"values":{"new line":1}}},"errorCount":0,"warningCount":0,"fixableCount":0,"numTokens":23},"\/home\/carlos\/Projects\/factory\/src\/ServiceFactory.php":{"hash":"56e242256f0874adfa49963d1503e82333204","errors":[],"warnings":[],"metrics":{"Declarations and side effects mixed":{"values":{"no":1}},"PHP short open tag used":{"values":{"no":1}},"EOL char":{"values":{"\\n":1}},"Number of newlines at EOF":{"values":{"1":1}},"PHP closing tag at end of PHP-only file":{"values":{"no":1}},"Line length":{"values":{"80 or less":77,"81-120":11}},"Line indent":{"values":{"spaces":81}},"PHP keyword case":{"values":{"lower":51}},"Multiple statements on same line":{"values":{"no":25}},"One class per file":{"values":{"yes":1}},"Class defined in namespace":{"values":{"yes":1}},"PascalCase class name":{"values":{"yes":1}},"Class opening brace placement":{"values":{"new line":1}},"PHP type case":{"values":{"lower":19}},"CamelCase method name":{"values":{"yes":5}},"Function opening brace placement":{"values":{"new line":5}},"Spaces after control structure open parenthesis":{"values":[8]},"Spaces before control structure close parenthesis":{"values":[8]},"Blank lines at start of control structure":{"values":[8]},"Blank lines at end of control structure":{"values":[8]},"Control structure defined inline":{"values":{"no":7}},"PHP constant case":{"values":{"lower":5}}},"errorCount":0,"warningCount":0,"fixableCount":0,"numTokens":757}} \ No newline at end of file +{"config":{"phpVersion":80226,"phpExtensions":"6444a8d24aa127a4fe13989038fc4447","tabWidth":4,"encoding":"utf-8","recordErrors":true,"annotations":true,"configData":[],"codeHash":"b5dc58ae9d1a395f86384fd9a1678bb4","rulesetHash":"670f8348153700c3a70fc6127ca0e484"},"\/home\/carlos\/Projects\/factory\/src\/Exceptions\/ClassNotFoundException.php":{"hash":"ef42127b99db182750f3cb885f5a00a233204","errors":[],"warnings":[],"metrics":{"Declarations and side effects mixed":{"values":{"no":1}},"PHP short open tag used":{"values":{"no":1}},"EOL char":{"values":{"\\n":1}},"Number of newlines at EOF":{"values":{"1":1}},"PHP closing tag at end of PHP-only file":{"values":{"no":1}},"Line length":{"values":{"80 or less":5}},"PHP keyword case":{"values":{"lower":3}},"Multiple statements on same line":{"values":{"no":1}},"One class per file":{"values":{"yes":1}},"Class defined in namespace":{"values":{"yes":1}},"PascalCase class name":{"values":{"yes":1}},"Class opening brace placement":{"values":{"new line":1}}},"errorCount":0,"warningCount":0,"fixableCount":0,"numTokens":23},"\/home\/carlos\/Projects\/factory\/src\/Exceptions\/ServiceNotFoundException.php":{"hash":"fd4d95418d462fa9339b8f96cf301a7533204","errors":[],"warnings":[],"metrics":{"Declarations and side effects mixed":{"values":{"no":1}},"PHP short open tag used":{"values":{"no":1}},"EOL char":{"values":{"\\n":1}},"Number of newlines at EOF":{"values":{"1":1}},"PHP closing tag at end of PHP-only file":{"values":{"no":1}},"Line length":{"values":{"80 or less":5}},"PHP keyword case":{"values":{"lower":3}},"Multiple statements on same line":{"values":{"no":1}},"One class per file":{"values":{"yes":1}},"Class defined in namespace":{"values":{"yes":1}},"PascalCase class name":{"values":{"yes":1}},"Class opening brace placement":{"values":{"new line":1}}},"errorCount":0,"warningCount":0,"fixableCount":0,"numTokens":23},"\/home\/carlos\/Projects\/factory\/src\/ServiceFactory.php":{"hash":"8af11ef2a53563eb49f1e8f2415d9a7b33204","errors":{"125":{"13":[{"message":"No blank line found after control structure","source":"Squiz.WhiteSpace.ControlStructureSpacing.NoLineAfterClose","listener":"PHP_CodeSniffer\\Standards\\Squiz\\Sniffs\\WhiteSpace\\ControlStructureSpacingSniff","severity":0,"fixable":true}]}},"warnings":[],"metrics":{"Declarations and side effects mixed":{"values":{"no":1}},"PHP short open tag used":{"values":{"no":1}},"EOL char":{"values":{"\\n":1}},"Number of newlines at EOF":{"values":{"1":1}},"PHP closing tag at end of PHP-only file":{"values":{"no":1}},"Line length":{"values":{"80 or less":100,"81-120":11}},"Line indent":{"values":{"spaces":104}},"PHP keyword case":{"values":{"lower":64}},"Multiple statements on same line":{"values":{"no":26}},"One class per file":{"values":{"yes":1}},"Class defined in namespace":{"values":{"yes":1}},"PascalCase class name":{"values":{"yes":1}},"Class opening brace placement":{"values":{"new line":1}},"PHP type case":{"values":{"lower":19}},"CamelCase method name":{"values":{"yes":6}},"Function opening brace placement":{"values":{"new line":6}},"Spaces after control structure open parenthesis":{"values":[12]},"Spaces before control structure close parenthesis":{"values":[12]},"Blank lines at start of control structure":{"values":[13]},"Blank lines at end of control structure":{"values":[13]},"Control structure defined inline":{"values":{"no":10}},"PHP constant case":{"values":{"lower":8}}},"errorCount":1,"warningCount":0,"fixableCount":1,"numTokens":870}} \ No newline at end of file diff --git a/src/ServiceFactory.php b/src/ServiceFactory.php index e453ef5..a9754e7 100644 --- a/src/ServiceFactory.php +++ b/src/ServiceFactory.php @@ -58,7 +58,7 @@ public function associateCondition(string $serviceName, string $conditionName): /** * Evaluates all conditions for a service - * + * * @throws \InvalidArgumentException When a condition is not met */ private function evaluateConditions(string $serviceName): \Generator @@ -74,8 +74,12 @@ private function evaluateConditions(string $serviceName): \Generator yield match ($result) { true => true, - false => throw new \InvalidArgumentException("Condition '$conditionName' not met for service: $serviceName"), - default => throw new \InvalidArgumentException("Invalid result for condition '$conditionName' on service: $serviceName") + false => throw new \InvalidArgumentException( + "Condition '$conditionName' not met for service: $serviceName" + ), + default => throw new \InvalidArgumentException( + "Invalid result for condition '$conditionName' on service: $serviceName" + ) }; } }