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

Create Check_Result class #50

Merged
merged 7 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
195 changes: 194 additions & 1 deletion includes/Checker/Check_Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,204 @@

namespace WordPress\Plugin_Check\Checker;

use WordPress\Plugin_Check\Plugin_Context;
felixarntz marked this conversation as resolved.
Show resolved Hide resolved
use WordPress\Plugin_Check\Checker\Check_Context;

/**
* Result for running checks on a plugin.
*
* @since n.e.x.t
*/
class Check_Result {
// @TODO: Complete as part of issue #11.

/**
* Main context instance.
*
* @since n.e.x.t
* @var Plugin_Context
*/
protected $main_context;
felixarntz marked this conversation as resolved.
Show resolved Hide resolved

/**
* Context for the plugin to check.
*
* @since n.e.x.t
* @var Check_Context
*/
protected $check_context;

/**
* List of errors.
*
* @since n.e.x.t
* @var array
*/
protected $errors = array();

/**
* List of warnings.
*
* @since n.e.x.t
* @var array
*/
protected $warnings = array();

/**
* Number of errors.
*
* @since n.e.x.t
* @var int
*/
protected $error_count = 0;

/**
* Number of warnings.
*
* @since n.e.x.t
* @var int
*/
protected $warning_count = 0;

/**
* Sets the context for the plugin to check.
*
* @since n.e.x.t
*
* @param Plugin_Context $main_context Main context instance.
* @param Check_Context $check_context Check context instance for the plugin.
*/
public function __construct( Plugin_Context $main_context, Check_Context $check_context ) {
$this->main_context = $main_context;
felixarntz marked this conversation as resolved.
Show resolved Hide resolved
$this->check_context = $check_context;
}

/**
* Returns the main context for the plugin checker.
*
* @since n.e.x.t
*
* @return Plugin_Context Main context instance.
*/
public function context() {
return $this->main_context;
}
felixarntz marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns the context for the plugin to check.
*
* @since n.e.x.t
*
* @return Check_Context Plugin context instance.
*/
public function plugin() {
return $this->check_context;
}

/**
* Adds an error or warning to the respective stack.
*
* @since n.e.x.t
*
* @param bool $error Whether it is an error message.
* @param string $message The message.
* @param array $args {
* Additional message arguments.
*
* @type string $code Violation code according to the message. Default empty string.
* @type string $file The file in which the message occurred. Default empty string (unknown file).
* @type int $line The line on which the message occurred. Default 0 (unknown line).
* @type int $column The column on which the message occurred. Default 0 (unknown column).
* }
*/
public function add_message( $error, $message, $args = array() ) {
$defaults = array(
'code' => '',
'file' => '',
'line' => 0,
'column' => 0,
);

$data = array_merge(
array(
'message' => $message,
),
$defaults,
array_intersect_key( $args, $defaults )
);

$file = str_replace( $this->check_context->path( '/' ), '', $data['file'] );
$line = $data['line'];
$column = $data['column'];
unset( $data['line'], $data['column'] );
felixarntz marked this conversation as resolved.
Show resolved Hide resolved

if ( $error ) {
if ( ! isset( $this->errors[ $file ] ) ) {
$this->errors[ $file ] = array();
}
if ( ! isset( $this->errors[ $file ][ $line ] ) ) {
$this->errors[ $file ][ $line ] = array();
}
if ( ! isset( $this->errors[ $file ][ $line ][ $column ] ) ) {
$this->errors[ $file ][ $line ][ $column ] = array();
}
$this->errors[ $file ][ $line ][ $column ][] = $data;
$this->error_count++;
} else {
if ( ! isset( $this->warnings[ $file ] ) ) {
$this->warnings[ $file ] = array();
}
if ( ! isset( $this->warnings[ $file ][ $line ] ) ) {
$this->warnings[ $file ][ $line ] = array();
}
if ( ! isset( $this->warnings[ $file ][ $line ][ $column ] ) ) {
$this->warnings[ $file ][ $line ][ $column ] = array();
}
$this->warnings[ $file ][ $line ][ $column ][] = $data;
$this->warning_count++;
}
}

/**
* Returns all errors.
*
* @since n.e.x.t
*
* @return array All errors with their data.
*/
public function get_errors() {
return $this->errors;
}

/**
* Returns all warnings.
*
* @since n.e.x.t
*
* @return array All warnings with their data.
*/
public function get_warnings() {
return $this->warnings;
}

/**
* Returns the number of errors.
*
* @since n.e.x.t
*
* @return int Number of errors found.
*/
public function get_error_count() {
return $this->error_count;
}

/**
* Returns the number of warnings.
*
* @since n.e.x.t
*
* @return int Number of warnings found.
*/
public function get_warning_count() {
return $this->warning_count;
}
}
Loading