Skip to content
Snippets Groups Projects
Unverified Commit 35715e1a authored by Bartek Jaskulski's avatar Bartek Jaskulski
Browse files

feat: add strong typing to the package

parent e4208af7
No related branches found
No related tags found
2 merge requests!28release: 3.0.0,!19Add strong typing for 3.0 version
Showing
with 345 additions and 335 deletions
# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org
# WordPress Coding Standards
# https://make.wordpress.org/core/handbook/coding-standards/
# Configuration for PhpStorm
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = tab
indent_size = 4
tab_width = 4
ij_continuation_indent_size = 4
[*.scss]
ij_scss_enforce_quotes_on_format = true
ij_scss_hex_color_long_format = true
ij_scss_hex_color_lower_case = true
[{*.cjs,*.js}]
indent_style = space
indent_size = 2
ij_javascript_do_while_brace_force = always
ij_javascript_if_brace_force = always
ij_javascript_import_sort_module_name = true
ij_javascript_reformat_c_style_comments = true
ij_javascript_space_after_unary_not = true
ij_javascript_space_before_unary_not = true
ij_javascript_spaces_within_array_initializer_brackets = true
ij_javascript_spaces_within_brackets = true
ij_javascript_spaces_within_catch_parentheses = true
ij_javascript_spaces_within_for_parentheses = true
ij_javascript_spaces_within_if_parentheses = true
ij_javascript_spaces_within_imports = true
ij_javascript_spaces_within_interpolation_expressions = true
ij_javascript_spaces_within_method_call_parentheses = true
ij_javascript_spaces_within_method_parentheses = true
ij_javascript_spaces_within_object_literal_braces = true
ij_javascript_spaces_within_parentheses = true
ij_javascript_spaces_within_switch_parentheses = true
ij_javascript_spaces_within_while_parentheses = true
ij_javascript_while_brace_force = always
[{*.ctp,*.hphp,*.inc,*.module,*.php,*.php4,*.php5,*.phtml}]
ij_php_align_assignments = true
ij_php_align_class_constants = true
ij_php_align_key_value_pairs = true
ij_php_align_phpdoc_comments = true
ij_php_align_phpdoc_param_names = true
ij_php_class_brace_style = end_of_line
ij_php_comma_after_last_array_element = true
ij_php_else_if_style = combine
ij_php_force_short_declaration_array_style = true
ij_php_getters_setters_naming_style = snake_case
ij_php_if_brace_force = always
ij_php_lower_case_boolean_const = true
ij_php_lower_case_null_const = true
ij_php_method_brace_style = end_of_line
ij_php_phpdoc_blank_line_before_tags = true
ij_php_phpdoc_blank_lines_around_parameters = true
ij_php_phpdoc_keep_blank_lines = false
ij_php_phpdoc_wrap_long_lines = true
ij_php_space_after_type_cast = true
ij_php_space_after_unary_not = true
ij_php_space_before_unary_not = true
ij_php_spaces_around_var_within_brackets = true
ij_php_spaces_within_array_initializer_braces = true
ij_php_spaces_within_catch_parentheses = true
ij_php_spaces_within_for_parentheses = true
ij_php_spaces_within_if_parentheses = true
ij_php_spaces_within_method_call_parentheses = true
ij_php_spaces_within_method_parentheses = true
ij_php_spaces_within_parentheses = true
ij_php_spaces_within_switch_parentheses = true
ij_php_spaces_within_while_parentheses = true
ij_php_ternary_operation_signs_on_next_line = true
ij_php_variable_naming_style = snake_case
[{*.htm,*.html,*.ng,*.sht,*.shtm,*.shtml}]
ij_html_add_new_line_before_tags = div,p,a
ij_html_align_attributes = false
ij_html_do_not_indent_children_of_tags = html,thead,tbody,tfoot
ij_html_new_line_after_last_attribute = when multiline
ij_html_space_inside_empty_tag = true
ij_html_uniform_ident = true
WP_ROOT_FOLDER="${APACHE_DOCUMENT_ROOT}"
TEST_SITE_WP_ADMIN_PATH="/wp-admin"
TEST_SITE_DB_NAME="wptest"
TEST_SITE_DB_HOST="mysqltests"
TEST_SITE_DB_USER="mysql"
TEST_SITE_DB_PASSWORD="mysql"
TEST_SITE_TABLE_PREFIX="wp_"
TEST_DB_NAME="wptest"
TEST_DB_HOST="mysqltests"
TEST_DB_USER="mysql"
TEST_DB_PASSWORD="mysql"
TEST_TABLE_PREFIX="wp_"
TEST_SITE_WP_URL="http://${WOOTESTS_IP}"
TEST_SITE_WP_DOMAIN="${WOOTESTS_IP}"
TEST_SITE_ADMIN_EMAIL="grola@seostudio.pl"
TEST_SITE_ADMIN_USERNAME="admin"
TEST_SITE_ADMIN_PASSWORD="admin"
......@@ -3,3 +3,4 @@
composer.lock
build-coverage
swagger
.phpcs-cache
......@@ -24,10 +24,11 @@
},
"require-dev": {
"phpunit/phpunit": "<7",
"wp-coding-standards/wpcs": "^0.14.1",
"squizlabs/php_codesniffer": "^3.0.2",
"mockery/mockery": "*",
"10up/wp_mock": "*"
"phpstan/phpstan": "^0.12",
"wpdesk/wp-code-sniffer": "^1.2.3",
"10up/wp_mock": "*",
"szepeviktor/phpstan-wordpress": "^0.7.7",
"phpstan/extension-installer": "^1.1"
},
"autoload": {
"psr-4": {
......
<?xml version="1.0"?>
<ruleset name="WordPress Coding Standards for WP Desk Plugin">
<!--
#############################################################################
COMMAND LINE ARGUMENTS
https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml
#############################################################################
-->
<!-- Scan all files. -->
<file>./src</file>
<file>./templates</file>
<!-- Only check PHP files. -->
<arg name="extensions" value="php"/>
<!-- Show progress, show the error codes for each message (source). -->
<arg value="sp"/>
<!-- Check up to 8 files simultaneously. -->
<arg name="parallel" value="8"/>
<!-- Cache outcomes for better performance. Remember to add the file to .gitignore. -->
<arg name="cache" value="./.phpcs-cache"/>
<!--
#############################################################################
USE THE WPDeskCS RULESET
#############################################################################
-->
<!-- Define plugin text domain for i18n. -->
<config name="text_domain" value="shopmagic-for-woocommerce"/>
<!-- This value should be aligned with WordPress support version declared in plugin header -->
<config name="minimum_supported_wp_version" value="5.0"/>
<!-- Set value aligned with supported PHP Version for PHPCompatibilityWP check. -->
<config name="testVersion" value="7.0-"/>
<rule ref="WPDeskPlugin"/>
<rule ref="Squiz.Commenting.ClassComment.Missing">
<exclude name="Squiz.Commenting.ClassComment.Missing"/>
</rule>
</ruleset>
parameters:
level: max
paths:
- src/
......@@ -13,11 +13,5 @@
</whitelist>
</filter>
<logging>
<log type="junit" target="build-coverage/report.junit.xml"/>
<log type="coverage-html" target="build-coverage/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build-coverage/coverage.txt"/>
<log type="coverage-clover" target="build-coverage/clover.xml"/>
</logging>
</phpunit>
\ No newline at end of file
......@@ -2,7 +2,6 @@
namespace WPDesk\Forms;
use Psr\Container\ContainerInterface;
use WPDesk\Persistence\PersistentContainer;
......@@ -12,18 +11,12 @@ use WPDesk\Persistence\PersistentContainer;
* @package WPDesk\Forms
*/
interface ContainerForm {
/**
* @param ContainerInterface $data
*
* @return void
*/
public function set_data( $data );
/** @return void */
public function set_data( ContainerInterface $data );
/**
* Put data from form into a container.
*
* @param PersistentContainer $container Target container.
*
* @return void
*/
public function put_data( PersistentContainer $container );
......
......@@ -3,10 +3,6 @@
namespace WPDesk\Forms;
interface Escaper {
/**
* @param mixed $value
*
* @return string
*/
public function escape( $value );
/** @param mixed $value */
public function escape( $value ): string;
}
......@@ -2,8 +2,6 @@
namespace WPDesk\Forms;
use WPDesk\Forms\Field\BasicField;
/**
* The idea is that from the moment the factory returns this interface it's values cannot be changed.
* And that is why here are only the getters.
......@@ -13,157 +11,88 @@ use WPDesk\Forms\Field\BasicField;
* @package WPDesk\Forms
*/
interface Field {
/** @return string */
public function get_name();
public function get_name(): string;
/** @return mixed */
public function get_default_value();
/** @return string */
public function get_template_name();
public function get_template_name(): string;
/**
* When this field is used on form this field will force it's own template.
*
* return bool
*/
public function should_override_form_template();
/** When this field is used on form this field will force it's own template. */
public function should_override_form_template(): bool;
/**
* HTML label.
*
* @return string
*/
public function get_label();
/** HTML label. */
public function get_label(): string;
/** bool */
public function has_label();
public function has_label(): bool;
/**
* Description for field. It can be shown near the field.
*
* @return string
*/
public function get_description();
public function get_description(): string;
/**
* Additional field description that should be shown in optional hover tip.
*
* @return string
*/
public function get_description_tip();
/** Additional field description that should be shown in optional hover tip. */
public function get_description_tip(): string;
/** @return bool */
public function has_description_tip();
public function has_description_tip(): bool;
/** @return bool */
public function has_description();
public function has_description(): bool;
/**
* @return bool
*/
public function is_readonly();
public function is_readonly(): bool;
/** @return bool */
public function is_disabled();
public function is_disabled(): bool;
/** @return string */
public function get_id();
public function get_id(): string;
/** @bool */
public function is_required();
public function is_required(): bool;
/** @return bool */
public function has_placeholder();
public function has_placeholder(): bool;
/** @return string */
public function get_placeholder();
public function get_placeholder(): string;
/**
* @param string[] $except
*
* @return string[] name->value
*/
public function get_attributes( $except = [] );
public function get_attributes( array $except = [] ): array;
/**
* @param string $name
* @param string $default
* @param ?string $default
*
* @return string
*/
public function get_attribute( $name, $default = null );
public function get_attribute( string $name, $default = null ): string;
/** @return bool */
public function is_attribute_set( $name );
public function is_attribute_set( string $name ): bool;
/**
* @param string $name
*
* @return string
*/
public function get_meta_value( $name );
public function get_meta_value( string $name ): string;
/** @return bool */
public function is_meta_value_set( $name );
public function is_meta_value_set( string $name ): bool;
/**
* @return string
*/
public function get_classes();
public function get_classes(): string;
/** bool */
public function has_classes();
public function has_classes(): bool;
/** @return bool */
public function is_class_set( $name );
public function is_class_set( string $name ): bool;
/** bool */
public function has_data();
public function has_data(): bool;
/**
* @return array
*/
public function get_data();
/** @return array<string,int> */
public function get_data(): array;
/**
* @param string $data_name
* @param string $data_value
*
* @return $this
*/
public function add_data( $data_name, $data_value );
public function add_data( string $data_name, string $data_value ): self;
/**
* @param string $data_name
*
* @return $this
*/
public function unset_data( $data_name );
public function unset_data( string $data_name ): self;
/**
* @return mixed
*/
/** @return mixed */
public function get_possible_values();
/**
* @return bool
*/
public function is_multiple();
public function is_multiple(): bool;
/**
* @return Validator
*/
public function get_validator();
public function get_validator(): Validator;
/**
* @return Sanitizer
*/
public function get_sanitizer();
public function get_sanitizer(): Sanitizer;
/** @return Serializer */
public function get_serializer();
public function get_serializer(): Serializer;
/** @return int */
public function get_priority();
public function get_priority(): int;
}
......@@ -4,9 +4,11 @@ namespace WPDesk\Forms\Field;
use WPDesk\Forms\Field;
use WPDesk\Forms\Form\FormWithFields;
use WPDesk\Forms\Sanitizer;
use WPDesk\Forms\Sanitizer\NoSanitize;
use WPDesk\Forms\Serializer;
use WPDesk\Forms\Serializer\NoSerialize;
use WPDesk\Forms\Validator;
use WPDesk\Forms\Validator\ChainValidator;
use WPDesk\Forms\Validator\RequiredValidator;
......@@ -19,209 +21,164 @@ use WPDesk\Forms\Validator\RequiredValidator;
abstract class BasicField implements Field {
use Field\Traits\HtmlAttributes;
/** @var array[] */
const DEFAULT_PRIORITY = 10;
/** @var array<string,int,bool> */
protected $meta;
/** @var string */
protected $default_value;
public function __construct() {
$this->meta['class'] = [];
$this->meta['priority'] = 10;
$this->meta['priority'] = self::DEFAULT_PRIORITY;
}
public function get_label() {
public function get_label(): string {
return $this->meta['label'];
}
/**
* @param string $value
*
* @return $this
*/
public function set_label( $value ) {
public function set_label( string $value ): self {
$this->meta['label'] = $value;
return $this;
}
public function get_description_tip() {
public function get_description_tip(): string {
return $this->meta['description_tip'];
}
public function has_description_tip() {
public function has_description_tip(): bool {
return isset( $this->meta['description_tip'] );
}
public function should_override_form_template() {
return isset( $this->attributes['overrite_template'] ) ? $this->attributes['overrite_template'] : false;
public function should_override_form_template(): bool {
return $this->attributes['overrite_template'] ?? false;
}
public function get_description() {
public function get_description(): string {
return $this->meta['description'];
}
public function has_label() {
public function has_label(): bool {
return isset( $this->meta['label'] );
}
public function has_description() {
public function has_description(): bool {
return isset( $this->meta['description'] );
}
public function set_description( $value ) {
public function set_description( string $value ): self {
$this->meta['description'] = $value;
return $this;
}
public function set_description_tip( $value ) {
public function set_description_tip( string $value ): self {
$this->meta['description_tip'] = $value;
return $this;
}
/**
* @return array
*
* @deprecated not sure if needed. TODO: Check later.
*/
public function get_type() {
public function get_type(): string {
return $this->attributes['type'];
}
/**
* @param string $value
*
* @return $this
*/
public function set_placeholder( $value ) {
public function set_placeholder( string $value ): self {
$this->meta['placeholder'] = $value;
return $this;
}
public function has_placeholder() {
public function has_placeholder(): bool {
return isset( $this->meta['placeholder'] );
}
public function get_placeholder() {
public function get_placeholder(): string {
return $this->meta['placeholder'];
}
/**
* @param string $name
*
* @return $this
*/
public function set_name( $name ) {
public function set_name( string $name ): self {
$this->attributes['name'] = $name;
return $this;
}
public function get_meta_value( $name ) {
public function get_meta_value( string $name ): string {
return $this->meta[ $name ];
}
public function get_classes() {
public function get_classes(): string {
return implode( ' ', $this->meta['class'] );
}
public function has_classes() {
public function has_classes(): bool {
return ! empty( $this->meta['class'] );
}
public function has_data() {
public function has_data(): bool {
return ! empty( $this->meta['data'] );
}
/**
* @return array
*/
public function get_data() {
return empty( $this->meta['data'] ) ? [] : $this->meta['data'];
public function get_data(): array {
return $this->meta['data'] ?: [];
}
public function get_possible_values() {
return isset( $this->meta['possible_values'] ) ? $this->meta['possible_values'] : [];
}
public function get_id() {
return isset( $this->attributes['id'] ) ? $this->attributes['id'] : sanitize_title( $this->get_name() );
public function get_id(): string {
return $this->attributes['id'] ?? sanitize_title( $this->get_name() );
}
public function get_name() {
public function get_name(): string {
return $this->attributes['name'];
}
public function is_multiple() {
return isset( $this->attributes['multiple'] ) ? $this->attributes['multiple'] : false;
public function is_multiple(): bool {
return $this->attributes['multiple'] ?? false;
}
/**
* @return $this
*/
public function set_disabled() {
public function set_disabled(): self {
$this->attributes['disabled'] = true;
return $this;
}
public function is_disabled() {
return isset( $this->attributes['disabled'] ) ? $this->attributes['disabled'] : false;
public function is_disabled(): bool {
return $this->attributes['disabled'] ?? false;
}
/**
* @return $this
*/
public function set_readonly() {
public function set_readonly(): self {
$this->attributes['readonly'] = true;
return $this;
}
public function is_readonly() {
return isset( $this->attributes['readonly'] ) ? $this->attributes['readonly'] : false;
public function is_readonly(): bool {
return $this->attributes['readonly'] ?? false;
}
/**
* @return $this
*/
public function set_required() {
public function set_required(): self {
$this->meta['required'] = true;
return $this;
}
/**
* @param string $class_name
*
* @return $this
*/
public function add_class( $class_name ) {
public function add_class( string $class_name ): self {
$this->meta['class'][ $class_name ] = $class_name;
return $this;
}
/**
* @param string $class_name
*
* @return $this
*/
public function unset_class( $class_name ) {
public function unset_class( string $class_name ): self {
unset( $this->meta['class'][ $class_name ] );
return $this;
}
/**
* @param string $data_name
* @param string $data_value
*
* @return $this
*/
public function add_data( $data_name, $data_value ) {
public function add_data( string $data_name, string $data_value ): self {
if ( ! isset( $this->meta['data'] ) ) {
$this->meta['data'] = [];
}
......@@ -230,44 +187,31 @@ abstract class BasicField implements Field {
return $this;
}
/**
* @param string $data_name
*
* @return $this
*/
public function unset_data( $data_name ) {
public function unset_data( string $data_name ): self {
unset( $this->meta['data'][ $data_name ] );
return $this;
}
public function is_meta_value_set( $name ) {
public function is_meta_value_set( string $name ): bool {
return isset( $this->meta[ $name ] );
}
public function is_class_set( $name ) {
public function is_class_set( string $name ): bool {
return isset( $this->meta['class'][ $name ] );
}
public function get_default_value() {
public function get_default_value(): string {
return $this->default_value;
}
/**
* @param string $value
*
* @return $this
*/
public function set_default_value( $value ) {
public function set_default_value( string $value ): self {
$this->default_value = $value;
return $this;
}
/**
* @return ChainValidator
*/
public function get_validator() {
public function get_validator(): Validator {
$chain = new ChainValidator();
if ( $this->is_required() ) {
$chain->attach( new RequiredValidator() );
......@@ -276,18 +220,15 @@ abstract class BasicField implements Field {
return $chain;
}
public function is_required() {
return isset( $this->meta['required'] ) ? $this->meta['required'] : false;
public function is_required(): bool {
return $this->meta['required'] ?? false;
}
public function get_sanitizer() {
public function get_sanitizer(): Sanitizer {
return new NoSanitize();
}
/**
* @return Serializer
*/
public function get_serializer() {
public function get_serializer(): Serializer {
if ( isset( $this->meta['serializer'] ) && $this->meta['serializer'] instanceof Serializer ) {
return $this->meta['serializer'];
}
......@@ -295,7 +236,7 @@ abstract class BasicField implements Field {
return new NoSerialize();
}
public function set_serializer( Serializer $serializer ) {
public function set_serializer( Serializer $serializer ): self {
$this->meta['serializer'] = $serializer;
return $this;
......
......@@ -2,14 +2,12 @@
namespace WPDesk\Forms\Field;
class ButtonField extends NoValueField
{
public function get_template_name()
{
class ButtonField extends NoValueField {
public function get_template_name(): string {
return 'button';
}
public function get_type()
{
public function get_type(): string {
return 'button';
}
}
......@@ -2,7 +2,6 @@
namespace WPDesk\Forms\Field;
class CheckboxField extends BasicField {
const VALUE_TRUE = 'yes';
const VALUE_FALSE = 'no';
......@@ -12,21 +11,21 @@ class CheckboxField extends BasicField {
$this->set_attribute( 'type', 'checkbox' );
}
public function get_template_name() {
public function get_template_name(): string {
return 'input-checkbox';
}
public function get_sublabel() {
public function get_sublabel(): string {
return $this->meta['sublabel'];
}
public function set_sublabel( $value ) {
public function set_sublabel( string $value ): self {
$this->meta['sublabel'] = $value;
return $this;
}
public function has_sublabel() {
public function has_sublabel(): bool {
return isset( $this->meta['sublabel'] );
}
}
......@@ -2,6 +2,7 @@
namespace WPDesk\Forms\Field;
use WPDesk\Forms\Sanitizer;
use WPDesk\Forms\Sanitizer\TextFieldSanitizer;
class DatePickerField extends BasicField {
......@@ -13,11 +14,11 @@ class DatePickerField extends BasicField {
$this->set_attribute( 'type', 'text' );
}
public function get_sanitizer() {
public function get_sanitizer(): Sanitizer {
return new TextFieldSanitizer();
}
public function get_template_name() {
public function get_template_name(): string {
return 'input-date-picker';
}
}
......@@ -8,15 +8,15 @@ class Header extends NoValueField {
$this->meta['header_size'] = '';
}
public function get_template_name() {
public function get_template_name(): string {
return 'header';
}
public function should_override_form_template() {
public function should_override_form_template(): bool {
return true;
}
public function set_header_size( $value ) {
public function set_header_size( int $value ): self {
$this->meta['header_size'] = $value;
return $this;
......
......@@ -2,6 +2,7 @@
namespace WPDesk\Forms\Field;
use WPDesk\Forms\Sanitizer;
use WPDesk\Forms\Sanitizer\TextFieldSanitizer;
class HiddenField extends BasicField {
......@@ -11,11 +12,11 @@ class HiddenField extends BasicField {
$this->set_attribute( 'type', 'hidden' );
}
public function get_sanitizer() {
public function get_sanitizer(): Sanitizer {
return new TextFieldSanitizer();
}
public function get_template_name() {
public function get_template_name(): string {
return 'input-hidden';
}
}
......@@ -10,10 +10,7 @@ class ImageInputField extends BasicField {
$this->set_attribute( 'type', 'text' );
}
/**
* @return string
*/
public function get_template_name() {
public function get_template_name(): string {
return 'input-image';
}
}
......@@ -2,6 +2,7 @@
namespace WPDesk\Forms\Field;
use WPDesk\Forms\Sanitizer;
use WPDesk\Forms\Sanitizer\TextFieldSanitizer;
class InputNumberField extends BasicField {
......@@ -11,11 +12,11 @@ class InputNumberField extends BasicField {
$this->set_attribute( 'type', 'number' );
}
public function get_sanitizer() {
public function get_sanitizer(): Sanitizer {
return new TextFieldSanitizer();
}
public function get_template_name() {
public function get_template_name(): string {
return 'input-number';
}
}
......@@ -2,6 +2,7 @@
namespace WPDesk\Forms\Field;
use WPDesk\Forms\Sanitizer;
use WPDesk\Forms\Sanitizer\TextFieldSanitizer;
class InputTextField extends BasicField {
......@@ -11,11 +12,11 @@ class InputTextField extends BasicField {
$this->set_attribute( 'type', 'text' );
}
public function get_sanitizer() {
public function get_sanitizer(): Sanitizer {
return new TextFieldSanitizer();
}
public function get_template_name() {
public function get_template_name(): string {
return 'input-text';
}
}
......@@ -4,10 +4,7 @@ namespace WPDesk\Forms\Field;
class MultipleInputTextField extends InputTextField {
/**
* @return string
*/
public function get_template_name() {
public function get_template_name(): string {
return 'input-text-multiple';
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment