File tree Expand file tree Collapse file tree 2 files changed +13
-2
lines changed Expand file tree Collapse file tree 2 files changed +13
-2
lines changed Original file line number Diff line number Diff line change @@ -87,7 +87,7 @@ public function getSubMatchAt($index)
8787 */
8888 public function getSubMatchOrDefaultAt ($ index , $ default = null )
8989 {
90- return isset ($ this ->element [$ index ][ 0 ] ) ? $ this ->element [$ index ][0 ] : $ default ;
90+ return isset ($ this ->element [$ index ]) ? $ this ->element [$ index ][0 ] : $ default ;
9191 }
9292
9393 /**
@@ -113,7 +113,9 @@ public function getSubmatchOffsetAt($index)
113113 private function validateIndex ($ index )
114114 {
115115 if (! isset ($ this ->element [$ index ]) || $ index == 0 ) {
116- throw new \OutOfBoundsException ('Trying to access invalid submatch index ' );
116+ throw new \OutOfBoundsException (
117+ 'Trying to access invalid submatch index ' . $ index . 'in match: ' . $ this ->getFullMatch ()
118+ );
117119 }
118120 }
119121}
Original file line number Diff line number Diff line change @@ -73,6 +73,15 @@ public function shouldReturnTheInnerMatch()
7373 $ this ->assertEquals ('456 ' , $ this ->result ->getSubMatchAt (2 ));
7474 }
7575
76+ /**
77+ * @test
78+ */
79+ public function shouldReturnTheDefaultIfInnerMatchIsNotSet ()
80+ {
81+ $ this ->assertEquals ('potato ' , $ this ->result ->getSubMatchOrDefaultAt (3 , 'potato ' ));
82+ $ this ->assertNull ($ this ->result ->getSubMatchOrDefaultAt (4 ));
83+ }
84+
7685 /**
7786 * @test
7887 * @expectedException \OutOfBoundsException
You can’t perform that action at this time.
0 commit comments