Skip to content

Commit 8e10f11

Browse files
authored
Merge pull request #37 from Staffbase/code-cleanup
refactor: Code cleanup Pt1
2 parents e01b4d0 + bc8629c commit 8e10f11

35 files changed

Lines changed: 5301 additions & 254 deletions

.github/copilot-instructions.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# GitHub Copilot instructions for plugins-sdk-php
2+
3+
This guidance file helps Copilot generate code aligned with project standards and domain design for the Staffbase Plugin SDK for PHP.
4+
5+
## General Copilot goals
6+
- Follow project coding style and PSR-4 namespace conventions.
7+
- Prefer using and extending the existing src/SSOToken, PluginSession, and RemoteCall infrastructure.
8+
- When creating new code, integrate with provided interfaces, traits, and classes (see src/SSOData, src/RemoteCall, src/Exceptions).
9+
- All code should work with PHP 8.3, strict_types, and Composer autoloading.
10+
- When suggesting test code, match the structure of test/ files and use PHPUnit 10+ only.
11+
12+
## Style and static analysis
13+
- Conform to rules in .php-cs-fixer.dist.php (array_syntax short, strict_types, remove unused imports, use strict parameters).
14+
- Code should pass `composer run cs-fixer:check` and `composer run lint` on commit.
15+
- Code should pass PHPStan at level 4 (phpstan.neon.dist), including for new types, traits, and test cases.
16+
17+
## Domain guidance
18+
- For SSO authentication, use and extend SSOToken, PluginSession, and SSODataTrait as the foundation — avoid duplicating token logic.
19+
- Remote call support should use AbstractRemoteCallHandler or interfaces from src/RemoteCall if you need plugin event handling (e.g., deletion).
20+
- When handling sessions, use PluginSession and its methods for SSO data. Avoid manual $_SESSION logic except for advanced cases.
21+
- Exceptions should inherit src/Exceptions base classes as relevant.
22+
23+
## Recommended code generation practices
24+
- Prefer composition and interface-driven design (see src/SSOData, src/RemoteCall, src/Exceptions).
25+
- Follow README.md example patterns for token creation, session management, and error handling.
26+
- Place new classes in src/ with Staffbase\plugins\sdk\ namespace; place tests in test/ with Staffbase\plugins\test\ namespace.
27+
28+
## Copilot DON'Ts
29+
- Do not add new dependencies unless absolutely required and justified in code comments.
30+
- Do not use deprecated PHP practices or legacy global state.
31+
- Do not bypass static analysis rules for quick fixes.
32+
- Do not create code outside of src/ and test/ unless explicitly requested. Never create example or playground code that is not integrated into the SDK or its tests.
33+
34+
## Documentation
35+
- Consult CLAUDE.md at project root for further architectural guidance and standard commands.
36+
- Reference README.md and inline docblocks for API documentation style.

.github/workflows/php.yml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ name: PHP Composer
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches:
6+
- 'main'
7+
- '**/**'
8+
69
pull_request:
7-
branches: [ main ]
10+
branches:
11+
- 'main'
12+
- '**/**'
813

914
jobs:
1015
run:
@@ -29,7 +34,10 @@ jobs:
2934

3035
- name: Install dependencies
3136
if: steps.composer-cache.outputs.cache-hit != 'true'
32-
run: composer update --prefer-dist --no-progress
37+
run: composer install --prefer-dist --no-progress
38+
39+
- name: Linting
40+
run: composer lint
3341

3442
- name: Run test suite
3543
run: composer test

