@@ -34,9 +34,9 @@ class MagicQuery
3434 private $ enableTwig = false ;
3535
3636 /**
37- * @param \Doctrine\DBAL\Connection $connection
37+ * @param \Doctrine\DBAL\Connection $connection
3838 * @param \Doctrine\Common\Cache\Cache $cache
39- * @param SchemaAnalyzer $schemaAnalyzer (optional). If not set, it is initialized from the connection.
39+ * @param SchemaAnalyzer $schemaAnalyzer (optional). If not set, it is initialized from the connection.
4040 */
4141 public function __construct ($ connection = null , $ cache = null , SchemaAnalyzer $ schemaAnalyzer = null )
4242 {
@@ -54,11 +54,15 @@ public function __construct($connection = null, $cache = null, SchemaAnalyzer $s
5454 /**
5555 * Whether Twig parsing should be enabled or not.
5656 * Defaults to false.
57+ *
5758 * @param bool $enableTwig
59+ *
5860 * @return $this
5961 */
60- public function setEnableTwig ($ enableTwig = true ) {
62+ public function setEnableTwig ($ enableTwig = true )
63+ {
6164 $ this ->enableTwig = $ enableTwig ;
65+
6266 return $ this ;
6367 }
6468
@@ -79,6 +83,7 @@ public function build($sql, array $parameters = array())
7983 $ sql = $ this ->getTwigEnvironment ()->render ($ sql , $ parameters );
8084 }
8185 $ select = $ this ->parse ($ sql );
86+
8287 return $ this ->toSql ($ select , $ parameters );
8388 }
8489
@@ -87,13 +92,16 @@ public function build($sql, array $parameters = array())
8792 * This tree representation can be used to manipulate the SQL.
8893 *
8994 * @param string $sql
95+ *
9096 * @return NodeInterface
97+ *
9198 * @throws MagicQueryMissingConnectionException
9299 * @throws MagicQueryParserException
93100 */
94- public function parse ($ sql ) {
101+ public function parse ($ sql )
102+ {
95103 // We choose md4 because it is fast.
96- $ cacheKey = " request_ " .hash (" md4 " , $ sql );
104+ $ cacheKey = ' request_ ' .hash (' md4 ' , $ sql );
97105 $ select = $ this ->cache ->fetch ($ cacheKey );
98106
99107 if ($ select === false ) {
@@ -111,27 +119,32 @@ public function parse($sql) {
111119 // Let's store the tree
112120 $ this ->cache ->save ($ cacheKey , $ select );
113121 }
122+
114123 return $ select ;
115124 }
116125
117126 /**
118127 * Transforms back a tree of SQL node into a SQL string.
119128 *
120129 * @param NodeInterface $sqlNode
121- * @param array $parameters
130+ * @param array $parameters
131+ *
122132 * @return string
123133 */
124- public function toSql (NodeInterface $ sqlNode , array $ parameters = array ()) {
134+ public function toSql (NodeInterface $ sqlNode , array $ parameters = array ())
135+ {
125136 return $ sqlNode ->toSql ($ parameters , $ this ->connection , 0 , SqlRenderInterface::CONDITION_GUESS );
126137 }
127138
128139 /**
129140 * Scans the SQL statement and replaces the "magicjoin" part with the correct joins.
130141 *
131142 * @param NodeInterface $select
143+ *
132144 * @throws MagicQueryMissingConnectionException
133145 */
134- private function magicJoin (NodeInterface $ select ) {
146+ private function magicJoin (NodeInterface $ select )
147+ {
135148 // Let's find if this is a MagicJoin query.
136149 $ magicJoinDetector = new DetectMagicJoinSelectVisitor ();
137150 $ nodeTraverser = new NodeTraverser ();
@@ -147,11 +160,14 @@ private function magicJoin(NodeInterface $select) {
147160 }
148161
149162 /**
150- * For one given MagicJoin select, let's apply MagicJoin
163+ * For one given MagicJoin select, let's apply MagicJoin.
164+ *
151165 * @param MagicJoinSelect $magicJoinSelect
166+ *
152167 * @return Select
153168 */
154- private function magicJoinOnOneQuery (MagicJoinSelect $ magicJoinSelect ) {
169+ private function magicJoinOnOneQuery (MagicJoinSelect $ magicJoinSelect )
170+ {
155171 $ tableSearchNodeTraverser = new NodeTraverser ();
156172 $ detectTableVisitor = new DetectTablesVisitor ($ magicJoinSelect ->getMainTable ());
157173 $ tableSearchNodeTraverser ->addVisitor ($ detectTableVisitor );
@@ -183,7 +199,7 @@ private function magicJoinOnOneQuery(MagicJoinSelect $magicJoinSelect) {
183199 $ tableNode = new Table ();
184200 $ tableNode ->setTable ($ mainTable );
185201 $ tables = [
186- $ mainTable => $ tableNode
202+ $ mainTable => $ tableNode,
187203 ];
188204
189205 foreach ($ completePath as $ foreignKey ) {
@@ -202,7 +218,7 @@ private function magicJoinOnOneQuery(MagicJoinSelect $magicJoinSelect) {
202218 $ onNode ->setRightOperand ($ rightCol );
203219
204220 $ tableNode = new Table ();
205- $ tableNode ->setJoinType (" LEFT JOIN " );
221+ $ tableNode ->setJoinType (' LEFT JOIN ' );
206222 $ tableNode ->setRefClause ($ onNode );
207223
208224 if (isset ($ tables [$ foreignKey ->getLocalTableName ()])) {
@@ -215,34 +231,38 @@ private function magicJoinOnOneQuery(MagicJoinSelect $magicJoinSelect) {
215231 }
216232
217233 $ select ->setFrom ($ tables );
218-
219234 }
220235
221236 /**
222237 * @return SchemaAnalyzer
223238 */
224- private function getSchemaAnalyzer () {
239+ private function getSchemaAnalyzer ()
240+ {
225241 if ($ this ->schemaAnalyzer === null ) {
226242 if (!$ this ->connection ) {
227243 throw new MagicQueryMissingConnectionException ('In order to use MagicJoin, you need to configure a DBAL connection. ' );
228244 }
229245
230246 $ this ->schemaAnalyzer = new SchemaAnalyzer ($ this ->connection ->getSchemaManager (), $ this ->cache , $ this ->getConnectionUniqueId ());
231247 }
248+
232249 return $ this ->schemaAnalyzer ;
233250 }
234251
235- private function getConnectionUniqueId () {
236- return hash ('md4 ' , $ this ->connection ->getHost ()."- " .$ this ->connection ->getPort ()."- " .$ this ->connection ->getDatabase ()."- " .$ this ->connection ->getDriver ()->getName ());
252+ private function getConnectionUniqueId ()
253+ {
254+ return hash ('md4 ' , $ this ->connection ->getHost ().'- ' .$ this ->connection ->getPort ().'- ' .$ this ->connection ->getDatabase ().'- ' .$ this ->connection ->getDriver ()->getName ());
237255 }
238256
239257 /**
240258 * @return \Twig_Environment
241259 */
242- private function getTwigEnvironment () {
260+ private function getTwigEnvironment ()
261+ {
243262 if ($ this ->twigEnvironment === null ) {
244263 $ this ->twigEnvironment = SqlTwigEnvironmentFactory::getTwigEnvironment ();
245264 }
265+
246266 return $ this ->twigEnvironment ;
247267 }
248268}
0 commit comments