@@ -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,24 +90,24 @@ 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 { |
|
| 94 | - if ( ! is_subclass_of( $state_event, Plugin_State_Change::class ) ) { |
|
| 95 | - throw Plugin_State_Exception::invalid_state_change_event_type( $state_event ); |
|
| 93 | + public function event($state_event): self { |
|
| 94 | + if ( ! is_subclass_of($state_event, Plugin_State_Change::class)) { |
|
| 95 | + throw Plugin_State_Exception::invalid_state_change_event_type($state_event); |
|
| 96 | 96 | } |
| 97 | 97 | // If its a string, attempt to create via DI container. |
| 98 | - if ( is_string( $state_event ) ) { |
|
| 98 | + if (is_string($state_event)) { |
|
| 99 | 99 | $state_event_string = $state_event; |
| 100 | 100 | |
| 101 | 101 | try { |
| 102 | 102 | /** @var Plugin_State_Change|null */ |
| 103 | - $state_event = $this->app->get_container()->create( $state_event ); |
|
| 104 | - } catch ( \Throwable $th ) { |
|
| 105 | - throw Plugin_State_Exception::failed_to_create_state_change_event( $state_event_string ); |
|
| 103 | + $state_event = $this->app->get_container()->create($state_event); |
|
| 104 | + } catch (\Throwable $th) { |
|
| 105 | + throw Plugin_State_Exception::failed_to_create_state_change_event($state_event_string); |
|
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | // Throw exception if failed to create |
| 109 | - if ( null === $state_event || ! is_a( $state_event, Plugin_State_Change::class ) ) { |
|
| 110 | - throw Plugin_State_Exception::failed_to_create_state_change_event( $state_event_string ); |
|
| 109 | + if (null === $state_event || ! is_a($state_event, Plugin_State_Change::class)) { |
|
| 110 | + throw Plugin_State_Exception::failed_to_create_state_change_event($state_event_string); |
|
| 111 | 111 | } |
| 112 | 112 | } |
| 113 | 113 | $this->state_events[] = $state_event; |
@@ -121,35 +121,35 @@ discard block |
||
| 121 | 121 | * @return self |
| 122 | 122 | * @throws Plugin_State_Exception [103] failed_to_locate_calling_file() |
| 123 | 123 | */ |
| 124 | - public function finalise( ?string $file = null ): self { |
|
| 125 | - if ( null === $file ) { |
|
| 124 | + public function finalise(?string $file = null): self { |
|
| 125 | + if (null === $file) { |
|
| 126 | 126 | $file = $this->plugin_base_file ?? $this->get_called_file(); |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | // Activation hooks if need adding. |
| 130 | - if ( $this->has_events_for_state( Activation::class ) ) { |
|
| 131 | - register_activation_hook( $file, $this->activation() ); |
|
| 130 | + if ($this->has_events_for_state(Activation::class)) { |
|
| 131 | + register_activation_hook($file, $this->activation()); |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | // Deactivation hooks. |
| 135 | - if ( $this->has_events_for_state( Deactivation::class ) ) { |
|
| 136 | - register_deactivation_hook( $file, $this->deactivation() ); |
|
| 135 | + if ($this->has_events_for_state(Deactivation::class)) { |
|
| 136 | + register_deactivation_hook($file, $this->deactivation()); |
|
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | // If we have an uninstall events, add then during activation. |
| 140 | - if ( $this->has_events_for_state( Uninstall::class ) ) { |
|
| 140 | + if ($this->has_events_for_state(Uninstall::class)) { |
|
| 141 | 141 | $callback = $this->uninstall(); |
| 142 | 142 | |
| 143 | 143 | // Register the callback so itsits included (but wont run due to serialization issues). |
| 144 | 144 | register_activation_hook( |
| 145 | 145 | $file, |
| 146 | - static function() use ( $file, $callback ): void { |
|
| 147 | - register_uninstall_hook( $file, $callback ); |
|
| 146 | + static function() use ($file, $callback): void { |
|
| 147 | + register_uninstall_hook($file, $callback); |
|
| 148 | 148 | } |
| 149 | 149 | ); |
| 150 | 150 | |
| 151 | 151 | // Manually re-add the uninstall hook. |
| 152 | - add_action( 'uninstall_' . plugin_basename( $file ), $callback ); |
|
| 152 | + add_action('uninstall_'.plugin_basename($file), $callback); |
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | return $this; |
@@ -161,11 +161,11 @@ discard block |
||
| 161 | 161 | * @param string $state |
| 162 | 162 | * @return Plugin_State_Change[] |
| 163 | 163 | */ |
| 164 | - private function get_events_for_state( string $state ): array { |
|
| 164 | + private function get_events_for_state(string $state): array { |
|
| 165 | 165 | return array_filter( |
| 166 | 166 | $this->state_events, |
| 167 | - function( $e ) use ( $state ): bool { |
|
| 168 | - return is_subclass_of( $e, $state ); |
|
| 167 | + function($e) use ($state): bool { |
|
| 168 | + return is_subclass_of($e, $state); |
|
| 169 | 169 | } |
| 170 | 170 | ); |
| 171 | 171 | } |
@@ -176,8 +176,8 @@ discard block |
||
| 176 | 176 | * @param string $state |
| 177 | 177 | * @return bool |
| 178 | 178 | */ |
| 179 | - private function has_events_for_state( string $state ): bool { |
|
| 180 | - return count( $this->get_events_for_state( $state ) ) !== 0; |
|
| 179 | + private function has_events_for_state(string $state): bool { |
|
| 180 | + return count($this->get_events_for_state($state)) !== 0; |
|
| 181 | 181 | } |
| 182 | 182 | |
| 183 | 183 | /** |
@@ -186,7 +186,7 @@ discard block |
||
| 186 | 186 | * @return State_Change_Queue |
| 187 | 187 | */ |
| 188 | 188 | public function activation(): State_Change_Queue { |
| 189 | - return new State_Change_Queue( ...$this->get_events_for_state( Activation::class ) ); |
|
| 189 | + return new State_Change_Queue(...$this->get_events_for_state(Activation::class)); |
|
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | /** |
@@ -195,7 +195,7 @@ discard block |
||
| 195 | 195 | * @return State_Change_Queue |
| 196 | 196 | */ |
| 197 | 197 | public function deactivation(): State_Change_Queue { |
| 198 | - return new State_Change_Queue( ...$this->get_events_for_state( Deactivation::class ) ); |
|
| 198 | + return new State_Change_Queue(...$this->get_events_for_state(Deactivation::class)); |
|
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | /** |
@@ -204,7 +204,7 @@ discard block |
||
| 204 | 204 | * @return State_Change_Queue |
| 205 | 205 | */ |
| 206 | 206 | public function uninstall(): State_Change_Queue { |
| 207 | - return new State_Change_Queue( ...$this->get_events_for_state( Uninstall::class ) ); |
|
| 207 | + return new State_Change_Queue(...$this->get_events_for_state(Uninstall::class)); |
|
| 208 | 208 | } |
| 209 | 209 | |
| 210 | 210 | /** |
@@ -216,16 +216,16 @@ discard block |
||
| 216 | 216 | protected function get_called_file(): string { |
| 217 | 217 | $backtrace = debug_backtrace(); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace |
| 218 | 218 | |
| 219 | - $backtrace_count = count( $backtrace ); |
|
| 220 | - for ( $i = 0; $i < $backtrace_count; $i++ ) { |
|
| 221 | - if ( $backtrace[ $i ]['function'] === __FUNCTION__ |
|
| 222 | - && \array_key_exists( 'class', $backtrace[ $i ] ) |
|
| 223 | - && $backtrace[ $i ]['class'] === get_class() |
|
| 224 | - && \array_key_exists( ( $i + 1 ), $backtrace ) |
|
| 225 | - && \array_key_exists( 'file', $backtrace[ $i + 1 ] ) |
|
| 219 | + $backtrace_count = count($backtrace); |
|
| 220 | + for ($i = 0; $i < $backtrace_count; $i++) { |
|
| 221 | + if ($backtrace[$i]['function'] === __FUNCTION__ |
|
| 222 | + && \array_key_exists('class', $backtrace[$i]) |
|
| 223 | + && $backtrace[$i]['class'] === get_class() |
|
| 224 | + && \array_key_exists(($i + 1), $backtrace) |
|
| 225 | + && \array_key_exists('file', $backtrace[$i + 1]) |
|
| 226 | 226 | |
| 227 | 227 | ) { |
| 228 | - return $backtrace[ $i + 1 ]['file']; |
|
| 228 | + return $backtrace[$i + 1]['file']; |
|
| 229 | 229 | } |
| 230 | 230 | } |
| 231 | 231 | // @codeCoverageIgnoreStart |