22
33namespace Aternos \Licensee \License ;
44
5+ use Aternos \Licensee \Exception \RegExpException ;
56use Aternos \Licensee \Generated \Condition ;
67use Aternos \Licensee \Generated \Limitation ;
78use Aternos \Licensee \Generated \Permission ;
1112use DOMDocument ;
1213use DOMNode ;
1314use InvalidArgumentException ;
15+ use RuntimeException ;
1416
1517class License
1618{
@@ -98,7 +100,11 @@ public function __construct(
98100 protected string $ key
99101 )
100102 {
101- $ this ->parseLicenseFile (file_get_contents (static ::LICENSE_DIR . $ key . ".txt " ));
103+ try {
104+ $ this ->parseLicenseFile (file_get_contents (static ::LICENSE_DIR . $ key . ".txt " ));
105+ } catch (RegExpException $ e ) {
106+ throw new RuntimeException ("Built-in license file \"" . $ key . "\" could not be loaded. " , previous: $ e );
107+ }
102108 }
103109
104110 /**
@@ -112,6 +118,7 @@ protected function readSpdxXmlContent(): string
112118 /**
113119 * @param string $content
114120 * @return void
121+ * @throws RegExpException
115122 */
116123 protected function parseLicenseFile (string $ content ): void
117124 {
@@ -120,7 +127,7 @@ protected function parseLicenseFile(string $content): void
120127 throw new InvalidArgumentException ("Invalid license format " );
121128 }
122129
123- $ parts = preg_split ('/^---\s*$/m ' , $ content , 3 );
130+ $ parts = RegExpException:: handleFalse ( preg_split ('/^---\s*$/m ' , $ content , 3 ) );
124131 if (count ($ parts ) !== 3 ) {
125132 throw new InvalidArgumentException ("Invalid license format " );
126133 }
@@ -158,6 +165,7 @@ protected function parseLicenseFile(string $content): void
158165 /**
159166 * I used the regex to regex the regex
160167 * @return string
168+ * @throws RegExpException
161169 */
162170 public function getTitleRegex (): string
163171 {
@@ -168,29 +176,29 @@ public function getTitleRegex(): string
168176 $ simpleTitleRegex = strtolower ($ this ->title );
169177 $ simpleTitleRegex = str_replace ('* ' , 'u ' , $ simpleTitleRegex );
170178 $ simpleTitleRegex = preg_quote ($ simpleTitleRegex , '/ ' );
171- $ titleRegex = preg_replace ('/^the /i ' , '' , $ simpleTitleRegex );
172- $ titleRegex = preg_replace ('/,? version / ' , ' ' , $ titleRegex );
173- $ titleRegex = preg_replace ('/v(\d+\.\d+)/ ' , '$1 ' , $ titleRegex );
174- $ titleRegex = preg_quote ($ titleRegex , '/ ' );
175- $ titleRegex = preg_replace ('/ \\\ licen[sc]e/i ' , '(?:\ licen[sc]e)? ' , $ titleRegex );
176- preg_match ('/\d+ \\\+\.(\d+)/ ' , $ titleRegex , $ versionMatch );
179+ $ titleRegex = RegExpException:: handleNull ( preg_replace ('/^the /i ' , '' , $ simpleTitleRegex) );
180+ $ titleRegex = RegExpException:: handleNull ( preg_replace ('/,? version / ' , ' ' , $ titleRegex) );
181+ $ titleRegex = RegExpException:: handleNull ( preg_replace ('/v(\d+\.\d+)/ ' , '$1 ' , $ titleRegex) );
182+ $ titleRegex = RegExpException:: handleNull ( preg_quote ($ titleRegex , '/ ' ) );
183+ $ titleRegex = RegExpException:: handleNull ( preg_replace ('/ \\\ licen[sc]e/i ' , '(?:\ licen[sc]e)? ' , $ titleRegex) );
184+ RegExpException:: handleFalse ( preg_match ('/\d+ \\\+\.(\d+)/ ' , $ titleRegex , $ versionMatch) );
177185 if ($ versionMatch ) {
178186 if ($ versionMatch [1 ] === '0 ' ) {
179187 $ sub = ',?\s+(?:version\ |v(?:\. )?)?$1($2)? ' ;
180188 } else {
181189 $ sub = ',?\s+(?:version\ |v(?:\. )?)?$1$2 ' ;
182190 }
183- $ titleRegex = preg_replace ('/\s*(\d+) \\\+(\.\d+)/ ' , $ sub , $ titleRegex );
191+ $ titleRegex = RegExpException:: handleNull ( preg_replace ('/\s*(\d+) \\\+(\.\d+)/ ' , $ sub , $ titleRegex) );
184192 }
185- $ titleRegex = preg_replace ('/\bgnu \\\ /i ' , '(?:GNU )? ' , $ titleRegex );
193+ $ titleRegex = RegExpException:: handleNull ( preg_replace ('/\bgnu \\\ /i ' , '(?:GNU )? ' , $ titleRegex) );
186194
187- $ keyRegex = str_replace ('- ' , '[- ] ' , $ this ->getSpdxId ()->value );
188- $ keyRegex = str_replace ('. ' , '\. ' , $ keyRegex );
195+ $ keyRegex = RegExpException:: handleNull ( str_replace ('- ' , '[- ] ' , $ this ->getSpdxId ()->value ) );
196+ $ keyRegex = RegExpException:: handleNull ( str_replace ('. ' , '\. ' , $ keyRegex) );
189197 $ keyRegex .= '(?:\ licen[sc]e)? ' ;
190198
191199 $ parts = [$ simpleTitleRegex , $ titleRegex , $ keyRegex ];
192200 if ($ this ->nickname ) {
193- $ parts [] = preg_replace ('/\bGNU /i ' , '(?:GNU )? ' , preg_quote ($ this ->nickname , '/ ' ));
201+ $ parts [] = RegExpException:: handleNull ( preg_replace ('/\bGNU /i ' , '(?:GNU )? ' , preg_quote ($ this ->nickname , '/ ' ) ));
194202 }
195203
196204 return $ this ->titleRegexp = implode ('| ' , $ parts );
0 commit comments