@@ -14,4 +14,4 @@ |
||
| 14 | 14 | |
| 15 | 15 | use PinkCrab\Plugin_Lifecycle\Plugin_State_Change; |
| 16 | 16 | |
| 17 | -interface Activation extends Plugin_State_Change{} |
|
| 17 | +interface Activation extends Plugin_State_Change {} |
|
@@ -14,4 +14,4 @@ |
||
| 14 | 14 | |
| 15 | 15 | use PinkCrab\Plugin_Lifecycle\Plugin_State_Change; |
| 16 | 16 | |
| 17 | -interface Deactivation extends Plugin_State_Change{} |
|
| 17 | +interface Deactivation extends Plugin_State_Change {} |
|
@@ -24,13 +24,13 @@ discard block |
||
| 24 | 24 | * @param Throwable|null $exception |
| 25 | 25 | * @return Plugin_State_Exception |
| 26 | 26 | */ |
| 27 | - public static function failed_to_create_state_change_event( $event, ?Throwable $exception = null ): Plugin_State_Exception { |
|
| 27 | + public static function failed_to_create_state_change_event($event, ?Throwable $exception = null): Plugin_State_Exception { |
|
| 28 | 28 | $message = \sprintf( |
| 29 | 29 | 'Failed to construct %s using the DI Container. %s', |
| 30 | - is_string( $event ) ? $event : get_class( $event ), |
|
| 30 | + is_string($event) ? $event : get_class($event), |
|
| 31 | 31 | $exception ? $exception->getMessage() : '' |
| 32 | 32 | ); |
| 33 | - return new Plugin_State_Exception( $message, 101 ); |
|
| 33 | + return new Plugin_State_Exception($message, 101); |
|
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | /** |
@@ -39,12 +39,12 @@ discard block |
||
| 39 | 39 | * @param string|object $event |
| 40 | 40 | * @return Plugin_State_Exception |
| 41 | 41 | */ |
| 42 | - public static function invalid_state_change_event_type( $event ): Plugin_State_Exception { |
|
| 42 | + public static function invalid_state_change_event_type($event): Plugin_State_Exception { |
|
| 43 | 43 | $message = \sprintf( |
| 44 | 44 | '%s is not a valid Plugin State Change Event class', |
| 45 | - is_string( $event ) ? $event : get_class( $event ) |
|
| 45 | + is_string($event) ? $event : get_class($event) |
|
| 46 | 46 | ); |
| 47 | - return new Plugin_State_Exception( $message, 102 ); |
|
| 47 | + return new Plugin_State_Exception($message, 102); |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | /** |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | * @return Plugin_State_Exception |
| 54 | 54 | */ |
| 55 | 55 | public static function failed_to_locate_calling_file(): Plugin_State_Exception { |
| 56 | - return new Plugin_State_Exception( 'Could not locate the file which created the Plugin State Controller. Please define this value manually using $controller->finalise( string $file )', 103 ); |
|
| 56 | + return new Plugin_State_Exception('Could not locate the file which created the Plugin State Controller. Please define this value manually using $controller->finalise( string $file )', 103); |
|
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | /** |
@@ -63,12 +63,12 @@ discard block |
||
| 63 | 63 | * @param Throwable $exception |
| 64 | 64 | * @return Plugin_State_Exception |
| 65 | 65 | */ |
| 66 | - public static function error_running_state_change_event( Plugin_State_Change $event, ?Throwable $exception = null ): Plugin_State_Exception { |
|
| 66 | + public static function error_running_state_change_event(Plugin_State_Change $event, ?Throwable $exception = null): Plugin_State_Exception { |
|
| 67 | 67 | $message = \sprintf( |
| 68 | 68 | 'Failed to run %s->run(), error thrown::%s', |
| 69 | - get_class( $event ), |
|
| 69 | + get_class($event), |
|
| 70 | 70 | $exception === null ? 'NO ERROR PASSED' : $exception->getMessage() |
| 71 | 71 | ); |
| 72 | - return new Plugin_State_Exception( $message, 104 ); |
|
| 72 | + return new Plugin_State_Exception($message, 104); |
|
| 73 | 73 | } |
| 74 | 74 | } |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | protected $events; |
| 26 | 26 | |
| 27 | 27 | /** @param Plugin_State_Change ...$event */ |
| 28 | - public function __construct( Plugin_State_Change ...$event ) { |
|
| 28 | + public function __construct(Plugin_State_Change ...$event) { |
|
| 29 | 29 | $this->events = $event; |
| 30 | 30 | } |
| 31 | 31 | |
@@ -35,13 +35,13 @@ discard block |
||
| 35 | 35 | * @return void |
| 36 | 36 | */ |
| 37 | 37 | public function __invoke() { |
| 38 | - foreach ( $this->events as $event ) { |
|
| 38 | + foreach ($this->events as $event) { |
|
| 39 | 39 | try { |
| 40 | 40 | $event->run(); |
| 41 | - } catch ( \Throwable $th ) { |
|
| 41 | + } catch (\Throwable $th) { |
|
| 42 | 42 | // If caught on Activation, throw Plugin_State_Exception |
| 43 | - if ( is_a( $event, Activation::class ) ) { |
|
| 44 | - throw Plugin_State_Exception::error_running_state_change_event( $event, $th ); |
|
| 43 | + if (is_a($event, Activation::class)) { |
|
| 44 | + throw Plugin_State_Exception::error_running_state_change_event($event, $th); |
|
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | continue; |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | */ |
| 48 | 48 | protected $plugin_base_file = null; |
| 49 | 49 | |
| 50 | - public function __construct( App $app, ?string $plugin_base_file = null ) { |
|
| 50 | + public function __construct(App $app, ?string $plugin_base_file = null) { |
|
| 51 | 51 | $this->app = $app; |
| 52 | 52 | $this->plugin_base_file = $plugin_base_file; |
| 53 | 53 | } |
@@ -58,8 +58,8 @@ discard block |
||
| 58 | 58 | * @param App $app |
| 59 | 59 | * @return self |
| 60 | 60 | */ |
| 61 | - public static function init( App $app, ?string $plugin_base_file = null ): self { |
|
| 62 | - $instance = new self( $app, $plugin_base_file ); |
|
| 61 | + public static function init(App $app, ?string $plugin_base_file = null): self { |
|
| 62 | + $instance = new self($app, $plugin_base_file); |
|
| 63 | 63 | return $instance; |
| 64 | 64 | } |
| 65 | 65 | |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | * @param string $plugin_base_file Holds the location of the plugin base file. |
| 79 | 79 | * @return self |
| 80 | 80 | */ |
| 81 | - public function set_plugin_base_file( string $plugin_base_file ): self { |
|
| 81 | + public function set_plugin_base_file(string $plugin_base_file): self { |
|
| 82 | 82 | $this->plugin_base_file = $plugin_base_file; |
| 83 | 83 | return $this; |
| 84 | 84 | } |
@@ -90,25 +90,25 @@ discard block |
||
| 90 | 90 | * @return self |
| 91 | 91 | * @throws Plugin_State_Exception If none Plugin_State_Change (string or object) passed or fails to create instance from valid class name. |
| 92 | 92 | */ |
| 93 | - public function event( $state_event ): self { |
|
| 93 | + public function event($state_event): self { |
|
| 94 | 94 | /* @phpstan-ignore-next-line, as this cannot be type hinted the check exists. */ |
| 95 | - if ( ! is_subclass_of( $state_event, Plugin_State_Change::class ) ) { |
|
| 96 | - throw Plugin_State_Exception::invalid_state_change_event_type( $state_event ); |
|
| 95 | + if ( ! is_subclass_of($state_event, Plugin_State_Change::class)) { |
|
| 96 | + throw Plugin_State_Exception::invalid_state_change_event_type($state_event); |
|
| 97 | 97 | } |
| 98 | 98 | // If its a string, attempt to create via DI container. |
| 99 | - if ( is_string( $state_event ) ) { |
|
| 99 | + if (is_string($state_event)) { |
|
| 100 | 100 | $state_event_string = $state_event; |
| 101 | 101 | |
| 102 | 102 | try { |
| 103 | 103 | /** @var Plugin_State_Change|null */ |
| 104 | - $state_event = $this->app->get_container()->create( $state_event ); |
|
| 105 | - } catch ( \Throwable $th ) { |
|
| 106 | - throw Plugin_State_Exception::failed_to_create_state_change_event( $state_event_string ); |
|
| 104 | + $state_event = $this->app->get_container()->create($state_event); |
|
| 105 | + } catch (\Throwable $th) { |
|
| 106 | + throw Plugin_State_Exception::failed_to_create_state_change_event($state_event_string); |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | // Throw exception if failed to create |
| 110 | - if ( null === $state_event || ! is_a( $state_event, Plugin_State_Change::class ) ) { |
|
| 111 | - throw Plugin_State_Exception::failed_to_create_state_change_event( $state_event_string ); |
|
| 110 | + if (null === $state_event || ! is_a($state_event, Plugin_State_Change::class)) { |
|
| 111 | + throw Plugin_State_Exception::failed_to_create_state_change_event($state_event_string); |
|
| 112 | 112 | } |
| 113 | 113 | } |
| 114 | 114 | $this->state_events[] = $state_event; |
@@ -122,35 +122,35 @@ discard block |
||
| 122 | 122 | * @return self |
| 123 | 123 | * @throws Plugin_State_Exception [103] failed_to_locate_calling_file() |
| 124 | 124 | */ |
| 125 | - public function finalise( ?string $file = null ): self { |
|
| 126 | - if ( null === $file ) { |
|
| 125 | + public function finalise(?string $file = null): self { |
|
| 126 | + if (null === $file) { |
|
| 127 | 127 | $file = $this->plugin_base_file ?? $this->get_called_file(); |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | // Activation hooks if need adding. |
| 131 | - if ( $this->has_events_for_state( Activation::class ) ) { |
|
| 132 | - register_activation_hook( $file, $this->activation() ); |
|
| 131 | + if ($this->has_events_for_state(Activation::class)) { |
|
| 132 | + register_activation_hook($file, $this->activation()); |
|
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | // Deactivation hooks. |
| 136 | - if ( $this->has_events_for_state( Deactivation::class ) ) { |
|
| 137 | - register_deactivation_hook( $file, $this->deactivation() ); |
|
| 136 | + if ($this->has_events_for_state(Deactivation::class)) { |
|
| 137 | + register_deactivation_hook($file, $this->deactivation()); |
|
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | // If we have an uninstall events, add then during activation. |
| 141 | - if ( $this->has_events_for_state( Uninstall::class ) ) { |
|
| 141 | + if ($this->has_events_for_state(Uninstall::class)) { |
|
| 142 | 142 | $callback = $this->uninstall(); |
| 143 | 143 | |
| 144 | 144 | // Register the callback so itsits included (but wont run due to serialization issues). |
| 145 | 145 | register_activation_hook( |
| 146 | 146 | $file, |
| 147 | - static function() use ( $file, $callback ): void { |
|
| 148 | - register_uninstall_hook( $file, $callback ); |
|
| 147 | + static function() use ($file, $callback): void { |
|
| 148 | + register_uninstall_hook($file, $callback); |
|
| 149 | 149 | } |
| 150 | 150 | ); |
| 151 | 151 | |
| 152 | 152 | // Manually re-add the uninstall hook. |
| 153 | - add_action( 'uninstall_' . plugin_basename( $file ), $callback ); |
|
| 153 | + add_action('uninstall_'.plugin_basename($file), $callback); |
|
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | return $this; |
@@ -162,12 +162,12 @@ discard block |
||
| 162 | 162 | * @param string $state |
| 163 | 163 | * @return Plugin_State_Change[] |
| 164 | 164 | */ |
| 165 | - private function get_events_for_state( string $state ): array { |
|
| 165 | + private function get_events_for_state(string $state): array { |
|
| 166 | 166 | return array_filter( |
| 167 | 167 | $this->state_events, |
| 168 | - function( $e ) use ( $state ): bool { |
|
| 168 | + function($e) use ($state): bool { |
|
| 169 | 169 | /* @phpstan-ignore-next-line */ |
| 170 | - return is_subclass_of( $e, $state ); |
|
| 170 | + return is_subclass_of($e, $state); |
|
| 171 | 171 | } |
| 172 | 172 | ); |
| 173 | 173 | } |
@@ -178,8 +178,8 @@ discard block |
||
| 178 | 178 | * @param string $state |
| 179 | 179 | * @return bool |
| 180 | 180 | */ |
| 181 | - private function has_events_for_state( string $state ): bool { |
|
| 182 | - return count( $this->get_events_for_state( $state ) ) !== 0; |
|
| 181 | + private function has_events_for_state(string $state): bool { |
|
| 182 | + return count($this->get_events_for_state($state)) !== 0; |
|
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | /** |
@@ -188,7 +188,7 @@ discard block |
||
| 188 | 188 | * @return State_Change_Queue |
| 189 | 189 | */ |
| 190 | 190 | public function activation(): State_Change_Queue { |
| 191 | - return new State_Change_Queue( ...$this->get_events_for_state( Activation::class ) ); |
|
| 191 | + return new State_Change_Queue(...$this->get_events_for_state(Activation::class)); |
|
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | /** |
@@ -197,7 +197,7 @@ discard block |
||
| 197 | 197 | * @return State_Change_Queue |
| 198 | 198 | */ |
| 199 | 199 | public function deactivation(): State_Change_Queue { |
| 200 | - return new State_Change_Queue( ...$this->get_events_for_state( Deactivation::class ) ); |
|
| 200 | + return new State_Change_Queue(...$this->get_events_for_state(Deactivation::class)); |
|
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | /** |
@@ -206,7 +206,7 @@ discard block |
||
| 206 | 206 | * @return State_Change_Queue |
| 207 | 207 | */ |
| 208 | 208 | public function uninstall(): State_Change_Queue { |
| 209 | - return new State_Change_Queue( ...$this->get_events_for_state( Uninstall::class ) ); |
|
| 209 | + return new State_Change_Queue(...$this->get_events_for_state(Uninstall::class)); |
|
| 210 | 210 | } |
| 211 | 211 | |
| 212 | 212 | /** |
@@ -218,16 +218,16 @@ discard block |
||
| 218 | 218 | protected function get_called_file(): string { |
| 219 | 219 | $backtrace = debug_backtrace(); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace |
| 220 | 220 | |
| 221 | - $backtrace_count = count( $backtrace ); |
|
| 222 | - for ( $i = 0; $i < $backtrace_count; $i++ ) { |
|
| 223 | - if ( $backtrace[ $i ]['function'] === __FUNCTION__ |
|
| 224 | - && \array_key_exists( 'class', $backtrace[ $i ] ) |
|
| 225 | - && $backtrace[ $i ]['class'] === get_class() |
|
| 226 | - && \array_key_exists( ( $i + 1 ), $backtrace ) |
|
| 227 | - && \array_key_exists( 'file', $backtrace[ $i + 1 ] ) |
|
| 221 | + $backtrace_count = count($backtrace); |
|
| 222 | + for ($i = 0; $i < $backtrace_count; $i++) { |
|
| 223 | + if ($backtrace[$i]['function'] === __FUNCTION__ |
|
| 224 | + && \array_key_exists('class', $backtrace[$i]) |
|
| 225 | + && $backtrace[$i]['class'] === get_class() |
|
| 226 | + && \array_key_exists(($i + 1), $backtrace) |
|
| 227 | + && \array_key_exists('file', $backtrace[$i + 1]) |
|
| 228 | 228 | |
| 229 | 229 | ) { |
| 230 | - return $backtrace[ $i + 1 ]['file']; |
|
| 230 | + return $backtrace[$i + 1]['file']; |
|
| 231 | 231 | } |
| 232 | 232 | } |
| 233 | 233 | // @codeCoverageIgnoreStart |