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

Try to increase quality of the codebase #1654

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1e8978c
Map: Uses cb1 constant for legacy post type
datengraben Oct 31, 2024
c9bb779
API: Also return empty list as valid response
datengraben Oct 31, 2024
3042e8f
CB1: Add accessed variables as class members
datengraben Oct 31, 2024
2392857
makes base constructor final for safe usage of new static()
datengraben Oct 31, 2024
ab8784b
Plugin: removed unused saveOptionsActions
datengraben Oct 31, 2024
c339bbd
Refactors doc block to match return type of void
datengraben Oct 31, 2024
22a76b8
Adds docblocks with matching return types to setCustomColumnSortOrder
datengraben Oct 31, 2024
ccb617b
Fix phpdoc return type
datengraben Oct 31, 2024
73a2734
several missing return statements
datengraben Oct 31, 2024
0fc16f8
Fix several phpdoc WP_Query annotations
datengraben Oct 31, 2024
8d483b3
Makes UserWidget compliant with parent type
datengraben Oct 31, 2024
f6a226c
removes typo
datengraben Oct 31, 2024
79c7093
Fix phpdoc use correct timeframe type
datengraben Oct 31, 2024
a0aab11
make type hint picked up without import
datengraben Oct 31, 2024
dcc84e1
add docblock
datengraben Oct 31, 2024
0ab0172
ruleParams is always an empty list
datengraben Oct 31, 2024
6e3f88e
missing import
datengraben Oct 31, 2024
64e4f17
removes unused param and fix phpdoc
datengraben Oct 31, 2024
2457a28
Merge branch 'master' into feature-codequality
datengraben Nov 4, 2024
492e3b4
declares undeclared variables
datengraben Nov 7, 2024
1a19bde
adds phpdoc details
datengraben Nov 9, 2024
ad4e372
phpdoc: add properties to detect magic methods members
datengraben Nov 9, 2024
00ade2e
Tests: removes unused Timeframe call and variable
datengraben Nov 9, 2024
caac319
TimeframeExport: Moves method call to catch ExportException in constr…
datengraben Nov 9, 2024
b4fed3b
Merge branch 'master' into feature-codequality
datengraben Nov 10, 2024
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
24 changes: 13 additions & 11 deletions src/API/AvailabilityRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@

namespace CommonsBooking\API;

use CommonsBooking\Helper\Wordpress;
use CommonsBooking\Model\Calendar;
use CommonsBooking\Model\Day;
use CommonsBooking\Model\Timeframe;
use CommonsBooking\Model\Week;
use CommonsBooking\Repository\Item;
use DateTime;
use DateTimeZone;
use Exception;
use stdClass;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;