.gitignore

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,43 @@
1-
.DS_Store
2-
.rnd
3-
composer.lock
4-
vendor
5-
.idea
1+
### PHP-CS-Fixer template
2+
# Covers PHP CS Fixer
3+
# Reference: https://cs.symfony.com/
4+
5+
# Generated files
6+
.php-cs-fixer.cache
7+
8+
# Local config See: https://cs.symfony.com/doc/config.html
9+
.php-cs-fixer.php
10+
11+
### Composer template
12+
composer.phar
13+
/vendor/
14+
15+
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
16+
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
17+
# composer.lock
18+
19+
### PHPUnit template
20+
# Covers PHPUnit
21+
# Reference: https://phpunit.de/
22+
23+
# Generated files
24+
.phpunit.result.cache
25+
.phpunit.cache
26+
clover.xml
27+
28+
# PHPUnit
29+
/app/phpunit.xml
30+
/phpunit.xml
31+
32+
33+
# Build data
34+
/build/
35+
36+
### PHPCodeSniffer template
37+
# CodeSniffer
38+
phpcs.xml
39+
40+
/vendor/*
41+
/wpcs/*
42+
43+
!/.gitignore

.php-cs-fixer.dist.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->exclude(['vendor', 'build'])
5+
->in(__DIR__);
6+
7+
return (new PhpCsFixer\Config())
8+
->setRules([
9+
'@PHP8x4Migration' => true,
10+
'@PER-CS' => true,
11+
'array_syntax' => ['syntax' => 'short'],
12+
'declare_strict_types' => true,
13+
'strict_param' => true,
14+
'no_unused_imports' => true,
15+
])
16+
->setRiskyAllowed(true)
17+
->setFinder($finder)
18+
->setParallelConfig(PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect());

.phpactor.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"$schema": "/phpactor.schema.json",
3+
"language_server_phpstan.enabled": true,
4+
"language_server_php_cs_fixer.enabled": true,
5+
"indexer.exclude_patterns": [
6+
"/vendor/**/Tests/**/*",
7+
"/vendor/**/tests/**/*",
8+
"/var/cache/**/*",
9+
"/vendor/composer/**/*"
10+
],
11+
"language_server.diagnostics_on_update": false,
12+
"language_server_highlight.enabled": false
13+
}

