Skip to content

Commit

Permalink
Replace deprecated usage of CRM_Core_BAO_Setting
Browse files Browse the repository at this point in the history
Additionally add configuration for phpcs, phpstan, and phpunit and a
first test.
  • Loading branch information
Dominic Tubach committed Apr 18, 2024
1 parent 39e2a61 commit 94be8d2
Show file tree
Hide file tree
Showing 30 changed files with 2,069 additions and 11 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/phpcs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: PHP_CodeSniffer

on:
pull_request::
paths:
- '**.php'
- tools/phpcs/composer.json
- phpcs.xml.dist

jobs:
phpcs:
runs-on: ubuntu-latest
name: PHP_CodeSniffer

steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.3
coverage: none
tools: cs2pr
env:
fail-fast: true

- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('tools/phpcs/composer.json') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
run: composer composer-phpcs -- update --no-progress --prefer-dist

- name: Run PHP_CodeSniffer
run: composer phpcs -- -q --report=checkstyle | cs2pr
57 changes: 57 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: PHPStan

on:
push:
pull_request:
- '**.php'
- composer.json
- tools/phpstan/composer.json
- ci/composer.json
- phpstan.ci.neon
- phpstan.neon.dist

jobs:
phpstan:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['7.4', '8.0', '8.3']
prefer: ['prefer-stable', 'prefer-lowest']
name: PHPStan with PHP ${{ matrix.php-versions }} ${{ matrix.prefer }}

steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
coverage: none
env:
fail-fast: true

- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ matrix.prefer }}-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-${{ matrix.prefer }}-

- name: Install dependencies
run: |
git clone --depth=1 https://github.com/systopia/activity-entity.git ../activity-entity &&
git clone --depth=1 https://github.com/systopia/de.systopia.civioffice.git ../de.systopia.civioffice &&
git clone --depth=1 https://github.com/systopia/de.systopia.remotetools.git ../de.systopia.remotetools &&
git clone --depth=1 https://github.com/systopia/external-file.git ../external-file &&
composer update --no-progress --prefer-dist --${{ matrix.prefer }} --optimize-autoloader --ignore-platform-req=ext-gd &&
composer composer-phpunit -- update --no-progress --prefer-dist &&
composer composer-phpstan -- update --no-progress --prefer-dist --optimize-autoloader &&
composer --working-dir=ci update --no-progress --prefer-dist --${{ matrix.prefer }} --optimize-autoloader --ignore-platform-req=ext-gd
composer --working-dir=../de.systopia.remotetools update --no-progress --prefer-dist --${{ matrix.prefer }} --optimize-autoloader
- name: Run PHPStan
run: composer phpstan -- analyse -c phpstan.ci.neon
36 changes: 36 additions & 0 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: PHPUnit

on:
pull_request:
paths:
- '**.php'
- composer.json
- tools/phpunit/composer.json
- phpunit.xml.dist

env:
# On github CI machine creating the "/vendor" volume fails otherwise with: read-only file system: unknown
BIND_VOLUME_PERMISSIONS: rw

jobs:
phpunit:
runs-on: ubuntu-latest
strategy:
matrix:
civicrm-image-tags: [ '5-drupal', '5.38-drupal-php8.0' ]
name: PHPUnit with Docker image michaelmcandrew/civicrm:${{ matrix.civicrm-image-tags }}
env:
CIVICRM_IMAGE_TAG: ${{ matrix.civicrm-image-tags }}