/**
* Endpoint exposes item availability
*
* @See Calendar for computing item availability
* @see Calendar for computing item availability.
*
* @see JSON-schema-Specification {@see https://github.com/wielebenwir/commons-api/blob/master/commons-api.availability.schema.json}
*/
class AvailabilityRoute extends BaseRoute {

Expand All @@ -40,9 +38,7 @@ class AvailabilityRoute extends BaseRoute {
/**
* This retrieves bookable timeframes and the different items assigned, with their respective availability.
*
* @param bool $id The id of a \CommonsBooking\Wordpress\CustomPostType\Item::post_type post to search for
* @param null $startTime The start date of the calendar to get the data for
* @param null $endTime The end date of the calendar to get the data for
* @param bool $id The id of a {@see \CommonsBooking\Wordpress\CustomPostType\Item::post_type} post to search for
*
* @return array
* @throws Exception
Expand All @@ -60,6 +56,10 @@ public function getItemData( $id = false ): array {

/**
* Get one item from the collection
*
* @param $request WP_REST_Request
*
* @return WP_REST_Response|WP_Error
*/
public function get_item( $request ) {
//get parameters from request
Expand All @@ -72,7 +72,9 @@ public function get_item( $request ) {
if ( count( $data->availability ) ) {
return new WP_REST_Response( $data, 200 );
} else {

// This was missing in previous versions. According to the availability spec, we can return a list with no items
// TODO this part and the enclosing if-clause can be removed in future version, if no problems arose ...
return new WP_REST_Response( $data, 200 );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funktioniert das auch? Also gibt der dann erfolgreich eine leere Liste zurück? Wenn ja kann das so bleiben.

}
} catch (Exception $e) {
return new WP_Error( 'code', $e->getMessage() );
Expand All @@ -83,7 +85,7 @@ public function get_item( $request ) {
/**
* Get a collection of items
*
* @param $request Full data about the request.
* @param $request WP_REST_Request full data about the request.
*
* @return WP_REST_Response
*/
Expand Down
25 changes: 25 additions & 0 deletions src/CB/CB1UserFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@

class CB1UserFields {

/**
* @var false|mixed
*/
private mixed $termsservices_url;
/**
* @var array|string[]
*/
private array $registration_fields;
/**
* @var array|array[]
*/
private array $extra_profile_fields;
/**
* @var array|string[]
*/
private array $registration_fields_required;
/**
* @var array|array[]
*/
private array $user_fields;
/**
* @var array|mixed
*/
private mixed $user_vars;

public function __construct() {

// Registration: Form fields
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/TimeframeInvalidException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class TimeframeInvalidException extends \Exception {

public function __construct( $message = "", $code = 0, Throwable $previous = null ) {
public function __construct( $message = "", $code = 0, \Throwable $previous = null ) {
//get immediate caller
if ( debug_backtrace()[1]['class'] == Booking::class ) {
$message .= " " . __( "Booking is saved as draft.", 'commonsbooking' );
Expand Down
2 changes: 2 additions & 0 deletions src/Map/BaseShortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
abstract class BaseShortcode {

final public function __construct() { }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Der Konstruktor wird aber nicht nochmal von den erbenden Klassen benutzt, muss der dann trotzdem hier angegeben werden? Bzw welchen Sinn hat es dann ihn da anzugeben?


/**
* The shortcode handler - load all the needed assets and render the map container
*
Expand Down
23 changes: 20 additions & 3 deletions src/Model/CustomPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
* Pseudo extends WP_Post class.
*
* All the public methods are available as template tags.
* * In using magic methods you can retrieve data from model objects, when the model object class derive from this class.
* * All the public methods are available as template tags.
*
* @package CommonsBooking\Model
*
* @property int $post_author identifier of the WordPress user.
* @property int $post_status describes whether the post is published.
* @property int $ID of the WordPress post.
* @property string $post_title
*/
class CustomPost {
/**
Expand All @@ -27,17 +35,17 @@ class CustomPost {
/**
* CustomPost constructor.
*
* @param int|WP_Post $post
* @param int|WP_Post $post uses either int as id reference or the post object
*
* @throws Exception
* @throws Exception when $post param does not reference a valid post object
*/
public function __construct( $post ) {
if ( $post instanceof WP_Post ) {
$this->post = $post;
} elseif ( is_int( $post ) ) {
$this->post = get_post( $post );
} else {
throw new Exception( "Invalid post param. Needed WP_Post or ID (int)" );
throw new Exception( 'Invalid post param. Needed WP_Post or ID (int)' );
}
}

Expand Down Expand Up @@ -87,6 +95,15 @@ public function __get( $name ) {
}
}

/**
* Enables that we can call methods of \CustomPost as template tags.
*
* @param string $name of the member function
* @param array $arguments given to the template tag.
*
* @return array|mixed|void
* @throws \ReflectionException if called template tag is not a registered method
*/
public function __call( $name, $arguments ) {
if ( method_exists( $this->post, $name ) ) {
$reflectionMethod = new ReflectionMethod( $this->post, $name );
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Timeframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public function getLatestPossibleBookingDateTimestamp() {
$calculationBase = time();

// if meta-value not set we define a default value far in the future so that we count all possibly relevant timeframes
$advanceBookingDays = $this->getMeta( TimeFrame::META_TIMEFRAME_ADVANCE_BOOKING_DAYS ) ?: 365;
$advanceBookingDays = $this->getMeta( Timeframe::META_TIMEFRAME_ADVANCE_BOOKING_DAYS ) ?: 365;

// we subtract one day to reflect the current day in calculation
$advanceBookingDays --;
Expand Down
13 changes: 4 additions & 9 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
use CommonsBooking\Wordpress\Options\OptionsTab;
use CommonsBooking\Wordpress\PostStatus\PostStatus;

/**
* @since 2.10 removed saveOptionsActions, the transient commonsbooking_options_saved which is used in
* Plugin::admin_init is set in OptionsTab class
*/
class Plugin {

use Cache;
Expand Down Expand Up @@ -513,15 +517,6 @@ public static function maybeEnableCB1UserFields() {
}
}

/**
* run actions after plugin options are saved
* TODOD: @markus-mw I think this function is deprecated now. Would you please check this? It is only referenced by an inactive hook
*/
public static function saveOptionsActions() {
// Run actions after options update
set_transient( 'commonsbooking_options_saved', 1 );
}

/**
* Register Admin-Options
*/
Expand Down
6 changes: 4 additions & 2 deletions src/Repository/BookingCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,16 @@ public static function getCodes( int $timeframeId, int $startDate = null, int $e

/**
* Filter an array of BookingCode|s such that it contains only one BookingCode per date.
* Works by reference on the variable.
*
* Function is only needed when new cb version handles database entries created by old cb version.
* The filtering can be omitted in future cb versions when backward compatibility with old cb is dropped.
*
* @param $bookingCodes[] array of BookingCode|s. It is assumed that they all have the same itemId.
* @param array &$bookingCodes reference of array of BookingCode|s. It is assumed that they all have the same itemId.
* @param int $preferredTimeframeId timeframeId to prefer when filtering
* @param int $preferredLocationId locationId to prefer when filtering
*
* @return $bookingCodes[] array of BookingCode|s (only one code per day)
* @return void works by reference on param $bookingCodes
*/
private static function backwardCompatibilityFilter(&$bookingCodes, $preferredTimeframeId, $preferredLocationId) {
$filteredCodes = [];
Expand Down
2 changes: 2 additions & 0 deletions src/Repository/CB1.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ public static function getCB2PostIdByCB1Id( $id ): ?int {
if ( $result && count( $result ) > 0 ) {
return $result[0]->cb2_id;
}

return null;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Service/BookingRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,8 @@ private static function checkBookingRangeForDays( DateTime $startOfRange, DateTi
* @param $appliedTerms
* @param int $allowedTotalBookings
*
* @return void
* @return Booking[]|null
* @throws Exception
*/
private static function checkBookingAmount( DateTime $startOfRange, DateTime $endOfRange, Booking $booking, $appliedTerms, int $allowedTotalBookings ) {
$countedPostTypes = [ 'confirmed' ];
Expand Down
2 changes: 1 addition & 1 deletion src/Service/BookingRuleApplied.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public static function init( bool $ignoreErrors = false ):array{
}
try {
$bookingRule->setAppliedParams(
$ruleParams ?? [],
$ruleParams,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Geht das dann immer noch mit Regeln die keine ruleParams definiert haben? Ich meine ich hab das eingeführt, weil es sonst einen fatal Error mit solchen Regeln gab.

$selectParam ?? null
);
} catch ( BookingRuleException $e ) {
Expand Down
32 changes: 23 additions & 9 deletions src/Service/TimeframeExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,19 @@ public static function ajaxExportCsv() {
}
}

/**
* Exports a file to the given directory path.
* This functions wraps the actual logic of {@see __construct()}, {@see getExportData} and {@see getCsv}.
*
* Note: At the moment this is not a helper to be used outside its context.
* It's heavily coupled to different values set via {@see Settings} and should therefore not be used outside of it's WordPress context.
*
* @param string $exportPath writable directory.
*
* @return void
* @throws CacheException From cache layer.
* @throws InvalidArgumentException From cache layer.
*/
public static function cronExport( $exportPath ) {
$timerange = Settings::getOption( 'commonsbooking_options_export', 'export-timerange' );
$start = date( 'd.m.Y' );
Expand All @@ -201,16 +214,17 @@ public static function cronExport( $exportPath ) {
} else {
$type = 0;
}
$exportObject = new self(
$type,
$start,
$end,
$configuredLocationFields ? self::convertInputFields( $configuredLocationFields ) : null,
$configuredItemFields ? self::convertInputFields( $configuredItemFields ) : null,
$configuredUserFields ? self::convertInputFields( $configuredUserFields ) : null,
);
$exportObject->setCron();

try {
$exportObject = new self(
$type,
$start,
$end,
$configuredLocationFields ? self::convertInputFields( $configuredLocationFields ) : null,
$configuredItemFields ? self::convertInputFields( $configuredItemFields ) : null,
$configuredUserFields ? self::convertInputFields( $configuredUserFields ) : null,
);
$exportObject->setCron();
$exportObject->getExportData();
$exportObject->getCSV( $exportPath . $exportObject->exportFilename );
} catch ( ExportException $e ) {
Expand Down
5 changes: 5 additions & 0 deletions src/Wordpress/CustomPostType/Booking.php
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,11 @@ public function setCustomColumnsData( $column, $post_id ) {
}
}

/**
* @param \WP_Query $query
*
* @return void
*/
public function setCustomColumnSortOrder( \WP_Query $query ) {
if (! parent::setCustomColumnSortOrder( $query ) ) {
return;
Expand Down
10 changes: 6 additions & 4 deletions src/Wordpress/CustomPostType/CustomPostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,15 @@ public static function getModel( $post ) {
}

/**
* This is called by the inheritances of the customPosts, it will just check if we
* are processing one of our CPTs.
* Checks if we query is processing one of our CPTs.
*
* This is called by the inheritances of the customPosts.
*
* @param \WP_Query $query
*
* @return void
* @return bool true if query is about our CPTs, false otherwise
*/
public function setCustomColumnSortOrder(\WP_Query $query) {
public function setCustomColumnSortOrder( \WP_Query $query ) {
if ( ! is_admin() || ! $query->is_main_query() || $query->get( 'post_type' ) !== static::$postType ) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Wordpress/CustomPostType/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ public function savePost($post_id, \WP_Post $post) {
/**
* Filters admin list by type (e.g. bookable, repair etc. )
*
* @param (wp_query object) $query
* @param \WP_Query $query for admin list objects
*
* @return Void
* @return void
*/
public static function filterAdminList( $query ) {
global $pagenow;
Expand Down
5 changes: 3 additions & 2 deletions src/Wordpress/CustomPostType/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public static function termChange($term_id, $tt_id, $taxonomy) {
/**
* Filters admin list by type (e.g. bookable, repair etc. )
*
* @param (wp_query object) $query
* @param \WP_Query $query for admin list objects
*
* @return Void
* @return void
*/
public static function filterAdminList( $query ) {
global $pagenow;
Expand Down Expand Up @@ -435,6 +435,7 @@ public function registerMetabox() {
}

// we store registered metaboxes to options table to be able to retrieve it in export function
$metabox_fields = [];
foreach ($cmb->meta_box['fields'] as $metabox_field) {
$metabox_fields[$metabox_field['id']] = $metabox_field['name'];
}
Expand Down
7 changes: 5 additions & 2 deletions src/Wordpress/CustomPostType/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use CommonsBooking\Map\MapAdmin;
use CommonsBooking\Map\MapSettings;
use CommonsBooking\Map\MapShortcode;
use CommonsBooking\Repository\CB1;
use CommonsBooking\Repository\Item;
use CommonsBooking\Repository\Timeframe;
use Exception;
Expand Down Expand Up @@ -51,8 +52,7 @@ public static function getView() {
**/
public static function replace_map_link_target() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Die Funktion ist sowieso unbenutzt, nicht? Also lohnt es sich vielleicht gar nicht da was dran zu ändern.

global $post;
$cb_item = 'cb_items';
if ( is_object( $post ) && $post->post_type == $cb_item ) {
if ( is_object( $post ) && $post->post_type == CB1::$ITEM_TYPE_ID ) {
//get timeframes of item
$cb_data = new CB_Data();
$date_start = date( 'Y-m-d' ); // current date
Expand Down Expand Up @@ -81,6 +81,9 @@ public static function replace_map_link_target() {
* load all timeframes from db (that end in the future and it's item's status is 'publish')
**/
public static function get_timeframes() {

$result = [];

$timeframes = Timeframe::getBookableForCurrentUser(
[],
[],
Expand Down
Loading