Skip to content

Commit

Permalink
Move source classes to the Yoast\WHIPv2 namespace
Browse files Browse the repository at this point in the history
Also includes backslashing of the global namespace functions
  • Loading branch information
enricobattocchi committed Dec 18, 2023
1 parent 71a47ac commit 7d14d01
Show file tree
Hide file tree
Showing 35 changed files with 251 additions and 265 deletions.
1 change: 1 addition & 0 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
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.
Expand All @@ -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' );
}

Expand Down Expand Up @@ -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.
Expand All @@ -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 );
}
Expand All @@ -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;
Expand All @@ -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 );
}
Expand All @@ -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;
Expand All @@ -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/';
}

Expand Down
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.
Expand Down Expand Up @@ -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.
Expand Down
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.
Expand All @@ -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();
}
}
Expand All @@ -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;
}

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

$this->deleteMessages();

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

/**
Expand All @@ -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;
}
Expand Down
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.
Expand Down Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -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() );
}

/**
Expand Down
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.
Expand Down Expand Up @@ -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 );
}

Expand Down Expand Up @@ -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 );
}
}
Expand Down
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.
Expand All @@ -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 );
}

/**
Expand All @@ -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;
}
Expand Down
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.
Expand Down Expand Up @@ -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();
Expand All @@ -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.
Expand All @@ -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

0 comments on commit 7d14d01

Please sign in to comment.