Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move source classes to the Yoast\WHIPv2 namespace #157

Merged
merged 5 commits into from
Dec 22, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Move source classes to the Yoast\WHIPv2 namespace
Also includes backslashing of the global namespace functions
  • Loading branch information
enricobattocchi committed Dec 22, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 75ff9d599a249e743fcfd693ad98ac111676f314
1 change: 1 addition & 0 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@
<!-- Provide the plugin specific prefixes for all naming related sniffs. -->
<property name="prefixes" type="array">
<element value="Yoast\WHIP"/>
<element value="Yoast\WHIPv2"/>
<element value="whip"/>
</property>
</properties>
14 changes: 7 additions & 7 deletions src/Whip_Configuration.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/**
* WHIP libary file.
*
* @package Yoast\WHIP
*/

namespace Yoast\WHIPv2;

use Yoast\WHIPv2\Exceptions\Whip_InvalidType;
use Yoast\WHIPv2\Interfaces\Whip_Requirement;

/**
* Class Whip_Configuration.
@@ -25,7 +25,7 @@ class Whip_Configuration {
* @throws Whip_InvalidType When the $configuration parameter is not of the expected type.
*/
public function __construct( $configuration = array() ) {
if ( ! is_array( $configuration ) ) {
if ( ! \is_array( $configuration ) ) {
throw new Whip_InvalidType( 'Configuration', $configuration, 'array' );
}

@@ -56,6 +56,6 @@ public function configuredVersion( Whip_Requirement $requirement ) {
* @return bool Whether or not the requirement is present in the configuration.
*/
public function hasRequirementConfigured( Whip_Requirement $requirement ) {
return array_key_exists( $requirement->component(), $this->configuration );
return \array_key_exists( $requirement->component(), $this->configuration );
}
}
21 changes: 9 additions & 12 deletions src/Whip_Host.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?php
/**
* WHIP libary file.
*
* @package Yoast\WHIP
*/

namespace Yoast\WHIPv2;

/**
* Represents a host.
@@ -30,7 +27,7 @@ class Whip_Host {
* @return string The name of the host.
*/
public static function name() {
$name = (string) getenv( self::HOST_NAME_KEY );
$name = (string) \getenv( self::HOST_NAME_KEY );

return self::filterName( $name );
}
@@ -44,8 +41,8 @@ public static function name() {
* @return string The filtered name of the host.
*/
private static function filterName( $name ) {
if ( function_exists( 'apply_filters' ) ) {
return (string) apply_filters( strtolower( self::HOST_NAME_KEY ), $name );
if ( \function_exists( 'apply_filters' ) ) {
return (string) \apply_filters( \strtolower( self::HOST_NAME_KEY ), $name );
}

return $name;
@@ -59,7 +56,7 @@ private static function filterName( $name ) {
* @return string The message as set by the host.
*/
public static function message( $messageKey ) {
$message = (string) getenv( $messageKey );
$message = (string) \getenv( $messageKey );

return self::filterMessage( $messageKey, $message );
}
@@ -74,8 +71,8 @@ public static function message( $messageKey ) {
* @return string
*/
private static function filterMessage( $messageKey, $message ) {
if ( function_exists( 'apply_filters' ) ) {
return (string) apply_filters( strtolower( $messageKey ), $message );
if ( \function_exists( 'apply_filters' ) ) {
return (string) \apply_filters( \strtolower( $messageKey ), $message );
}

return $message;
@@ -101,7 +98,7 @@ public static function hostingPageUrl() {
* @return string The new URL to the hosting overview page.
*/
private static function filterHostingPageUrl( $url ) {
if ( function_exists( 'apply_filters' ) && apply_filters( self::HOSTING_PAGE_FILTER_KEY, false ) ) {
if ( \function_exists( 'apply_filters' ) && \apply_filters( self::HOSTING_PAGE_FILTER_KEY, false ) ) {
return 'https://wordpress.org/hosting/';
}

11 changes: 5 additions & 6 deletions src/Whip_MessageDismisser.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php
/**
* WHIP libary file.
*
* @package Yoast\WHIP
*/

namespace Yoast\WHIPv2;

use Yoast\WHIPv2\Interfaces\Whip_DismissStorage;

/**
* A class to dismiss messages.
@@ -71,6 +70,6 @@ public function isDismissed() {
* @return bool True when the nonce is valid.
*/
public function verifyNonce( $nonce, $action ) {
return (bool) wp_verify_nonce( $nonce, $action );
return (bool) \wp_verify_nonce( $nonce, $action );
}
}
7 changes: 2 additions & 5 deletions src/Whip_MessageFormatter.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?php
/**
* WHIP libary file.
*
* @package Yoast\WHIP
*/

namespace Yoast\WHIPv2;

/**
* A helper class to format messages.
18 changes: 9 additions & 9 deletions src/Whip_MessagesManager.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/**
* WHIP libary file.
*
* @package Yoast\WHIP
*/

namespace Yoast\WHIPv2;

use Yoast\WHIPv2\Interfaces\Whip_Message;
use Yoast\WHIPv2\Messages\Whip_NullMessage;

/**
* Manages messages using a global to prevent duplicate messages.
@@ -14,7 +14,7 @@ class Whip_MessagesManager {
* Whip_MessagesManager constructor.
*/
public function __construct() {
if ( ! array_key_exists( 'whip_messages', $GLOBALS ) ) {
if ( ! \array_key_exists( 'whip_messages', $GLOBALS ) ) {
$GLOBALS['whip_messages'] = array();
}
}
@@ -38,7 +38,7 @@ public function addMessage( Whip_Message $message ) {
* @return bool Whether or not there are messages available.
*/
public function hasMessages() {
return isset( $GLOBALS['whip_messages'] ) && count( $GLOBALS['whip_messages'] ) > 0;
return isset( $GLOBALS['whip_messages'] ) && \count( $GLOBALS['whip_messages'] ) > 0;
}

/**
@@ -73,7 +73,7 @@ public function getLatestMessage() {

$this->deleteMessages();

return array_pop( $messages );
return \array_pop( $messages );
}

/**
@@ -84,7 +84,7 @@ public function getLatestMessage() {
* @return array The sorted list of messages.
*/
private function sortByVersion( array $messages ) {
uksort( $messages, 'version_compare' );
\uksort( $messages, 'version_compare' );

return $messages;
}
21 changes: 12 additions & 9 deletions src/Whip_RequirementsChecker.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php
/**
* WHIP libary file.
*
* @package Yoast\WHIP
*/

namespace Yoast\WHIPv2;

use Yoast\WHIPv2\Exceptions\Whip_InvalidType;
use Yoast\WHIPv2\Interfaces\Whip_Message;
use Yoast\WHIPv2\Interfaces\Whip_Requirement;
use Yoast\WHIPv2\Messages\Whip_InvalidVersionRequirementMessage;
use Yoast\WHIPv2\Messages\Whip_UpgradePhpMessage;

/**
* Main controller class to require a certain version of software.
@@ -84,7 +87,7 @@ public function hasRequirements() {
* @return int The total amount of requirements.
*/
public function totalRequirements() {
return count( $this->requirements );
return \count( $this->requirements );
}

/**
@@ -115,11 +118,11 @@ private function requirementIsFulfilled( Whip_Requirement $requirement ) {
$availableVersion = $this->configuration->configuredVersion( $requirement );
$requiredVersion = $requirement->version();

if ( in_array( $requirement->operator(), array( '=', '==', '===' ), true ) ) {
return version_compare( $availableVersion, $requiredVersion, '>=' );
if ( \in_array( $requirement->operator(), array( '=', '==', '===' ), true ) ) {
return \version_compare( $availableVersion, $requiredVersion, '>=' );
}

return version_compare( $availableVersion, $requiredVersion, $requirement->operator() );
return \version_compare( $availableVersion, $requiredVersion, $requirement->operator() );
}

/**
23 changes: 13 additions & 10 deletions src/Whip_VersionRequirement.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php
/**
* WHIP libary file.
*
* @package Yoast\WHIP
*/

namespace Yoast\WHIPv2;

use Yoast\WHIPv2\Exceptions\Whip_EmptyProperty;
use Yoast\WHIPv2\Exceptions\Whip_InvalidOperatorType;
use Yoast\WHIPv2\Exceptions\Whip_InvalidType;
use Yoast\WHIPv2\Exceptions\Whip_InvalidVersionComparisonString;
use Yoast\WHIPv2\Interfaces\Whip_Requirement;

/**
* A value object containing a version requirement for a component version.
@@ -94,7 +97,7 @@ public static function fromCompareString( $component, $comparisonString ) {
([^>=<\s]+) # Matches anything except >, <, =, and whitespace.
`x';

if ( ! preg_match( $matcher, $comparisonString, $match ) ) {
if ( ! \preg_match( $matcher, $comparisonString, $match ) ) {
throw new Whip_InvalidVersionComparisonString( $comparisonString );
}

@@ -122,28 +125,28 @@ private function validateParameters( $component, $version, $operator ) {
throw new Whip_EmptyProperty( 'Component' );
}

if ( ! is_string( $component ) ) {
if ( ! \is_string( $component ) ) {
throw new Whip_InvalidType( 'Component', $component, 'string' );
}

if ( empty( $version ) ) {
throw new Whip_EmptyProperty( 'Version' );
}

if ( ! is_string( $version ) ) {
if ( ! \is_string( $version ) ) {
throw new Whip_InvalidType( 'Version', $version, 'string' );
}

if ( empty( $operator ) ) {
throw new Whip_EmptyProperty( 'Operator' );
}

if ( ! is_string( $operator ) ) {
if ( ! \is_string( $operator ) ) {
throw new Whip_InvalidType( 'Operator', $operator, 'string' );
}

$validOperators = array( '=', '==', '===', '<', '>', '<=', '>=' );
if ( ! in_array( $operator, $validOperators, true ) ) {
if ( ! \in_array( $operator, $validOperators, true ) ) {
throw new Whip_InvalidOperatorType( $operator, $validOperators );
}
}
13 changes: 6 additions & 7 deletions src/Whip_WPDismissOption.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php
/**
* WHIP libary file.
*
* @package Yoast\WHIP
*/

namespace Yoast\WHIPv2;

use Yoast\WHIPv2\Interfaces\Whip_DismissStorage;

/**
* Represents the WordPress option for saving the dismissed messages.
@@ -25,7 +24,7 @@ class Whip_WPDismissOption implements Whip_DismissStorage {
* @return bool True when successful.
*/
public function set( $dismissedValue ) {
return update_option( $this->optionName, $dismissedValue );
return \update_option( $this->optionName, $dismissedValue );
}

/**
@@ -34,7 +33,7 @@ public function set( $dismissedValue ) {
* @return int Returns the value of the option or an empty string when not set.
*/
public function get() {
$dismissedOption = get_option( $this->optionName );
$dismissedOption = \get_option( $this->optionName );
if ( ! $dismissedOption ) {
return 0;
}
19 changes: 9 additions & 10 deletions src/Whip_WPMessageDismissListener.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php
/**
* WHIP libary file.
*
* @package Yoast\WHIP
*/

namespace Yoast\WHIPv2;

use Yoast\WHIPv2\Interfaces\Whip_Listener;

/**
* Listener for dismissing a message.
@@ -41,9 +40,9 @@ public function __construct( Whip_MessageDismisser $dismisser ) {
public function listen() {

// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is verified in the dismisser.
$action = ( isset( $_GET['action'] ) && is_string( $_GET['action'] ) ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : null;
$action = ( isset( $_GET['action'] ) && \is_string( $_GET['action'] ) ) ? \sanitize_text_field( \wp_unslash( $_GET['action'] ) ) : null;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is verified in the dismisser.
$nonce = ( isset( $_GET['nonce'] ) && is_string( $_GET['nonce'] ) ) ? sanitize_text_field( wp_unslash( $_GET['nonce'] ) ) : '';
$nonce = ( isset( $_GET['nonce'] ) && \is_string( $_GET['nonce'] ) ) ? \sanitize_text_field( \wp_unslash( $_GET['nonce'] ) ) : '';

if ( $action === self::ACTION_NAME && $this->dismisser->verifyNonce( $nonce, self::ACTION_NAME ) ) {
$this->dismisser->dismiss();
@@ -56,10 +55,10 @@ public function listen() {
* @return string The url for dismissing the message.
*/
public function getDismissURL() {
return sprintf(
admin_url( 'index.php?action=%1$s&nonce=%2$s' ),
return \sprintf(
\admin_url( 'index.php?action=%1$s&nonce=%2$s' ),
self::ACTION_NAME,
wp_create_nonce( self::ACTION_NAME )
\wp_create_nonce( self::ACTION_NAME )
);
}
}
11 changes: 5 additions & 6 deletions src/exceptions/Whip_EmptyProperty.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php
/**
* WHIP libary file.
*
* @package Yoast\WHIP
*/

namespace Yoast\WHIPv2\Exceptions;

use Exception;

/**
* Class EmptyProperty.
@@ -16,6 +15,6 @@ class Whip_EmptyProperty extends Exception {
* @param string $property Property name.
*/
public function __construct( $property ) {
parent::__construct( sprintf( '%s cannot be empty.', (string) $property ) );
parent::__construct( \sprintf( '%s cannot be empty.', (string) $property ) );
}
}
Loading