Skip to content

Commit

Permalink
Analyzer: add JSON serialization (#21910)
Browse files Browse the repository at this point in the history
  • Loading branch information
brbrr committed Dec 6, 2021
1 parent ce49163 commit 249bf4a
Show file tree
Hide file tree
Showing 23 changed files with 389 additions and 187 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Changes to Analyzer serialization
24 changes: 24 additions & 0 deletions projects/packages/analyzer/src/Declarations/class-class-const.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ function to_csv_array() {
);
}

/**
* Returns a serializable representation of the object.
*
* @return array
*/
public function to_map() {
return array(
'decl_type' => $this->type(),
'file_path' => $this->path,
'file_line' => $this->line,
'class_name' => $this->class_name,
'member_name' => $this->const_name,
);
}

/**
* Create object from deserialized JSON object
*
* @param object $obj deserialized JSON object.
*/
public static function from_map( $obj ) {
return new Class_Const( $obj->file_path, $obj->file_line, $obj->class_name, $obj->member_name );
}

function type() {
return 'class_const';
}
Expand Down
36 changes: 36 additions & 0 deletions projects/packages/analyzer/src/Declarations/class-class-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,42 @@ function to_csv_array() {
);
}

/**
* Returns a serializable representation of the object.
*
* @return array
*/
public function to_map() {
return array(
'decl_type' => $this->type(),
'file_path' => $this->path,
'file_line' => $this->line,
'class_name' => $this->class_name,
'member_name' => $this->method_name,
'is_static' => $this->static,
'fnc_params' => $this->params,
'is_deprecated' => $this->deprecated,
);
}

/**
* Create object from deserialized JSON object
*
* @param object $obj deserialized JSON object.
*/
public static function from_map( $obj ) {
$declaration = new Class_Method( $obj->file_path, $obj->file_line, $obj->class_name, $obj->member_name, $obj->is_static, $obj->is_deprecated );

if ( is_array( $obj->fnc_params ) ) {
foreach ( $obj->fnc_params as $param ) {
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
$declaration->add_param( $param->name, $param->default, $param->type, $param->byRef, $param->variadic );
}
}

return $declaration;
}

function type() {
return 'method';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,31 @@ function to_csv_array() {
);
}

/**
* Returns a serializable representation of the object.
*
* @return array
*/
public function to_map() {
return array(
'decl_type' => $this->type(),
'file_path' => $this->path,
'file_line' => $this->line,
'class_name' => $this->class_name,
'member_name' => $this->prop_name,
'is_static' => $this->static,
);
}

/**
* Create object from deserialized JSON object
*
* @param object $obj deserialized JSON object.
*/
public static function from_map( $obj ) {
return new Class_Property( $obj->file_path, $obj->file_line, $obj->class_name, $obj->member_name, $obj->is_static );
}

function type() {
return 'property';
}
Expand Down
23 changes: 23 additions & 0 deletions projects/packages/analyzer/src/Declarations/class-class.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,29 @@ function to_csv_array() {
);
}

/**
* Returns a serializable representation of the object.
*
* @return array
*/
public function to_map() {
return array(
'decl_type' => $this->type(),
'file_path' => $this->path,
'file_line' => $this->line,
'class_name' => $this->class_name,
);
}

/**
* Create object from deserialized JSON object
*
* @param object $obj deserialized JSON object.
*/
public static function from_map( $obj ) {
return new Class_( $obj->file_path, $obj->file_line, $obj->class_name );
}

function type() {
return 'class';
}
Expand Down
33 changes: 33 additions & 0 deletions projects/packages/analyzer/src/Declarations/class-function.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,39 @@ function to_csv_array() {
);
}

/**
* Returns a serializable representation of the object.
*
* @return array
*/
public function to_map() {
return array(
'decl_type' => $this->type(),
'file_path' => $this->path,
'file_line' => $this->line,
'member_name' => $this->func_name,
'fnc_params' => $this->params,
'is_deprecated' => $this->deprecated,
);
}

/**
* Create object from deserialized JSON object
*
* @param object $obj deserialized JSON object.
*/
public static function from_map( $obj ) {
$declaration = new Function_( $obj->file_path, $obj->file_line, $obj->member_name, $obj->is_deprecated );
if ( is_array( $obj->fnc_params ) ) {
foreach ( $obj->fnc_params as $param ) {
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
$declaration->add_param( $param->name, $param->default, $param->type, $param->byRef, $param->variadic );
}
}

return $declaration;
}