steps:
- uses: actions/checkout@v3
- name: Pull images
run: docker compose -f tests/docker-compose.yml pull --quiet
- name: Start containers
run: docker compose -f tests/docker-compose.yml up -d
- name: Prepare environment
run: docker compose -f tests/docker-compose.yml exec civicrm sites/default/files/civicrm/ext/de.systopia.moregreetings/tests/docker-prepare.sh
- name: Run PHPUnit
run: docker compose -f tests/docker-compose.yml exec civicrm sites/default/files/civicrm/ext/de.systopia.moregreetings/tests/docker-phpunit.sh
- name: Remove containers
run: docker compose -f tests/docker-compose.yml down -v
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.phpcs.cache
/.phpunit.result.cache
/.phpstan/
/phpstan.neon
/tools/*/vendor/
/tools/*/composer.lock
2 changes: 1 addition & 1 deletion CRM/Moregreetings/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public static function restartCalculateAllGreetingsJob() {
$job = self::getAllGreetingsJob();

// start from zero:
CRM_Core_BAO_Setting::setItem('0', 'moregreetings', 'moregreetings_job_status');
Civi::settings()->set('moregreetings_job_status', '0');

// enable cronjob
if (!$job['is_active']) {
Expand Down
6 changes: 3 additions & 3 deletions CRM/Moregreetings/Form/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function buildQuickForm() {
* set the default (=current) values in the form
*/
public function setDefaultValues() {
$values = CRM_Core_BAO_Setting::getItem('moregreetings', 'moregreetings_templates');
$values = Civi::settings()->get('moregreetings_templates');
if (!is_array($values)) {
$values = array();
}
Expand All @@ -109,7 +109,7 @@ public function postProcess() {
$values = $this->exportValues();

// first: update the greetings
$old_greetings = CRM_Core_BAO_Setting::getItem('moregreetings', 'moregreetings_templates');
$old_greetings = Civi::settings()->get('moregreetings_templates');
$greetings_changed = FALSE;
for ($i = 1; $i <= self::getNumberOfGreetings(); ++$i) {
if (isset($values["greeting_smarty_{$i}"])) {
Expand All @@ -123,7 +123,7 @@ public function postProcess() {
$greetings_changed = TRUE;
}
}
CRM_Core_BAO_Setting::setItem($values_array, 'moregreetings', 'moregreetings_templates');
Civi::settings()->set('moregreetings_templates', $values_array);

// then: adjust the greeting count
if ($values['greeting_count'] != self::getNumberOfGreetings()) {
Expand Down
2 changes: 1 addition & 1 deletion CRM/Moregreetings/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function run($context) {
}

// determine the fields to load
$templates = CRM_Core_BAO_Setting::getItem('moregreetings', 'moregreetings_templates');
$templates = Civi::settings()->get('moregreetings_templates');
$used_fields = CRM_Moregreetings_Renderer::getUsedContactFields($templates);

// load contacts
Expand Down
4 changes: 2 additions & 2 deletions CRM/Moregreetings/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static function updateMoreGreetings($contact_id, $contact = NULL): void {
}

// load the templates
$templates = CRM_Core_BAO_Setting::getItem('moregreetings', 'moregreetings_templates');
$templates = Civi::settings()->get('moregreetings_templates');
if (!is_array($templates)) {
return;
}
Expand Down Expand Up @@ -95,7 +95,7 @@ public static function updateMoreGreetings($contact_id, $contact = NULL): void {
* @return int last contact ID processed, 0 if none
*/
public static function updateMoreGreetingsForContacts($from_id, $max_count): int {
$templates = CRM_Core_BAO_Setting::getItem('moregreetings', 'moregreetings_templates');
$templates = Civi::settings()->get('moregreetings_templates');

// remark: if you change these parameters, see if you also want to adjust
// CRM_Moregreetings_Job::run and CRM_Moregreetings_Renderer::updateMoreGreetings
Expand Down
2 changes: 1 addition & 1 deletion CRM/Xdedupe/Resolver/MoreGreetingsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CRM_Xdedupe_Resolver_MoreGreetingsSubscriber implements EventSubscriberInt
/**
* Subscribe to the list events, so we can plug the built-in ones
*/
public static function getSubscribedEvents() {
public static function getSubscribedEvents(): array {
return [
'civi.xdedupe.resolvers' => ['addBuiltinResolvers', Events::W_MIDDLE],
];
Expand Down
6 changes: 3 additions & 3 deletions api/v3/Job/UpdateMoregreetings.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
* Cron Job to update the more-greetings for all contacts
*/
function civicrm_api3_job_update_moregreetings($params) {
$last_id = CRM_Core_BAO_Setting::getItem('moregreetings', 'moregreetings_job_status');
$last_id = Civi::settings()->get('moregreetings_job_status');
if ($last_id == 'busy') {
// there's another job running
return civicrm_api3_create_success(ts("Job already running", array('domain' => 'de.systopia.moregreetings')));
}

// ok, let's go
CRM_Core_BAO_Setting::setItem('busy', 'moregreetings', 'moregreetings_job_status');
Civi::settings()->set('moregreetings_job_status', 'busy');
$start_time = microtime(TRUE);

// run the renderer on blocks of contacts until the time runs out
Expand All @@ -41,7 +41,7 @@ function civicrm_api3_job_update_moregreetings($params) {
}

// store last processed ID
CRM_Core_BAO_Setting::setItem((string)$last_id, 'moregreetings', 'moregreetings_job_status');
Civi::settings()->set('moregreetings_job_status', (string)$last_id);

if ($last_id == 0) {
// we're done!
Expand Down
2 changes: 2 additions & 0 deletions ci/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The dependencies specified in composer.json of this directory are required to
run phpstan in CI.
14 changes: 14 additions & 0 deletions ci/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"allow-plugins": {
"civicrm/composer-compile-plugin": false,
"civicrm/composer-downloads-plugin": true,
"cweagans/composer-patches": true
}
},
"require": {
"civicrm/civicrm-core": "^5.38"
}
}
44 changes: 44 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "systopia/de.systopia.moregreetings",
"type": "civicrm-ext",
"license": "AGPL-3.0-or-later",
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"sort-packages": true
},
"require": {},
"scripts": {
"composer-phpcs": [
"@composer --working-dir=tools/phpcs"
],
"composer-phpstan": [
"@composer --working-dir=tools/phpstan"
],
"composer-phpunit": [
"@composer --working-dir=tools/phpunit"
],
"composer-tools": [
"@composer-phpcs",
"@composer-phpstan",
"@composer-phpunit"
],
"phpcs": [
"@php tools/phpcs/vendor/bin/phpcs"
],
"phpcbf": [
"@php tools/phpcs/vendor/bin/phpcbf"
],
"phpstan": [
"@php tools/phpstan/vendor/bin/phpstan"
],
"phpunit": [
"@php tools/phpunit/vendor/bin/simple-phpunit --coverage-text"
],
"test": [
"@phpcs",
"@phpstan",
"@phpunit"
]
}
}
80 changes: 80 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="CiviCRM - PHPStan"
xsi:noNamespaceSchemaLocation="tools/phpcs/vendor/squizlabs/php_codesniffer/phpcs.xsd">
<description>CiviCRM coding standard with support for PHPStan type hints and some additional rules</description>

<!-- TODO: Commented paths should be commented in. -->
<!--<file>api</file>-->
<!--<file>CRM</file>-->
<file>tests</file>
<!--<file>moregreetings.php</file>-->

<arg name="extensions" value="php"/>
<arg name="cache" value=".phpcs.cache"/>
<arg name="colors"/>
<arg value="sp"/>

<!-- Exit with code 0 if warnings, but no error occurred -->
<config name="ignore_warnings_on_exit" value="true"/>

<rule ref="tools/phpcs/vendor/drupal/coder/coder_sniffer/Drupal">
<!-- Conflicts with PHPStan type hints -->
<exclude name="Drupal.Commenting.VariableComment.IncorrectVarType"/>
<exclude name="Drupal.Commenting.FunctionComment.ParamTypeSpaces"/>
<exclude name="Drupal.Commenting.VariableComment.MissingVar"/>

<!-- Don't enforce phpdoc type hint because it (might) only duplicate a PHP type hint -->
<exclude name="Drupal.Commenting.FunctionComment.ParamMissingDefinition"/>

<!-- False positive when license header is set and variable has no comment -->
<exclude name="Drupal.Commenting.VariableComment.WrongStyle"/>

<exclude name="Drupal.Commenting.VariableComment.MissingVar"/>
</rule>

<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
<rule ref="Generic.CodeAnalysis.EmptyStatement">
<exclude name="Generic.CodeAnalysis.EmptyStatement.DetectedCatch"/>
</rule>
<rule ref="Generic.CodeAnalysis.ForLoopWithTestFunctionCall"/>
<rule ref="Generic.Files.OneClassPerFile"/>
<rule ref="Generic.Files.OneInterfacePerFile"/>
<rule ref="Generic.Files.OneObjectStructurePerFile"/>
<rule ref="Generic.Files.OneTraitPerFile"/>
<rule ref="Generic.Formatting.DisallowMultipleStatements"/>
<rule ref="Generic.Metrics.CyclomaticComplexity"/>
<rule ref="Generic.Metrics.NestingLevel"/>
<rule ref="Generic.NamingConventions.AbstractClassNamePrefix"/>
<rule ref="Generic.NamingConventions.InterfaceNameSuffix"/>
<rule ref="Generic.NamingConventions.TraitNameSuffix"/>
<rule ref="Generic.PHP.RequireStrictTypes"/>
<rule ref="PSR1.Files.SideEffects"/>
<rule ref="PSR12.Classes.ClassInstantiation"/>
<rule ref="PSR12.Properties.ConstantVisibility"/>
<rule ref="Squiz.PHP.CommentedOutCode"/>
<rule ref="Squiz.PHP.GlobalKeyword"/>
<rule ref="Squiz.Strings.DoubleQuoteUsage">
<exclude name="Squiz.Strings.DoubleQuoteUsage.ContainsVar"/>
</rule>

<!-- Lines can be 120 chars long, but never show errors -->
<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="120"/>
<property name="absoluteLineLimit" value="0"/>
</properties>
</rule>

<!-- Ban some functions -->
<rule ref="Generic.PHP.ForbiddenFunctions">
<properties>
<property name="forbiddenFunctions" type="array">
<element key="sizeof" value="count"/>
<element key="delete" value="unset"/>
<element key="print" value="echo"/>
<element key="is_null" value="null"/>
<element key="create_function" value="null"/>
</property>
</properties>
</rule>
</ruleset>
Loading

0 comments on commit 94be8d2

Please sign in to comment.