@@ -57,56 +57,101 @@ public function stream_stat():array {
5757 * This function checks for a namespace declaration at the top of the
5858 * script, and if there is no declaration, it will inject one that
5959 * matches the current path.
60+ *
61+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
6062 */
6163 private function loadContents (SplFileObject $ file ):void {
64+ $ this ->processFileUntilNamespace ($ file );
65+ $ this ->appendRemainingContents ($ file );
66+ }
67+
68+ /**
69+ * Process the file until a namespace is found or added
70+ */
71+ private function processFileUntilNamespace (SplFileObject $ file ): void {
6272 $ foundNamespace = false ;
6373 $ withinBlockComment = false ;
6474 $ lineNumber = 0 ;
6575
6676 while (!$ file ->eof () && !$ foundNamespace ) {
6777 $ line = $ file ->fgets ();
78+
6879 if ($ lineNumber === 0 ) {
69- // TODO: Allow hashbangs before <?php
70- // Maybe this is possible by just skipping while the first character is a hash,
71- // and not increasing the line number.
72- if (!str_starts_with ($ line , "<?php " )) {
73- throw new Exception (
74- "Logic file at "
75- . $ this ->path
76- . " must start by opening a PHP tag. "
77- . "See https://www.php.gt/routing/logic-stream-wrapper "
78- );
79- }
80+ $ this ->validateFirstLine ($ line );
8081 }
81- $ trimmedLine = trim ($ line );
8282
83- if (str_starts_with ($ trimmedLine , "/* " )) {
84- $ withinBlockComment = true ;
85- }
83+ $ trimmedLine = trim ($ line );
84+ $ withinBlockComment = $ this ->handleBlockComment ($ trimmedLine , $ withinBlockComment );
8685
87- if ($ withinBlockComment ) {
88- if (str_contains ($ trimmedLine , "*/ " )) {
89- $ withinBlockComment = false ;
90- }
91- }
92- elseif ($ lineNumber > 0 ) {
93- if (str_starts_with ($ trimmedLine , "namespace " )) {
94- $ foundNamespace = true ;
95- }
96- elseif ($ trimmedLine ) {
97- $ namespace = new LogicStreamNamespace (
98- $ this ->path ,
99- self ::NAMESPACE_PREFIX
100- );
101- $ this ->contents .= "namespace $ namespace; \t" ;
102- $ foundNamespace = true ;
103- }
86+ if (!$ withinBlockComment && $ lineNumber > 0 ) {
87+ $ foundNamespace = $ this ->processNamespace ($ trimmedLine );
10488 }
10589
10690 $ this ->contents .= $ line ;
10791 $ lineNumber ++;
10892 }
93+ }
10994
95+ /**
96+ * Validate that the first line of the file starts with <?php
97+ */
98+ private function validateFirstLine (string $ line ): void {
99+ if (!str_starts_with ($ line , "<?php " )) {
100+ throw new Exception (
101+ "Logic file at "
102+ . $ this ->path
103+ . " must start by opening a PHP tag. "
104+ . "See https://www.php.gt/routing/logic-stream-wrapper "
105+ );
106+ }
107+ }
108+
109+ /**
110+ * Handle block comments in the file
111+ *
112+ * @param string $trimmedLine The trimmed line to check
113+ * @param bool $withinBlockComment Whether we're currently within a block comment
114+ * @return bool Updated block comment status
115+ */
116+ private function handleBlockComment (string $ trimmedLine , bool $ withinBlockComment ): bool {
117+ if (str_starts_with ($ trimmedLine , "/* " )) {
118+ $ withinBlockComment = true ;
119+ }
120+
121+ if ($ withinBlockComment && str_contains ($ trimmedLine , "*/ " )) {
122+ $ withinBlockComment = false ;
123+ }
124+
125+ return $ withinBlockComment ;
126+ }
127+
128+ /**
129+ * Process namespace declarations
130+ *
131+ * @param string $trimmedLine The trimmed line to check
132+ * @return bool Whether a namespace was found or added
133+ */
134+ private function processNamespace (string $ trimmedLine ): bool {
135+ if (str_starts_with ($ trimmedLine , "namespace " )) {
136+ return true ;
137+ }
138+
139+ if ($ trimmedLine ) {
140+ $ namespace = new LogicStreamNamespace (
141+ $ this ->path ,
142+ self ::NAMESPACE_PREFIX
143+ );
144+ $ this ->contents .= "namespace $ namespace; \t" ;
145+ return true ;
146+ }
147+
148+ return false ;
149+ }
150+
151+ /**
152+ * Append the remaining contents of the file
153+ */
154+ private function appendRemainingContents (SplFileObject $ file ): void {
110155 while (!$ file ->eof ()) {
111156 $ line = $ file ->fgets ();
112157 $ this ->contents .= $ line ;
0 commit comments