@@ -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 | |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | * @param string $plugin_base_file Holds the location of the plugin base file. |
70 | 70 | * @return self |
71 | 71 | */ |
72 | - public function set_plugin_base_file( string $plugin_base_file ): self { |
|
72 | + public function set_plugin_base_file(string $plugin_base_file): self { |
|
73 | 73 | $this->plugin_base_file = $plugin_base_file; |
74 | 74 | return $this; |
75 | 75 | } |
@@ -81,24 +81,24 @@ discard block |
||
81 | 81 | * @return self |
82 | 82 | * @throws Plugin_State_Exception If none Plugin_State_Change (string or object) passed or fails to create instance from valid class name. |
83 | 83 | */ |
84 | - public function event( $state_event ): self { |
|
85 | - if ( ! is_subclass_of( $state_event, Plugin_State_Change::class ) ) { |
|
86 | - throw Plugin_State_Exception::invalid_state_change_event_type( $state_event ); |
|
84 | + public function event($state_event): self { |
|
85 | + if ( ! is_subclass_of($state_event, Plugin_State_Change::class)) { |
|
86 | + throw Plugin_State_Exception::invalid_state_change_event_type($state_event); |
|
87 | 87 | } |
88 | 88 | // If its a string, attempt to create via DI container. |
89 | - if ( is_string( $state_event ) ) { |
|
89 | + if (is_string($state_event)) { |
|
90 | 90 | $state_event_string = $state_event; |
91 | 91 | |
92 | 92 | try { |
93 | 93 | /** @var Plugin_State_Change|null */ |
94 | - $state_event = $this->app->get_container()->create( $state_event ); |
|
95 | - } catch ( \Throwable $th ) { |
|
96 | - throw Plugin_State_Exception::failed_to_create_state_change_event( $state_event_string ); |
|
94 | + $state_event = $this->app->get_container()->create($state_event); |
|
95 | + } catch (\Throwable $th) { |
|
96 | + throw Plugin_State_Exception::failed_to_create_state_change_event($state_event_string); |
|
97 | 97 | } |
98 | 98 | |
99 | 99 | // Throw exception if failed to create |
100 | - if ( null === $state_event || ! is_a( $state_event, Plugin_State_Change::class ) ) { |
|
101 | - throw Plugin_State_Exception::failed_to_create_state_change_event( $state_event_string ); |
|
100 | + if (null === $state_event || ! is_a($state_event, Plugin_State_Change::class)) { |
|
101 | + throw Plugin_State_Exception::failed_to_create_state_change_event($state_event_string); |
|
102 | 102 | } |
103 | 103 | } |
104 | 104 | $this->state_events[] = $state_event; |
@@ -112,28 +112,28 @@ discard block |
||
112 | 112 | * @return self |
113 | 113 | * @throws Plugin_State_Exception [103] failed_to_locate_calling_file() |
114 | 114 | */ |
115 | - public function finalise( ?string $file = null ): self { |
|
116 | - if ( null === $file ) { |
|
115 | + public function finalise(?string $file = null): self { |
|
116 | + if (null === $file) { |
|
117 | 117 | $file = $this->plugin_base_file ?? $this->get_called_file(); |
118 | 118 | } |
119 | 119 | |
120 | 120 | // Activation hooks if need adding. |
121 | - if ( $this->has_events_for_state( Activation::class ) ) { |
|
122 | - register_activation_hook( $file, $this->activation() ); |
|
121 | + if ($this->has_events_for_state(Activation::class)) { |
|
122 | + register_activation_hook($file, $this->activation()); |
|
123 | 123 | } |
124 | 124 | |
125 | 125 | // Deactivation hooks. |
126 | - if ( $this->has_events_for_state( Deactivation::class ) ) { |
|
127 | - register_deactivation_hook( $file, $this->deactivation() ); |
|
126 | + if ($this->has_events_for_state(Deactivation::class)) { |
|
127 | + register_deactivation_hook($file, $this->deactivation()); |
|
128 | 128 | } |
129 | 129 | |
130 | 130 | // If we have an uninstall events, add then during activation. |
131 | - if ( $this->has_events_for_state( Uninstall::class ) ) { |
|
131 | + if ($this->has_events_for_state(Uninstall::class)) { |
|
132 | 132 | $callback = $this->uninstall(); |
133 | 133 | register_activation_hook( |
134 | 134 | $file, |
135 | - static function() use ( $file, $callback ): void { |
|
136 | - register_uninstall_hook( $file, $callback ); |
|
135 | + static function() use ($file, $callback): void { |
|
136 | + register_uninstall_hook($file, $callback); |
|
137 | 137 | } |
138 | 138 | ); |
139 | 139 | } |
@@ -147,11 +147,11 @@ discard block |
||
147 | 147 | * @param string $state |
148 | 148 | * @return Plugin_State_Change[] |
149 | 149 | */ |
150 | - private function get_events_for_state( string $state ): array { |
|
150 | + private function get_events_for_state(string $state): array { |
|
151 | 151 | return array_filter( |
152 | 152 | $this->state_events, |
153 | - function( $e ) use ( $state ): bool { |
|
154 | - return is_subclass_of( $e, $state ); |
|
153 | + function($e) use ($state): bool { |
|
154 | + return is_subclass_of($e, $state); |
|
155 | 155 | } |
156 | 156 | ); |
157 | 157 | } |
@@ -162,8 +162,8 @@ discard block |
||
162 | 162 | * @param string $state |
163 | 163 | * @return bool |
164 | 164 | */ |
165 | - private function has_events_for_state( string $state ): bool { |
|
166 | - return count( $this->get_events_for_state( $state ) ) !== 0; |
|
165 | + private function has_events_for_state(string $state): bool { |
|
166 | + return count($this->get_events_for_state($state)) !== 0; |
|
167 | 167 | } |
168 | 168 | |
169 | 169 | /** |
@@ -172,7 +172,7 @@ discard block |
||
172 | 172 | * @return State_Change_Queue |
173 | 173 | */ |
174 | 174 | public function activation(): State_Change_Queue { |
175 | - return new State_Change_Queue( ...$this->get_events_for_state( Activation::class ) ); |
|
175 | + return new State_Change_Queue(...$this->get_events_for_state(Activation::class)); |
|
176 | 176 | } |
177 | 177 | |
178 | 178 | /** |
@@ -181,7 +181,7 @@ discard block |
||
181 | 181 | * @return State_Change_Queue |
182 | 182 | */ |
183 | 183 | public function deactivation(): State_Change_Queue { |
184 | - return new State_Change_Queue( ...$this->get_events_for_state( Deactivation::class ) ); |
|
184 | + return new State_Change_Queue(...$this->get_events_for_state(Deactivation::class)); |
|
185 | 185 | } |
186 | 186 | |
187 | 187 | /** |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | * @return State_Change_Queue |
191 | 191 | */ |
192 | 192 | public function uninstall(): State_Change_Queue { |
193 | - return new State_Change_Queue( ...$this->get_events_for_state( Uninstall::class ) ); |
|
193 | + return new State_Change_Queue(...$this->get_events_for_state(Uninstall::class)); |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | /** |
@@ -202,13 +202,13 @@ discard block |
||
202 | 202 | protected function get_called_file(): string { |
203 | 203 | $backtrace = debug_backtrace(); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace |
204 | 204 | |
205 | - $backtrace_count = count( $backtrace ); |
|
206 | - for ( $i = 0; $i < $backtrace_count; $i++ ) { |
|
207 | - if ( $backtrace[ $i ]['function'] === __FUNCTION__ |
|
208 | - && $backtrace[ $i ]['class'] === get_class() |
|
209 | - && \array_key_exists( ( $i + 1 ), $backtrace ) |
|
205 | + $backtrace_count = count($backtrace); |
|
206 | + for ($i = 0; $i < $backtrace_count; $i++) { |
|
207 | + if ($backtrace[$i]['function'] === __FUNCTION__ |
|
208 | + && $backtrace[$i]['class'] === get_class() |
|
209 | + && \array_key_exists(($i + 1), $backtrace) |
|
210 | 210 | ) { |
211 | - return $backtrace[ $i + 1 ]['file']; |
|
211 | + return $backtrace[$i + 1]['file']; |
|
212 | 212 | } |
213 | 213 | } |
214 | 214 | // @codeCoverageIgnoreStart |
@@ -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; |