Skip to content

Commit

Permalink
Change namespace (#467)
Browse files Browse the repository at this point in the history
* feat: add namespace deprecator

* docs: fix typo in ci

* refactor: change namespace BracketSpace\Notification\Defaults to BracketSpace\Notification\Repository

* fix: change adapter namespace

* fix: compat namespace

* fix: phpstan

* fix: phpstan

* fix: autoloader exclusion

* fix: autoloader exclusion

* fix: phpstan
  • Loading branch information
jakubmikita authored Jun 2, 2024
1 parent 438628e commit 9964f0b
Show file tree
Hide file tree
Showing 161 changed files with 632 additions and 584 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
run: git flow init -d
- name: Start release
run: git flow release start ${{ github.event.inputs.new_version }}
- name: Replace 8.0.0 tags with new version number
- name: Replace Next tags with new version number
uses: jacobtomlinson/gha-find-replace@master
with:
find: "(?i)\\[Next\\]"
Expand Down
34 changes: 0 additions & 34 deletions compat/src-deprecated/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,6 @@
use BracketSpace\Notification\Store;
use BracketSpace\Notification\Dependencies\Micropackage\DocHooks\Helper as DocHooksHelper;

/**
* Helper function.
* Throws a deprecation notice from deprecated class
*
* @since 6.0.0
* @param string $class Deprecated class name.
* @param string $version Version since deprecated.
* @param string $replacement Replacement class.
* @return void
*/
function notification_deprecated_class( $class, $version, $replacement = null ) {

// phpcs:disable
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
if ( function_exists( '__' ) ) {
if ( ! is_null( $replacement ) ) {
/* translators: 1: Class name, 2: version number, 3: alternative function name */
trigger_error( sprintf( __('Class %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.'), $class, $version, $replacement ) );
} else {
/* translators: 1: Class name, 2: version number */
trigger_error( sprintf( __('Class %1$s is <strong>deprecated</strong> since version %2$s with no alternative available.'), $class, $version ) );
}
} else {
if ( ! is_null( $replacement ) ) {
trigger_error( sprintf( 'Class %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', $class, $version, $replacement ) );
} else {
trigger_error( sprintf( 'Class %1$s is <strong>deprecated</strong> since version %2$s with no alternative available.', $class, $version ) );
}
}
}
// phpcs:enable

}

/**
* Gets cached value or cache object
*
Expand Down
38 changes: 38 additions & 0 deletions compat/src-deprecated/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Deprecation helpers
*
* @package notification
*/

/**
* Helper function.
* Throws a deprecation notice from deprecated class
*
* @since 6.0.0
* @param string $class Deprecated class name.
* @param string $version Version since deprecated.
* @param string $replacement Replacement class.
* @return void
*/
function notification_deprecated_class($class, $version, $replacement = null) {
if (! defined('WP_DEBUG') || ! WP_DEBUG) {
return;
}

if (function_exists('__')) {
if (! is_null($replacement)) {
/* translators: 1: Class name, 2: version number, 3: alternative function name */
trigger_error(sprintf(__('Class %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.'), $class, $version, $replacement), E_USER_DEPRECATED);
} else {
/* translators: 1: Class name, 2: version number */
trigger_error(sprintf(__('Class %1$s is <strong>deprecated</strong> since version %2$s with no alternative available.'), $class, $version), E_USER_DEPRECATED);
}
} else {
if (! is_null($replacement)) {
trigger_error(sprintf('Class %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', $class, $version, $replacement), E_USER_DEPRECATED);
} else {
trigger_error(sprintf('Class %1$s is <strong>deprecated</strong> since version %2$s with no alternative available.', $class, $version), E_USER_DEPRECATED);
}
}
}
45 changes: 45 additions & 0 deletions compat/src-deprecated/namespaces.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Deprecated namespaces
*
* @package notification
*/

spl_autoload_register(function ($class) {
$deprecations = [
'[Next]' => [
'BracketSpace\Notification\Defaults' => 'BracketSpace\Notification\Repository',
],
];

// Classes meant to be left as is, ie. deprecated ones.
$exclusions = [
'BracketSpace\Notification\Defaults\Adapter',
];

foreach ($deprecations as $version => $map) {
foreach ($map as $oldNamespace => $newNamespace) {
// Match the loaded classname.
if (strpos($class, $oldNamespace) !== 0) {
continue;
}

// Check for exclusions.
foreach ($exclusions as $excludedNamespace) {
if (strpos($class, $excludedNamespace) !== 0) {
break 2;
}
}

$newClass = str_replace($oldNamespace, $newNamespace, $class);

if (! class_exists($newClass)) {
break;
}

notification_deprecated_class($class, $version, $newClass);

class_alias($newClass, $class);
}
}
});
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
"dependencies/"
],
"files": [
"compat/src-deprecated/helpers.php",
"compat/src-deprecated/namespaces.php",
"compat/src-deprecated/functions.php"
]
},
Expand Down
Loading

0 comments on commit 9964f0b

Please sign in to comment.