Skip to content

Commit b74dc4c

Browse files
committed
refacto
1 parent cf62cbd commit b74dc4c

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

store.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -136,30 +136,19 @@ func (s *PHPStore) BestVersionForDir(dir string) (*Version, string, string, erro
136136
return s.fallbackVersion("")
137137
}
138138

139-
// bestVersion returns the latest patch version for the given major (X), minor (X.Y), or patch (X.Y.Z)
140-
// version can be 7 or 7.1 or 7.1.2
141-
// non-symlinked versions have priority
139+
// bestVersion returns the latest patch version for the given major (X),
140+
// minor (X.Y), or patch (X.Y.Z). version can be 7 or 7.1 or 7.1.2.
141+
// Non-symlinked versions have priority
142142
// If the asked version is a patch one (X.Y.Z) and is not available, the lookup
143-
// will fallback to the last path version for the minor version (X.Y).
143+
// will fallback to the last patch version for the minor version (X.Y).
144144
// There's no fallback to the major version because PHP is known to occasionally
145145
// break BC in minor versions, so we can't safely fall back.
146146
func (s *PHPStore) bestVersion(versionPrefix, source string) (*Version, string, string, error) {
147147
warning := ""
148148

149-
isPatchVersion := false
150-
pos := strings.LastIndexByte(versionPrefix, '.')
151-
if pos != strings.IndexByte(versionPrefix, '.') {
152-
if versionPrefix[pos+1:] == "99" {
153-
versionPrefix = versionPrefix[:pos]
154-
pos = strings.LastIndexByte(versionPrefix, '.')
155-
} else {
156-
isPatchVersion = true
157-
}
158-
}
159-
160149
// Check if versionPrefix is actually a patch version, if so first do an
161150
// exact match lookup and fallback to a minor version check
162-
if isPatchVersion {
151+
if pos := strings.LastIndexByte(versionPrefix, '.'); pos != strings.IndexByte(versionPrefix, '.') {
163152
// look for an exact match, the order does not matter here
164153
for _, v := range s.versions {
165154
if v.Version == versionPrefix {
@@ -169,7 +158,9 @@ func (s *PHPStore) bestVersion(versionPrefix, source string) (*Version, string,
169158

170159
// exact match not found, fallback to minor version check
171160
newVersionPrefix := versionPrefix[:pos]
172-
warning = fmt.Sprintf(`the current dir requires PHP %s (%s), but this version is not available: fallback to %s`, versionPrefix, source, newVersionPrefix)
161+
if versionPrefix[pos+1:] != "99" {
162+
warning = fmt.Sprintf(`the current dir requires PHP %s (%s), but this version is not available: fallback to %s`, versionPrefix, source, newVersionPrefix)
163+
}
173164
versionPrefix = newVersionPrefix
174165
}
175166

0 commit comments

Comments
 (0)