Skip to content

Commit

Permalink
Merge pull request #113 from szepeviktor/phpstan-level1
Browse files Browse the repository at this point in the history
Clean up code for further fixing
  • Loading branch information
Nomafin authored Apr 17, 2019
2 parents 1a36bd3 + 2a69a05 commit d854748
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 124 deletions.
122 changes: 64 additions & 58 deletions classes/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Model {
protected $class_name;

// Arguments of this instance
private $args;
private $args = array();

// Instances of all submodels initiated from this class
private $submodels;
Expand All @@ -40,15 +40,21 @@ class Model {
// Is execution terminated
private $terminated;

private $ttl;

private $called_subs;

private $api;

/**
* Constructor for DustPress model class.
*
* @type function
* @date 10/8/2015
* @since 0.2.0
*
* @param load (boolean)
* @return N/A
* @param array $args
* @param mixed $parent
*/
public function __construct( $args = [], $parent = null ) {
$this->fix_deprecated();
Expand All @@ -69,7 +75,7 @@ public function __construct( $args = [], $parent = null ) {
* @date 3/6/2016
* @since 0.4.0
*
* @return void
* @return array
*/
public function get_args() {
return $this->args;
Expand Down Expand Up @@ -109,7 +115,7 @@ public function get_data() {
* @date 3/6/2016
* @since 0.4.0
*
* @return Dustpress\Model
* @return \Dustpress\Model
*/
public function get_submodel( $name ) {
return $this->submodels->{$name};
Expand All @@ -135,7 +141,7 @@ public function get_submodels() {
* @date 3/6/2016
* @since 0.4.0
*
* @return Dustpress\Model
* @return \Dustpress\Model
*/
public function get_ancestor( $model = null ) {
if ( ! isset( $model ) ) {
Expand All @@ -157,9 +163,6 @@ public function get_ancestor( $model = null ) {
* @type function
* @date 16/02/2017
* @since 1.5.5
*
* @param N/A
* @return N/A
*/
public function fix_deprecated() {
// Reassign deprecated "allowed_functions" to "api".
Expand All @@ -176,14 +179,16 @@ public function fix_deprecated() {
* @date 15/10/2015
* @since 0.2.0
*
* @param N/A
* @return N/A
* @param string $functions
* @param bool $tidy
*
* @return mixed
*/
public function fetch_data( $functions = null, $tidy = false ) {
$this->class_name = get_class( $this );

// Create a place to store the wanted data in the global data structure.
if ( ! isset( $this->data[ $this->class_name ] ) ) $this->data[ $this->class_name ] = new \StdClass();
if ( ! isset( $this->data[ $this->class_name ] ) ) $this->data[ $this->class_name ] = new \stdClass();

// Fetch all methods from given class and in its parents.
$methods = $this->get_class_methods( $this->class_name );
Expand Down Expand Up @@ -213,7 +218,7 @@ public function fetch_data( $functions = null, $tidy = false ) {
}
else {
if ( ! $this->is_function_allowed( $method_item ) ) {
die( json_encode( [ "error" => "Method '". $function ."' is not allowed to be run via AJAX or does not exist." ] ) );
die( json_encode( [ "error" => "Method '". $method_item ."' is not allowed to be run via AJAX or does not exist." ] ) );
}
else if ( $reflection->isProtected() || $reflection->isPrivate() ) {
$private_methods[] = $method_item;
Expand Down Expand Up @@ -353,8 +358,8 @@ public function fetch_data( $functions = null, $tidy = false ) {
* @date 19/3/2015
* @since 0.0.1
*
* @param $class_name (string)
* @param $methods (array)
* @param string $class_name
* @param array $methods
* @return $methods (array)
*/
private function get_class_methods( $class_name, $methods = array() ) {
Expand Down Expand Up @@ -448,7 +453,7 @@ public function bind_sub( $name, $args = null, $cache_sub = true ) {
}

if ( $model->terminated == true ) {
$this-terminate();
$this->terminate();
}
}

Expand All @@ -460,9 +465,9 @@ public function bind_sub( $name, $args = null, $cache_sub = true ) {
* @date 17/3/2015
* @since 0.0.1
*
* @param $data (N/A)
* @param $key (string)
* @param $model (string)
* @param N/A $data
* @param string $key
* @param string $model
* @return true/false (boolean)
*/
public function bind( $data, $key = null, $model = null ) {
Expand All @@ -476,7 +481,7 @@ public function bind( $data, $key = null, $model = null ) {

if ( $model ) {
// Create a place to store the wanted data in the global data structure.
if ( ! isset( $this->data[ $model ] ) ) $this->data[ $model ] = new \StdClass();
if ( ! isset( $this->data[ $model ] ) ) $this->data[ $model ] = new \stdClass();

if ( ! isset( $this->data[ $model ] ) ) {
$this->data[ $model ] = (object)[];
Expand All @@ -485,7 +490,7 @@ public function bind( $data, $key = null, $model = null ) {
}
else {
// Create a place to store the wanted data in the global data structure.
if ( ! isset( $this->data[ $this->class_name ] ) ) $this->data[ $this->class_name ] = new \StdClass();
if ( ! isset( $this->data[ $this->class_name ] ) ) $this->data[ $this->class_name ] = new \stdClass();

if ( ! $this->parent ) {
if ( is_array( $data ) ) {
Expand Down Expand Up @@ -534,8 +539,7 @@ public function get_template() {
* @date 15/10/2015
* @since 0.2.0
*
* @param $template (string)
* @return N/A
* @param string $template
*/
public function set_template( $template ) {
$ancestor = $this->get_ancestor();
Expand All @@ -561,8 +565,9 @@ public function set_template( $template ) {
* @date 29/01/2016
* @since 0.3.1
*
* @param $m (string)
* @return N/A
* @param string $m
*
* @return mixed
*/
private function run_function( $m, $class = null ) {
$cached = $this->get_cached( $m );
Expand Down Expand Up @@ -607,8 +612,9 @@ private function run_function( $m, $class = null ) {
* @date 29/01/2016
* @since 0.3.1
*
* @param $m (string)
* @return N/A
* @param string $m
*
* @return mixed|bool
*/
private function get_cached( $m ) {

Expand Down Expand Up @@ -644,42 +650,42 @@ private function get_cached( $m ) {
* @date 29/01/2016
* @since 0.3.1
*
* @param $m (string), $data (any), $subs (array)
* @return N/A
* @param string $m
* @param mixed $data
* @param array $subs
*/
private function maybe_cache( $m, $data, $subs ) {

// Check whether cache is enabled and model has ttl-settings.
if ( dustpress()->get_setting('cache') && $this->is_cacheable_function( $m ) ) {
if ( ! dustpress()->get_setting('cache') || ! $this->is_cacheable_function( $m ) ) {
return;
}

// Extend data with submodels
$to_cache = (object)[ 'data' => $data, 'subs' => $subs ];
// If no hash key exists, bail out
if ( empty( $this->hash ) ) {
return;
}

set_transient( $this->hash, $to_cache, $this->ttl[ $m ] );
// Extend data with submodels
$to_cache = (object)[ 'data' => $data, 'subs' => $subs ];

// If no hash key exists, bail
if ( ! isset( $this->hash ) ) {
return;
}
set_transient( $this->hash, $to_cache, $this->ttl[ $m ] );

// Index key for cache clearing
$index = $this->generate_cache_key( $this->class_name, $m );
$hash_index = get_transient( $index );
// Index key for cache clearing
$index = $this->generate_cache_key( $this->class_name, $m );
$hash_index = get_transient( $index );

if ( ! is_array( $hash_index ) ) {
$hash_index = [];
}

// Set the data hash key to the index array of this model function
if ( ! in_array( $this->hash, $hash_index ) ) {
$hash_index[] = $this->hash;
}
if ( ! is_array( $hash_index ) ) {
$hash_index = [];
}

// Store transient for 30 days
set_transient( $index, $hash_index, 30 * DAY_IN_SECONDS );
// Set the data hash key to the index array of this model function
if ( ! in_array( $this->hash, $hash_index ) ) {
$hash_index[] = $this->hash;
}

return false;
// Store transient for 30 days
set_transient( $index, $hash_index, 30 * DAY_IN_SECONDS );
}

/**
Expand Down Expand Up @@ -712,7 +718,7 @@ private function is_cacheable_function( $m ) {
* @date 17/12/2015
* @since 0.3.0
*
* @param $function (string)
* @param string $function
* @return $allowed (boolean)
*/
private function is_function_allowed( $function ) {
Expand Down Expand Up @@ -740,7 +746,7 @@ private function is_function_allowed( $function ) {
* @date 17/12/2015
* @since 0.3.0
*
* @param $args (ellipsis)
* @param ellipsis $args
* @return $key (string)
*/
private function generate_cache_key() {
Expand All @@ -765,8 +771,8 @@ private function generate_cache_key() {
* @since 0.3.0
*
*
* @param $function (string)
* @param $args (array)
* @param string $function
* @param array $args
* @return mixed
*/
public function run_restricted( $function ) {
Expand All @@ -786,8 +792,8 @@ public function run_restricted( $function ) {
* @since 1.2.0
*
*
* @param $function (string)
* @param $args (array)
* @param string $function
* @param array $args
* @return mixed
*/
protected function rename_model( $name ) {
Expand All @@ -811,7 +817,7 @@ protected function rename_model( $name ) {
/**
* A recursive array search.
*
* @param any $needle
* @param mixed $needle
* @param array $haystack
* @param boolean $strict
* @return boolean
Expand Down
32 changes: 17 additions & 15 deletions classes/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class Query {
* @date 20/3/2015
* @since 0.0.1
*
* @param $id (int)
* @param $args (array)
* @param int $id
* @param array $args
*
* @return array|object|null Type corresponding to output type on success or null on failure.
*/
Expand Down Expand Up @@ -87,8 +87,8 @@ public static function get_post( $id = null, $args = array() ) {
* @date 20/3/2015
* @since 0.0.1
*
* @param $id (int)
* @param $args (array)
* @param int $id
* @param array $args
*
* @return array|object|null Type corresponding to output type on success or null on failure.
*/
Expand All @@ -107,6 +107,7 @@ public static function get_acf_post( $id = null, $args = array() ) {

$options = array_merge( $defaults, $args );

// FIXME -> WP func.
extract( $options );

// Throw error if we have no post to fetch
Expand Down Expand Up @@ -175,10 +176,10 @@ public static function get_acf_post( $id = null, $args = array() ) {
* @date 16/8/2016
* @since 1.1.5
*
* @param array|object $field The current ACF field object.
* @param object $options Recurion options.
* @param array|object $field The current ACF field object.
* @param object $options Recurion options.
*
* @return any $field Returns the same type it is given, possibly extended.
* @return mixed Returns the same type it is given, possibly extended.
*/
private static function handle_field( $field, $options ) {
// No recursion for these post types
Expand Down Expand Up @@ -236,6 +237,7 @@ public static function get_posts( $args ) {

$options = array_merge( $defaults, $args );

// FIXME -> WP function
extract( $options );

self::$query = new WP_Query( $options );
Expand All @@ -262,9 +264,9 @@ public static function get_posts( $args ) {

// Return in the desired format.
return self::query_return_value_format( self::$query , $query_object, $no_found_rows );
} else {
return false;
}
}

return false;
}

/**
Expand All @@ -277,7 +279,7 @@ public static function get_posts( $args ) {
*
* @param array $args Arguments to override the defaults defined in get_wp_query_defaults.
*
* @return array/boolean Array of posts as an associative array with acf fields and meta data
* @return array|boolean Array of posts as an associative array with acf fields and meta data
*/
public static function get_acf_posts( $args ) {

Expand Down Expand Up @@ -347,11 +349,11 @@ public static function get_acf_posts( $args ) {
/**
* A wrapper for posts query function return value formatting.
*
* @param objecy $query The WP_Query object.
* @param boolean $query_object Do we want the whole query object?
* @param boolean $no_found_rows Was the query paginated?
* @param \WP_Query $query The WP_Query object.
* @param boolean $query_object Do we want the whole query object?
* @param boolean $no_found_rows Was the query paginated?
*
* @return object|array
* @return \WP_Query|array
*/
private static function query_return_value_format( $query, $query_object, $no_found_rows ) {
// Maybe return the whole query object
Expand Down
Loading

0 comments on commit d854748

Please sign in to comment.