CLAUDE.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
6+
## Quick commands
7+
8+
- Install dependencies: composer install
9+
- Run unit tests: composer test (runs phpunit)
10+
- Run tests with coverage: composer run test:coverage
11+
- Run static analysis: composer run phpstan (phpstan analyse --memory-limit=1G)
12+
- Run code style checks: composer run cs-fixer:check
13+
- Auto-fix code style: composer run cs-fixer:fix
14+
- Full check: composer run check (runs cs-fixer check, phpstan, and test coverage)
15+
- Lint (style + static analysis): composer run lint
16+
17+
To run a single PHPUnit test (by file):
18+
- ./vendor/bin/phpunit path/to/TestFile.php
19+
20+
To run a single test method:
21+
- ./vendor/bin/phpunit --filter testMethodName path/to/TestFile.php
22+
23+
24+
## Project overview — high level
25+
26+
This repository provides a small PHP SDK for Staffbase plugin Single Sign-On (SSO) token parsing and validation.
27+
28+
High-level structure:
29+
30+
- src/: Core library code. Primary classes:
31+
- src/SSOToken.php — main JWT parsing & validation wrapper for plugin SSO tokens (uses lcobucci/jwt)
32+
- src/SSOTokenGenerator.php — utility to generate test tokens for unit tests
33+
- src/PluginSession.php — session wrapper for persisting SSO data between requests
34+
- src/RemoteCall/* — handlers and interfaces for app-initiated remote calls (delete-instance, etc.)
35+
- src/SSOData/* — data interfaces/traits for shared and SSO specific claims
36+
- src/Exceptions/* — domain exceptions (SSOAuthenticationException, SSOException, ...)
37+
- src/Validation/* — custom validation constraints used by php-jwt/token validation
38+
- src/AbstractToken.php — base token parsing/verification logic used by SSOToken and others
39+
40+
- test/: PHPUnit test suite for the library. Tests instantiate SSOToken, SSOTokenGenerator and validate behavior.
41+
42+
- composer.json: Composer metadata and scripts (lint, phpstan, tests, cs-fixer). Key runtime deps: lcobucci/jwt, lcobucci/clock.
43+
44+
- phpstan.neon.dist: phpstan configuration (analyse src and test at level 4 by default).
45+
46+
- README.md: user-facing documentation and examples (installation, usage examples, remote calls).
47+
48+
49+
## CI and hooks
50+
51+
- GitHub Actions: a workflow badge exists in README, check .github/workflows for exact CI steps if needed.
52+
- Composer hooks: composer.json registers post-install and post-update hooks for composer-git-hooks. Pre-commit hooks in composer.extra.hooks run "composer fix" and "composer phpstan".
53+
54+
55+
## Notes for future Claude Code instances
56+
57+
- Use Composer scripts defined in composer.json for all common operations (tests, lint, phpstan, cs-fixer). Prefer the scripts so local project configuration is respected.
58+
- When adding or editing PHP code, run CS Fixer and PHPStan locally (composer run fix; composer run phpstan) before running tests.
59+
- Tests are run with phpunit (vendor/bin/phpunit). For debugging a single test, prefer running vendor/bin/phpunit --filter.
60+
61+
Key files to inspect for behavior changes:
62+
- src/SSOToken.php: constructor, token parsing and validation flow
63+
- src/AbstractToken.php: core token parsing/verification logic
64+
- src/Validation/HasInstanceId.php: custom constraint used by SSOToken
65+
- README.md and doc/api.md: user-visible behavior and API
66+
67+
68+
## When editing code
69+
70+
- Prefer editing existing files rather than creating new ones unless a new module is required.
71+
- Follow PSR-4 autoloading in composer.json (namespace Staffbase\plugins\sdk\ -> src/)
72+
- Keep phpstan.neon.dist level consideration in mind
73+
74+
75+
## Contact and references
76+
77+
- Project homepage: https://github.com/Staffbase/plugins-sdk-php
78+
- Composer entry: composer.json
79+

codecov.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
codecov:
2+
require_ci_to_pass: yes
3+
4+
coverage:
5+
precision: 2
6+
round: down
7+
range: "70...100"
8+
status:
9+
project:
10+
default:
11+
target: auto
12+
threshold: 0%
13+
patch:
14+
default:
15+
target: auto
16+
threshold: 0%
17+
18+
parsers:
19+
gcov:
20+
branch_detection:
21+
conditional: yes
22+
loop: yes
23+
method: no
24+
macro: no
25+
26+
comment:
27+
layout: "reach,diff,flags,tree"
28+
behavior: default
29+
require_changes: false

composer.json

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,50 @@
1515
"require": {
1616
"php": "^8.3 || ^8.4",
1717
"lcobucci/jwt": "^5.5",
18-
"lcobucci/clock": "^3.3"
19-
18+
"lcobucci/clock": "^3.3"
2019
},
2120
"require-dev": {
21+
"brainmaestro/composer-git-hooks": "^3.0",
22+
"friendsofphp/php-cs-fixer": "^3.89",
2223
"phpseclib/phpseclib": "^2.0",
23-
"phpunit/phpunit": "^9.0"
24+
"phpstan/extension-installer": "^1.4",
25+
"phpstan/phpstan-phpunit": "^2.0",
26+
"phpunit/phpunit": "^10.0"
2427
},
2528
"autoload": {
2629
"psr-4": {
27-
"Staffbase\\plugins\\sdk\\": "src",
28-
"Staffbase\\plugins\\test\\": "test"
30+
"Staffbase\\plugins\\sdk\\": "src/"
31+
}
32+
},
33+
"autoload-dev": {
34+
"psr-4": {
35+
"Staffbase\\plugins\\test\\": "test/"
36+
}
37+
},
38+
"config": {
39+
"sort-packages": true,
40+
"allow-plugins": {
41+
"phpstan/extension-installer": true
2942
}
3043
},
3144
"scripts": {
32-
"test": "phpunit --colors='always' --debug $PHPUNIT_ARGS",
33-
"lint": "phpcs --standard=PSR2 --extensions=php --ignore=*/vendor/* src test",
34-
"fix": "phpcbf --standard=PSR2 --extensions=php --ignore=*/vendor/* src test"
45+
"check": ["@cs-fixer:check", "@phpstan", "@test:coverage"],
46+
"lint": ["@cs-fixer:check", "@phpstan"],
47+
"fix": ["@cs-fixer:fix"],
48+
"cs-fixer:check": "php-cs-fixer fix --dry-run --diff -v",
49+
"cs-fixer:fix": "php-cs-fixer fix --diff -v",
50+
"phpstan": "phpstan analyse --memory-limit=1G",
51+
"test": "phpunit",
52+
"test:coverage": "phpunit --coverage-text --coverage-clover=clover.xml",
53+
"post-install-cmd": "cghooks add --ignore-lock",
54+
"post-update-cmd": "cghooks update"
55+
},
56+
"extra": {
57+
"hooks": {
58+
"pre-commit": [
59+
"composer fix",
60+
"composer phpstan"
61+
]
62+
}
3563
}
3664
}

0 commit comments

Comments
 (0)