function type() {
return 'function';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
namespace Automattic\Jetpack\Analyzer\Differences;

use Automattic\Jetpack\Analyzer\Invocations\Static_Const;
use Automattic\Jetpack\Analyzer\PersistentList\Item as PersistentListItem;
use Automattic\Jetpack\Analyzer\Warnings\Warning;
// TODO - subclasses?
/**
* Class Class_Const_Missing
*/
class Class_Const_Missing extends PersistentListItem implements Invocation_Warner {
class Class_Const_Missing extends Differences_List_Item implements Invocation_Warner {
/**
* Declaration.
*
Expand All @@ -33,17 +32,12 @@ public function __construct( $declaration ) {
}

/**
* Return array of declaration items.
* Returns serializable object.
*
* @return array
* @return object
*/
public function to_csv_array() {
return array(
$this->type(),
$this->declaration->path,
$this->declaration->line,
$this->declaration->display_name(),
);
protected function get_serializable() {
return array( 'old_declaration' => $this->declaration );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
namespace Automattic\Jetpack\Analyzer\Differences;

use Automattic\Jetpack\Analyzer\Invocations\Static_Const;
use Automattic\Jetpack\Analyzer\PersistentList\Item as PersistentListItem;
use Automattic\Jetpack\Analyzer\Warnings\Warning;
// TODO - subclasses?
/**
* Class Class_Const_Moved
*/
class Class_Const_Moved extends PersistentListItem implements Invocation_Warner {
class Class_Const_Moved extends Differences_List_Item implements Invocation_Warner {
/**
* Old declaration.
*
Expand All @@ -42,16 +41,14 @@ public function __construct( $old_declaration, $new_declaration ) {
}

/**
* Return array of declaration items.
* Returns serializable object.
*
* @return array
*/
public function to_csv_array() {
protected function get_serializable() {
return array(
$this->type(),
$this->old_declaration->path,
$this->old_declaration->line,
$this->old_declaration->display_name(),
'old_declaration' => $this->old_declaration,
'new_declaration' => $this->new_declaration,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,36 @@

namespace Automattic\Jetpack\Analyzer\Differences;

use Automattic\Jetpack\Analyzer\PersistentList\Item as PersistentListItem;
use Automattic\Jetpack\Analyzer\Warnings\Warning;
// TODO - subclasses?
/**
* Class Class_Method_Deprecated
*/
class Class_Method_Deprecated extends PersistentListItem implements Invocation_Warner {
class Class_Method_Deprecated extends Differences_List_Item implements Invocation_Warner {
/**
* Old declaration.
*
* @var object
*/
public $old_declaration;
/**
* New declaration.
*
* @var object
*/
public $new_declaration;

/**
* Class_Method_Deprecated constructor.
*
* @param object $old_declaration Old declaration.
* @param object $new_declaration New declaration.
*/
public function __construct( $old_declaration, $new_declaration ) {
public function __construct( $old_declaration ) {
$this->old_declaration = $old_declaration;
$this->new_declaration = $new_declaration;
}

/**
* Return array of declaration items.
* Returns serializable object.
*
* @return array
* @return object
*/
public function to_csv_array() {
return array(
$this->type(),
$this->old_declaration->path,
$this->old_declaration->line,
$this->old_declaration->display_name(),
);
protected function get_serializable() {
return array( 'old_declaration' => $this->old_declaration );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
namespace Automattic\Jetpack\Analyzer\Differences;

use Automattic\Jetpack\Analyzer\Invocations\Static_Call;
use Automattic\Jetpack\Analyzer\PersistentList\Item as PersistentListItem;
use Automattic\Jetpack\Analyzer\Warnings\Warning;
// TODO - subclasses?
/**
* Class Class_Method_Missing
*/
class Class_Method_Missing extends PersistentListItem implements Invocation_Warner {
class Class_Method_Missing extends Differences_List_Item implements Invocation_Warner {
/**
* Declaration.
*
Expand All @@ -33,17 +32,12 @@ public function __construct( $declaration ) {
}

/**
* Return array of declaration items.
* Returns serializable object.
*
* @return array
* @return object
*/
public function to_csv_array() {
return array(
$this->type(),
$this->declaration->path,
$this->declaration->line,
$this->declaration->display_name(),
);
protected function get_serializable() {
return array( 'old_declaration' => $this->declaration );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@

namespace Automattic\Jetpack\Analyzer\Differences;

use Automattic\Jetpack\Analyzer\PersistentList\Item as PersistentListItem;
use Automattic\Jetpack\Analyzer\Warnings\Warning;
// TODO - subclasses?
/**
* Class Class_Method_Moved
*/
class Class_Method_Moved extends PersistentListItem implements Invocation_Warner {
class Class_Method_Moved extends Differences_List_Item implements Invocation_Warner {
/**
* Old declaration.
*
Expand All @@ -40,16 +39,14 @@ public function __construct( $old_declaration, $new_declaration ) {
}

/**
* Return array of declaration items.
* Returns serializable object.
*
* @return array
*/
public function to_csv_array() {
protected function get_serializable() {
return array(
$this->type(),
$this->old_declaration->path,
$this->old_declaration->line,
$this->old_declaration->display_name(),
'old_declaration' => $this->old_declaration,
'new_declaration' => $this->new_declaration,
);
}

Expand Down
Loading

0 comments on commit 249bf4a

Please sign in to comment.